LSTM的loss和accuracy近乎不变问题
原因(如果我的理解不对请纠正,万分感谢) lstm不是线性准确度,这点与cnn不同,之前做cnn认为输出都是显性的,而lstm需要不定的多次迭代才会开始提高准确度 解决方法 更换模型结构,损失函数,标签样式,迭代次数,或者重新生成数据集,重新进行训练等 参考代码 train.py
import h5py
import numpy as np
from tensorflow.keras import layers, models
f1 = h5py.File('train_lstm.h5', 'r')
train_x = np.array(f1['train_content'])
train_y = np.array(f1['train_label'])
f2 = h5py.File('test_lstm.h5', 'r')
test_x = np.array(f2['test_content'])
test_y = np.array(f2['test_label'])
print(train_x)
print(train_y)
print(train_x.shape)
print(train_y.shape)
model = models.Sequential()
model.add(layers.LSTM(50, input_shape=(200, 100)))
model.add(layers.Dense(2, activation='softmax'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(train_x, train_y, epochs=10, batch_size=1, verbose=2)
model.save('model_lstm1.h5')
result = model.evaluate(test_x, test_y)
print('测试结果:', result)
test.py
import random
import tensorflow as tf
import numpy as np
import re
import jieba
from gensim.models import word2vec
label_list = ['虚假评论', '真实评论']
model = tf.keras.models.load_model(r'model_lstm1.h5')
contents = ['好吃,非常不错,口感挺软的,不会觉得噎,不会干是属于比较新鲜的口感。', '口味不错,热量也不高。吃着非常好,非常愉快的一次购物体验。', '做工蛮精细的,木材表面磨得很平滑。']
word = word2vec.Word2Vec.load('word2vec.model')
r = r"[a-zA-Z0-9\s+\.\!\/_,$%^*(+\"\']+|[+——!,。?、:;;《》“”~@#¥%……&*()]+"
l = []
for content in contents:
data = re.sub(r, '', content)
fc = list(jieba.cut(data))
print(fc)
data_list = np.zeros((200, 100))
num = 0
for j in fc:
if j in word.wv:
data_list[num] = list(word.wv[j])
else:
data_list[num] = [0]
num += 1
print(data_list)
l.append(data_list)
content = np.array(l)
model.summary()
predictions = model.predict(content, verbose=0)
print(predictions)
现在又不行了,哎,LSTM这玩意是真的玄学 部分成功样例 双输出二分类损失函数
model = models.Sequential()
model.add(layers.LSTM(50, input_shape=(200, 100)))
model.add(layers.Dense(2, activation='softmax'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(train_x, train_y, epochs=50, batch_size=1, verbose=2)
单输出(不符合二分类标准)
model = models.Sequential()
model.add(layers.LSTM(50, input_shape=(200, 100)))
model.add(layers.Dense(1))
model.compile(loss='mse', optimizer='adam', metrics=['accuracy'])
x_train, y_train = get_train()
x_test, y_test = get_test()
model.fit(x_train, y_train, epochs=50, batch_size=1, verbose=2)
纪念下卡了我两天的破LSTM模型,555~
|