如何解决使用自定义 Word2Vec 嵌入代替 GloVe
我正在使用 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 嵌入。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。