使用 lstm 进行 imdb 评论获得非常低的准确性



我已经使用Word2Vec将imdb评论转换为300维度。

我一直保留了 embedding_vecor_length = 32, input_length = 300 的 25000 条评论。

我得到的准确度很差,损失很大。

在 10 个 epoch 结束时,我得到的精度为 0.4977,损失为 0.6932。

embedding_vecor_length = 32
model = Sequential()
model.add(Embedding(25000, embedding_vecor_length, input_length=300))
model.add(LSTM(100))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics['accuracy'])

我应该添加或删除什么才能提高准确性并减少损失?

> 25000 似乎是您拥有的样本数,而不是嵌入层的输入维度。我认为您应该检查该函数中想要的尺寸。我认为,在没有看到您的数据的情况下,您真正想要的是:

model.add(Embedding(300, embedding_vecor_length))

但是由于您已经使用了word2vec,这已经是一个嵌入!您不需要嵌入层。我认为您应该删除它,然后查看您的准确性。

你可以使用预训练词嵌入手套,你可以使用glove.6B.50d.txt,你可以从 http://nlp.stanford.edu/data/glove.6B.zip 下载, 使用 50D

def read_glove_vecs(glove_file):
with open(glove_file,'r',encoding='UTF-8') as f:
words = set()
word_to_vec_map = {}
for line in f:
line = line.strip().split()
curr_word = line[0]
words.add(curr_word)
word_to_vec_map[curr_word] = np.array(line[1:], dtype=np.float64)
i = 1
words_to_index = {}
index_to_words = {}
for w in sorted(words):
words_to_index[w] = I
index_to_words[i] = w
i = i + 1
return words_to_index, index_to_words, word_to_vec_map

现在调用上面的函数,它将返回

word_to_index, index_to_word, word_to_vec_map = read_glove_vecs('data/glove.6B.50d.txt')

现在从这些预训练的单词创建单词嵌入

vocab_len = len(word_to_index) 
emb_dim = 50 # the above word vector are trained for 50 dim
emb_matrix = np.zeros((vocab_len, emb_dim))
for word, index in word_to_index.items():
emb_matrix[index,:] = word_to_vec_map[word]
embedding_layer = Embedding(vocab_len, emb_dim, trainable = False)
embedding_layer.build((None,))
embedding_layer.set_weights([emb_matrix])

现在使用它,在您的模型中嵌入层,这将提高您的准确性

最新更新