Train.npz, test.npz 以及embedding_matrix.npy的产生请看这本博客
完整代码如下
import os import numpy as np import tensorflow.keras as keras import tensorflow.keras.layers as layers import tensorflow as tf import time root_folder = './lstm_attention' def get_dataset(): train_set = np.load('./train_data_new1/train.npz') X_train = train_set['x'] y_train = train_set['y'] test_set = np.load('./train_data_new1/test.npz') X_test = test_set['x'] y_test = test_set['y'] print("X_train:", X_train.shape) print("y_train:", y_train.shape) print("X_test:", X_test.shape) print("y_test:", y_test.shape) return X_train, y_train, X_test, y_test def lstm_model(): embedding_matrix = np.load('./train_data_new1/embedding_matrix.npy') input = tf.keras.Input(batch_shape=(None, 200)) embed = tf.keras.layers.Embedding(input_dim=30001, output_dim=50, weights=[embedding_matrix],trainable=True)(input) bilstm = tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(128, return_sequences=True, dropout=0.5))(embed) attention_layer = tf.keras.layers.Attention()([bilstm, bilstm]) pooling_out1 = tf.keras.layers.GlobalMaxPooling1D()(bilstm) pooling_out2 = tf.keras.layers.GlobalMaxPooling1D()(attention_layer) merge = tf.keras.layers.Concatenate()([pooling_out1, pooling_out2]) mlp_hidden_out = tf.keras.layers.Dense(64, activation=tf.nn.relu)(merge) out = tf.keras.layers.Dense(2, activation=tf.nn.softmax)(mlp_hidden_out) model = tf.keras.Model(inputs=[input], outputs=[out]) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.summary() return model current_max_loss =9999 def train_my_model(model, X_train, y_train): weight_dir = root_folder+'\weight.h5' if os.path.isfile(weight_dir): print('load weight') model.load_weights(weight_dir) def save_weight(epoch, logs): global current_max_loss if(logs['val_loss'] is not None and logs['val_loss']< current_max_loss): current_max_loss = logs['val_loss'] print('save_weight', epoch, current_max_loss) model.save_weights(weight_dir) model.save(root_folder+'\model.h5', include_optimizer=False) batch_print_callback = keras.callbacks.LambdaCallback( on_epoch_end=save_weight ) callbacks = [ tf.keras.callbacks.EarlyStopping(patience=4, monitor='loss'), batch_print_callback, tf.keras.callbacks.TensorBoard(log_dir=root_folder+'\logs') ] begin = time.time() history = model.fit(X_train, y_train, batch_size=128, epochs=25,validation_split=0.1, callbacks= callbacks) finish = time.time() print("train time: ", (finish - begin), 's') import matplotlib.pyplot as plt plt.plot(history.history['accuracy']) plt.plot(history.history['val_accuracy']) plt.legend(['accuracy', 'val_accuracy'], loc='upper left') plt.show() def test_my_module(model, X_test, y_test): if os.path.isfile(root_folder+'/model.h5'): print('load weight') model.load_weights(root_folder+'/model.h5') test_result = model.evaluate(X_test, y_test) print('test Result', test_result) print('Test ',test_result) def predict_my_module(model): small_word_index = np.load('./train_data_new1/small_word_index.npy', allow_pickle=True) review_index = np.zeros((1, 200), dtype=int) #review = "I don't like it" #review = "this is bad movie " #review = "This is good movie" #review = "This isn‘t great movie" #review = "i think this is bad movie" #review = "I'm not very disappoint for this movie" #review = "I'm not very disappoint for this movie" review = "I'm very happy for this movie" #neg:0 postive:1 counter = 0 for word in review.split(): try: print(word, small_word_index.item()[word]) review_index[0][counter] = small_word_index.item()[word] counter = counter + 1 except Exception: print('Word error', word) print(review_index.shape) s = model.predict(x=review_index) print(s) if __name__ == '__main__': X_train, y_train, x_test, y_test = get_dataset() model = lstm_model() train_my_model(model, X_train, y_train) test_my_module(model,x_test, y_test) #predict_my_module(model)测试集上的结果是 90.78%
15000/15000 [==============================] - 5s 340us/sample - loss: 0.2375 - accuracy: 0.9078 test Result [0.2374931192914645, 0.9078] Test [0.2374931192914645, 0.9078]训练过程中损失函数以及准确率的变化曲线如下 在训练后,以通过下面命令看 tensorboard --logdir=logs