将 gensim doc2vec 嵌入导出到单独的文件中,以便稍后与 keras 嵌入层一起使用
Export gensim doc2vec embeddings into separate file to use with keras Embedding layer later
我对 gensim 有点陌生,现在我正在尝试解决涉及在 keras 中使用 doc2vec 嵌入的问题。我无法在 keras 中找到 doc2vec 的现有实现 - 据我在所有示例中看到的那样,到目前为止我发现每个人都只使用 gensim 来获取文档嵌入。
一旦我在 gensim 中训练了我的 doc2vec 模型,我需要以某种方式将嵌入权重从 genim 导出到 keras,但目前还不清楚如何做到这一点。我看到了
model.syn0
假设给出了 word2vec 嵌入权重(根据 this)。但不清楚如何对文档嵌入进行相同的导出。有什么建议吗?
我知道通常我可以直接从 gensim 模型中获取每个文档的嵌入,但我想稍后在 keras 中微调嵌入层,因为文档嵌入将用作更大的嵌入层的一部分任务因此他们可能会微调一点。
我想通了。
假设您已经训练了 gensim 模型并使用字符串标签作为文档 ID:
#get vector of doc
model.docvecs['2017-06-24AEON']
#raw docvectors (all of them)
model.docvecs.doctag_syn0
#docvector names in model
model.docvecs.offset2doctag
您可以将此文档向量导出到 keras 嵌入层,如下所示,假设您的 DataFrame df 包含所有文档。请注意,在嵌入矩阵中,您只需传递整数作为输入。我使用数据框中的原始数字作为输入文档的 ID。另请注意,嵌入层要求不要触及索引 0 - 它保留用于屏蔽,因此当我将文档 ID 作为输入传递到我的网络时,我需要确保它 >0
#creating embedding matrix
embedding_matrix = np.zeros((len(df)+1, text_encode_dim))
for i, row in df.iterrows():
embedding = modelDoc2Vec.docvecs[row['docCode']]
embedding_matrix[i+1] = embedding
#input with id of document
doc_input = Input(shape=(1,),dtype='int16', name='doc_input')
#embedding layer intialized with the matrix created earlier
embedded_doc_input = Embedding(output_dim=text_encode_dim, input_dim=len(df)+1,weights=[embedding_matrix], input_length=1, trainable=False)(doc_input)
更新
2017 年底之后,随着 Keras 2.0 的引入 API 最后一行应更改为:
embedded_doc_input = Embedding(output_dim=text_encode_dim, input_dim=len(df)+1,embeddings_initializer=Constant(embedding_matrix), input_length=1, trainable=False)(doc_input)
我对 gensim 有点陌生,现在我正在尝试解决涉及在 keras 中使用 doc2vec 嵌入的问题。我无法在 keras 中找到 doc2vec 的现有实现 - 据我在所有示例中看到的那样,到目前为止我发现每个人都只使用 gensim 来获取文档嵌入。
一旦我在 gensim 中训练了我的 doc2vec 模型,我需要以某种方式将嵌入权重从 genim 导出到 keras,但目前还不清楚如何做到这一点。我看到了
model.syn0
假设给出了 word2vec 嵌入权重(根据 this)。但不清楚如何对文档嵌入进行相同的导出。有什么建议吗?
我知道通常我可以直接从 gensim 模型中获取每个文档的嵌入,但我想稍后在 keras 中微调嵌入层,因为文档嵌入将用作更大的嵌入层的一部分任务因此他们可能会微调一点。
我想通了。
假设您已经训练了 gensim 模型并使用字符串标签作为文档 ID:
#get vector of doc
model.docvecs['2017-06-24AEON']
#raw docvectors (all of them)
model.docvecs.doctag_syn0
#docvector names in model
model.docvecs.offset2doctag
您可以将此文档向量导出到 keras 嵌入层,如下所示,假设您的 DataFrame df 包含所有文档。请注意,在嵌入矩阵中,您只需传递整数作为输入。我使用数据框中的原始数字作为输入文档的 ID。另请注意,嵌入层要求不要触及索引 0 - 它保留用于屏蔽,因此当我将文档 ID 作为输入传递到我的网络时,我需要确保它 >0
#creating embedding matrix
embedding_matrix = np.zeros((len(df)+1, text_encode_dim))
for i, row in df.iterrows():
embedding = modelDoc2Vec.docvecs[row['docCode']]
embedding_matrix[i+1] = embedding
#input with id of document
doc_input = Input(shape=(1,),dtype='int16', name='doc_input')
#embedding layer intialized with the matrix created earlier
embedded_doc_input = Embedding(output_dim=text_encode_dim, input_dim=len(df)+1,weights=[embedding_matrix], input_length=1, trainable=False)(doc_input)
更新
2017 年底之后,随着 Keras 2.0 的引入 API 最后一行应更改为:
embedded_doc_input = Embedding(output_dim=text_encode_dim, input_dim=len(df)+1,embeddings_initializer=Constant(embedding_matrix), input_length=1, trainable=False)(doc_input)