当前位置:   article > 正文

[深度学习-实战]LSTM Attention训练IMDB文本的情感分析_lstm-attention情感分析

lstm-attention情感分析

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)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118

测试集上的结果是 90.78%

15000/15000 [==============================] - 5s 340us/sample - loss: 0.2375 - accuracy: 0.9078
test Result [0.2374931192914645, 0.9078]
Test  [0.2374931192914645, 0.9078]
  • 1
  • 2
  • 3

训练过程中损失函数以及准确率的变化曲线如下
在训练后,以通过下面命令看
tensorboard --logdir=logs

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/715966
推荐阅读
相关标签
  

闽ICP备14008679号