数据来源是京东新款手机的评论和打分,目前收集到 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
_________________________________________________________________
1
wz74666291 2019-02-24 12:35:28 +08:00 via iPhone
你试着把 fit 的 step 减小一些试试,比如 10e-5
|
2
hanbing135 2019-02-24 14:17:12 +08:00 via Android
完全看不懂
|
3
aREMbosAl 2019-02-24 14:22:51 +08:00
label 是打分?但是用的 loss 是二分类的
|
4
ayase252 2019-02-24 15:19:10 +08:00
loss 为负?这违反代价函数定义了吧。
仔细考虑一下输出是什么 |
5
douglas1997 2019-02-24 15:52:42 +08:00
我就想知道 Epoch 15 的时候后面的 Acc 指标为什么一直是 0.001~0.002.... 压根就没有优化好。
|
6
zzj0311 2019-02-24 16:11:39 +08:00 via Android
loss 是负的是什么鬼。。这拿啥写的,sklearn ?
|
7
zzj0311 2019-02-24 16:14:48 +08:00 via Android
所以你这个输入是评分输出是一个二分类?那不可能对的嘛
|