数据来源是京东新款手机的评论和打分,目前收集到 2500 条数据,但打分小于 5 分的只有不到 40 条
训练模型层次是:嵌入层-》 LSTM-》 Dense-》 Dense-》输出层 嵌入层的初始数据为 word2vec 训练的词向量 输入的训练数据为词索引,标记为打分
部分代码如下:
def main():
x_train = pad_seq()
y_train = star()
x_train, y_train, x_test, y_test = set_data(x_train, y_train)
model = Sequential()
model.add(Embedding(input_dim=input_dim+1, output_dim=output_dim, input_length=k, embeddings_initializer=my_init))
model.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2, activation='sigmoid'))
model.add(Dense(256, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(1,activation='sigmoid'))
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
model.fit(x_train, y_train, batch_size=batch_size, epochs=15)
score, acc = model.evaluate(x_test, y_test, batch_size=batch_size)
print('Test score:', score)
print('Test accuracy:', acc)
然而训练结果是这样的。。。
Epoch 15/15
32/2015 [..............................] - ETA: 0s - loss: -63.2713 - acc: 0.0000e+00
160/2015 [=>............................] - ETA: 0s - loss: -63.4706 - acc: 0.0000e+00
288/2015 [===>..........................] - ETA: 0s - loss: -63.5481 - acc: 0.0000e+00
384/2015 [====>.........................] - ETA: 0s - loss: -63.2713 - acc: 0.0026
480/2015 [======>.......................] - ETA: 0s - loss: -63.3046 - acc: 0.0021
608/2015 [========>.....................] - ETA: 0s - loss: -63.4024 - acc: 0.0016
736/2015 [=========>....................] - ETA: 0s - loss: -63.3796 - acc: 0.0027
864/2015 [===========>..................] - ETA: 0s - loss: -63.3821 - acc: 0.0023
992/2015 [=============>................] - ETA: 0s - loss: -63.3838 - acc: 0.0020
1120/2015 [===============>..............] - ETA: 0s - loss: -63.3852 - acc: 0.0018
1248/2015 [=================>............] - ETA: 0s - loss: -63.3991 - acc: 0.0016
1376/2015 [===================>..........] - ETA: 0s - loss: -63.4104 - acc: 0.0015
1504/2015 [=====================>........] - ETA: 0s - loss: -63.3879 - acc: 0.0020
1632/2015 [=======================>......] - ETA: 0s - loss: -63.4081 - acc: 0.0018
1760/2015 [=========================>....] - ETA: 0s - loss: -63.4344 - acc: 0.0017
1888/2015 [===========================>..] - ETA: 0s - loss: -63.4233 - acc: 0.0021
2015/2015 [==============================] - 1s 470us/step - loss: -63.4214 - acc: 0.0020
32/504 [>.............................] - ETA: 2s
504/504 [==============================] - 0s 412us/step
Test score: -63.769539061046785
Test accuracy: 0.0
Process finished with exit code 0
model 如下:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding_1 (Embedding) (None, 20, 50) 49350
_________________________________________________________________
lstm_1 (LSTM) (None, 128) 91648
_________________________________________________________________
dense_1 (Dense) (None, 256) 33024
_________________________________________________________________
dense_2 (Dense) (None, 128) 32896
_________________________________________________________________
dense_3 (Dense) (None, 1) 129
=================================================================
Total params: 207,047
Trainable params: 207,047
Non-trainable params: 0
_________________________________________________________________
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.