【问题标题】:Getting very low accuracy using lstm for imdb reviews使用 lstm 进行 imdb 评论获得非常低的准确性
【发布时间】:2018-10-10 07:03:10
【问题描述】:

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

我保留了 embedding_vecor_length = 32、input_length = 25000 条评论中的 300 条。

我的准确率很差,而且损失很高。

在 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'])

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

【问题讨论】:

    标签: python lstm word2vec loss


    【解决方案1】:

    25000 似乎是您拥有的样本数,而不是嵌入层的输入维度。我认为您应该检查该功能中所需的尺寸。我认为,没有看到你的数据,你真正想要的是:

    model.add(Embedding(300, embedding_vecor_length))
    

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

    【讨论】:

    • 我尝试了上面建议的方式,没有太大变化。通过删除嵌入层,我只是将其注释掉,这给了我一个错误 ValueError:此模型尚未构建。首先通过调用 build() 或使用一些数据调用 fit() 来构建模型。或者在第一层指定 input_shape 或 batch_input_shape 进行自动构建。
    • 是的,当您启动模型时,您必须指定输入形状。 model.add(LSTM(100,input_shape=(sequence_length,300)))
    【解决方案2】:

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

    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])
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-26
      • 1970-01-01
      • 2020-04-20
      • 1970-01-01
      • 1970-01-01
      • 2019-08-05
      • 1970-01-01
      相关资源
      最近更新 更多