【发布时间】:2021-03-12 11:52:24
【问题描述】:
我正在使用 seq2seq 模型进行文本生成,其中使用了 GloVe 嵌入。我想在此代码中使用嵌入的自定义 Word2Vec (CBOW/Gensim)。谁能帮忙使用我的自定义嵌入而不是 GloVe?
def initialize_embeddings(self):
"""Reads the GloVe word-embeddings and creates embedding matrix and word to index and index to word mapping."""
# load the word embeddings
self.word2vec = {}
with open(glove_path%self.EMBEDDING_DIM, 'r') as file:
for line in file:
vectors = line.split()
self.word2vec[vectors[0]] = np.asarray(vectors[1:], dtype="float32")```
```# get the embeddings matrix
self.num_words = min(self.MAX_VOCAB_SIZE, len(self.word2idx)+1)
self.embeddings_matrix = np.zeros((self.num_words, self.EMBEDDING_DIM))
for word, idx in self.word2idx.items():
if idx <= self.num_words:
word_embeddings = self.word2vec.get(word)
if word_embeddings is not None:
self.embeddings_matrix[idx] = word_embeddings
self.idx2word = {v:k for k,v in self.word2idx.items()}
此代码用于将 GloVe 嵌入转换为 Word2Vec。我想加载我自己的 Word2Vec 嵌入。
【问题讨论】:
-
您的代码有什么问题?您是否已经创建了自定义嵌入?如果是这样,您如何/在哪里保存它? (Gensim 已经有编写/读取自己的模型和其他词向量格式的方法 - 因此通常不需要像这种自定义读取/拆分这样的代码。)
标签: keras stanford-nlp gensim word2vec seq2seq