<s> from gensim . models . word2vec import Word2Vec <EOL> from regression import BaseBowRegressor <EOL> import language <EOL> import gc <EOL> from keras . preprocessing import sequence <EOL> from keras . optimizers import SGD , RMSprop , Adagrad <EOL> from keras . utils import np_utils <EOL> from keras . models import Sequential <EOL> from keras . layers . core import Dense , Dropout , Activation <EOL> from keras . layers . embeddings import Embedding <EOL> from keras . layers . recurrent import LSTM , GRU <EOL> from evaluation import rmslog_error , rmslog_loss <EOL> import numpy as np <EOL> from sklearn . metrics import mean_squared_error , mean_absolute_error <EOL> WORDVECTOR_LENGTH = <NUM_LIT:100> <EOL> UNKNOWN_VECTOR = np . zeros ( WORDVECTOR_LENGTH ) <EOL> def tokens_to_word_vectors ( reviews_tokens , w2v_model ) : <EOL> X = [ ] <EOL> for review in reviews_tokens : <EOL> wvs = [ ] <EOL> for token in review : <EOL> if token in w2v_model : <EOL> wvs . append ( w2v_model [ token ] ) <EOL> else : <EOL> wvs . append ( UNKNOWN_VECTOR ) <EOL> X . append ( wvs ) <EOL> return X <EOL> def pad_sequence_word_vectors ( sequences , maxlen = None ) : <EOL> lengths = [ len ( s ) for s in sequences ] <EOL> nb_samples = len ( sequences ) <EOL> if maxlen is None : <EOL> maxlen = np . max ( lengths ) <EOL> x = np . zeros ( ( nb_samples , maxlen , WORDVECTOR_LENGTH ) ) . astype ( '<STR_LIT>' ) <EOL> for idx , s in enumerate ( sequences ) : <EOL> x [ idx , : lengths [ idx ] ] = s [ : maxlen ] <EOL> return x <EOL> VOTES_THRESHOLD = <NUM_LIT:2> <EOL> def give_balanced_classes ( reviews , funny_votes ) : <EOL> funny_reviews = [ ] <EOL> not_funny_reviews_indices = [ ] <EOL> final_reviews = [ ] <EOL> final_labels = [ ] <EOL> for i , review in enumerate ( reviews ) : <EOL> if funny_votes [ i ] >= VOTES_THRESHOLD : <EOL> final_reviews . append ( review ) <EOL> final_labels . append ( <NUM_LIT:1> ) <EOL> elif funny_votes [ i ] == <NUM_LIT:0> : <EOL> not_funny_reviews_indices . append ( i ) <EOL> np . random . shuffle ( not_funny_reviews_indices ) <EOL> num_funny_reviews = len ( final_reviews ) <EOL> for i in range ( num_funny_reviews ) : <EOL> final_reviews . append ( reviews [ not_funny_reviews_indices [ i ] ] ) <EOL> final_labels . append ( <NUM_LIT:0> ) <EOL> combined_lists = zip ( final_reviews , final_labels ) <EOL> np . random . shuffle ( combined_lists ) <EOL> final_reviews [ : ] , final_labels [ : ] = zip ( * combined_lists ) <EOL> print "<STR_LIT>" % ( num_funny_reviews , len ( final_reviews ) ) <EOL> return ( final_reviews , final_labels ) <EOL> WORD2VEC_MODEL = "<STR_LIT>" <EOL> PARTITIONS_TRAINING = range ( <NUM_LIT:1> , <NUM_LIT:30> ) <EOL> PARTITIONS_TESTING = range ( <NUM_LIT:50> , <NUM_LIT> ) <EOL> w2vmodel = Word2Vec . load ( WORD2VEC_MODEL ) <EOL> reviews_train , _ , funny_votes_train , _ , _ = BaseBowRegressor . get_reviews_data ( PARTITIONS_TRAINING ) <EOL> reviews_train , labels_train = give_balanced_classes ( reviews_train , funny_votes_train ) <EOL> print "<STR_LIT>" <EOL> NUM_ELEMENTS_TRAIN = None <EOL> NUM_ELEMENTS_TEST = None <EOL> reviews_tokens_train = [ language . tokenize_document ( ( i , unicode ( txt ) ) ) for ( i , txt ) in enumerate ( reviews_train [ : NUM_ELEMENTS_TRAIN ] ) ] <EOL> X_train = tokens_to_word_vectors ( reviews_tokens_train , w2vmodel ) <EOL> reviews_tokens_train = None <EOL> reviews_train = None <EOL> gc . collect ( ) <EOL> X_train = np . array ( X_train ) <EOL> labels_train = np . array ( labels_train [ : NUM_ELEMENTS_TRAIN ] ) <EOL> print "<STR_LIT>" <EOL> reviews_test , _ , funny_votes_test , _ , _ = BaseBowRegressor . get_reviews_data ( PARTITIONS_TESTING ) <EOL> reviews_test , labels_test = give_balanced_classes ( reviews_test , funny_votes_test ) <EOL> reviews_tokens_test = [ language . tokenize_document ( ( i , unicode ( txt ) ) ) for ( i , txt ) in enumerate ( reviews_test [ : NUM_ELEMENTS_TEST ] ) ] <EOL> reviews_test = None <EOL> gc . collect ( ) <EOL> maxlen = <NUM_LIT:100> <EOL> batch_size = <NUM_LIT:16> <EOL> print ( "<STR_LIT>" ) <EOL> X_train = pad_sequence_word_vectors ( X_train , maxlen = maxlen ) <EOL> print ( '<STR_LIT>' , X_train . shape ) <EOL> print ( '<STR_LIT>' ) <EOL> model = Sequential ( ) <EOL> model . add ( GRU ( WORDVECTOR_LENGTH , <NUM_LIT> ) ) <EOL> model . add ( Dropout ( <NUM_LIT> ) ) <EOL> model . add ( Dense ( <NUM_LIT> , <NUM_LIT:1> ) ) <EOL> model . add ( Activation ( '<STR_LIT>' ) ) <EOL> model . compile ( loss = '<STR_LIT>' , optimizer = '<STR_LIT>' , class_mode = "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> model . fit ( X_train , labels_train , batch_size = batch_size , nb_epoch = <NUM_LIT:15> , validation_split = <NUM_LIT:0.1> , show_accuracy = True ) <EOL> X_test = tokens_to_word_vectors ( reviews_tokens_test , w2vmodel ) <EOL> X_test = np . array ( X_test ) <EOL> labels_test = np . array ( labels_test [ : NUM_ELEMENTS_TEST ] ) <EOL> print "<STR_LIT>" <EOL> X_test = pad_sequence_word_vectors ( X_test , maxlen = maxlen ) <EOL> print ( '<STR_LIT>' , X_test . shape ) <EOL> score = model . evaluate ( X_test , labels_test , batch_size = batch_size ) <EOL> print ( '<STR_LIT>' , score ) <EOL> classes = model . predict_classes ( X_test , batch_size = batch_size ) <EOL> acc = np_utils . accuracy ( classes , labels_test ) <EOL> print ( '<STR_LIT>' , acc ) </s>
