<p class="content">下面回到前面的评论文本鉴定问题,不改变任何其他网络参数,仅是使用LSTM层替换Simple RNN层,然后看看效率是否会有所提升:</p> <div class="content_106"> <p class="content_105">from keras.models import Sequential # 导入序贯模型</p> <p class="content_105">from keras.layers.embeddings import Embedding #导入词嵌入层</p> <p class="content_105">from keras.layers import Dense #导入全连接层</p> <p class="content_105">from keras.layers import LSTM #导入LSTM层</p> <p class="content_105">embedding_vecor_length = 60 # 设定词嵌入向量长度为60</p> <p class="content_105">lstm = Sequential() #序贯模型</p> <p class="content_105">lstm.add(Embedding(dictionary_size, embedding_vecor_length,</p> <p class="content_111">input_length=max_comment_length)) # 加入词嵌入层</p> <p class="content_105">lstm.add(LSTM(100)) # 加入LSTM层</p> <p class="content_105">lstm.add(Dense(10, activation='relu')) # 加入全连接层</p> <p class="content_105">lstm.add(Dense(6, activation='softmax')) # 加入分类输出层</p> <p class="content_105">lstm.compile(loss='sparse_categorical_crossentropy', #损失函数</p> <p class="content_119">optimizer = 'adam', # 优化器</p> <p class="content_119">metrics = ['acc']) # 评估指标</p> <p class="content_105">history = rnn.fit(X_train, y_train,</p> <p class="content_124">validation_split = 0.3,</p> <p class="content_124">epochs=10,</p> <p class="content_124">batch_size=64)</p> </div> <p class="content">输出结果显示,同样训练10轮之后,验证集准确率为0.6171,比Simple RNN更准确了。</p> <div class="content_113"> <p class="content_109">Train on 7000 samples, validate on 3000 samples</p> <p class="content_109">Epoch 1/10</p> <p class="content_109">15848/15848 [============================] - 88s 6ms/step - loss: 1.2131 - acc:</p> <p class="content_109">0.5856 - val_loss: 1.0130 - val_acc: 0.6030</p> <p class="content_109">Epoch 2/10</p> <p class="content_109">15848/15848 [============================] - 87s 5ms/step - loss: 0.8891 - acc:</p> <p class="content_109">0.6363 - val_loss: 0.9449 - val_acc: 0.6015</p> <p class="content_109">… …</p> <p class="content_109">Epoch 10/10</p> <p class="content_109">15848/15848 [============================] - 88s 6ms/step - loss: 0.7999 - acc:</p> <p class="content_109">0.6661 - val_loss: 0.9389 - val_acc: 0.6171</p> </div>