【问题标题】:How to use own word embedding with pre-trained embedding like word2vec in Keras如何在 Keras 中将自己的词嵌入与预训练的嵌入(如 word2vec)一起使用
【发布时间】:2019-02-08 06:39:58
【问题描述】:

我有一个存储在 CSV 文件中的共现矩阵,其中包含单词和表情符号之间的关系,如下所示:

word emo1 emo2 emo3
w1   0.5   0.3  0.2
w2   0.8   0    0
w3   0.2   0.5  0.2

这个共现矩阵很大,有1584755 行和621 列。我在Keras 中有一个Sequential() LSTM 模型,我在其中使用预训练的(word2vec)词嵌入。现在我想使用共现矩阵作为另一个嵌入层。我怎样才能做到这一点?我当前的代码是这样的:

model = Sequential()
model.add(Embedding(max_features, embeddings_dim, input_length=max_sent_len, weights=[embedding_weights]))
model.add(Dropout(0.25))
model.add(Convolution1D(nb_filter=nb_filter, filter_length=filter_length, border_mode='valid', activation='relu', subsample_length=1))
model.add(MaxPooling1D(pool_length=pool_length))
model.add(LSTM(embeddings_dim))
model.add(Dense(reg_dimensions))
model.add(Activation('sigmoid'))
model.compile(loss='mean_absolute_error', optimizer='adam')
model.fit( train_sequences , train_labels , nb_epoch=30, batch_size=16) 

另外,如果共现矩阵是稀疏的,那么在嵌入层中使用它的最佳方法是什么?

【问题讨论】:

    标签: python keras nlp lstm word-embedding


    【解决方案1】:

    您可以使用Embedding 层并像这样设置自己的权重矩阵:

    Embedding(n_in, n_out, trainable=False, weights=[weights])
    

    如果我理解正确,weights 将是您的共现矩阵,n_in 是行数,n_out 是列数。

    您可以在this 博文中找到更多信息和示例。

    【讨论】:

    • 好的,谢谢,但共现矩阵存储在 csv 文件中。如何将文件链接到嵌入?博客文章在嵌入中使用了测试和训练数据,但我的测试和训练数据与矩阵不同。
    • @AbuShoeb 然后您必须在代码中加载csv,使用pandasnumpy。无法直接将文件“链接”到权重。
    猜你喜欢
    • 2019-02-07
    • 2016-06-11
    • 2019-11-14
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    相关资源
    最近更新 更多