Package: word2vec-keras
Word2Vec-Keras Text ClassifierWord2Vec-Keras is a simple Word2Vec and LSTM wrapper for text classification.It combines Gensim Word2Vec model with Keras neural network trhough an Embedding layer as input.
The Neural Network contains with LSTM layerHow installpip3 install git+https://github.com/paoloripamonti/word2vec-kerasUsagefromword2vec_kerasimportWord2VecKerasmodel=Word2VecKeras()model.train(x_train,y_train)Train Word2Vec and Keras models.
The Keras model hasEralyStoppingcallback for stopping training after 6 epochs that not improve accuracy.Train parameters:x_train: list of raw sentences, no text cleaning will be perfomedy_train: list of labelsw2v_size: (Default: 300) Word2Vec - Dimensionality of the word vectorsw2v_window: (Default: 5) Word2Vec - Maximum distance between the current and predicted word within a sentence.w2v_min_count: (Default: 1) Word2Vec - Ignores all words with total frequency lower than this.w2v_epochs: (Default: 100) Word2Vec - Number of iterations (epochs) over the corpus.k_max_sequence_len: (Default: 500) Keras - Maximum length of all sequencesk_batch_size:(Default: 128) Keras - Number of samples per gradient updatek_epochs:(Default: 32) Keras - Number of epochs to train the model. An epoch is an iteration over the entire x and y data providedk_lstm_neurons: (Default: 128) Keras - LSTM neurons per layerk_hidden_layer_neurons: (Default: [128, 64, 32]) Keras - Number of Dense layers after LSTM layer.verbose: (Default: 1) Keras- 0, 1, or 2. Verbosity mode. 0 = silent, 1 = progress bar, 2 = one line per epochEvaluatemodel.evaluate(x_test,y_test)Evaluate modelEvaluate parameters:x_test: list of raw sentences, no text cleaning will be perfomedy_test: list of labelsEvaluate result:Return a dictionary with ACCURAY, CLASSIFICATION_REPORT and CONFUSION_MATRIXPredictmodel.predict('lorem ipsum dolor sit amet consectetur adipiscing elit...',threshold=0.6)Make prediction of give textPredict parameters:x_text: Raw text, no text cleaning will be perfomedthreshold: (Default: 0.0) Cut-off threshold, if confidence il less than given value returnOTHERas labelPredict result:Return a dictionary with LABEL, CONFIDENCE and ELAPSED_TIME, i.e. {label: LABEL, confidence: CONFIDENCE, elapsed_time: TIME}Save & load modelmodel.save('/path/model.tar.gz')Save model as compressedtar.gzfile that contains several utility pickles, keras model and Word2Vec modelmodel=Word2VecKeras()model.load('/path/model.tar.gz')Load model from saved tar.gz fileExamplefromsklearn.datasetsimportfetch_20newsgroupsfromword2vec_kerasimportWord2VecKerasfrompprintimportpprint# fetch the dataset using scikit-learncategories=['alt.atheism','soc.religion.christian','comp.graphics','sci.med']train_b=fetch_20newsgroups(subset='train',categories=categories,shuffle=True,random_state=42)test_b=fetch_20newsgroups(subset='test',categories=categories,shuffle=True,random_state=42)print('size of training set:%s'%(len(train_b['data'])))print('size of validation set:%s'%(len(test_b['data'])))print('classes:%s'%(train_b.target_names))x_train=train_b.datay_train=[train_b.target_names[idx]foridxintrain_b.target]x_test=test_b.datay_test=[train_b.target_names[idx]foridxintest_b.target]model=Word2VecKeras()model.train(x_train,y_train)pprint(model.evaluate(x_test,y_test))model.save('./model.tar.gz')