请选择 进入手机版 | 继续访问电脑版
查看: 1456|回复: 0

keras使用word2vec pretrained vector注意事项

[复制链接]

665

主题

1234

帖子

6557

积分

xdtech

Rank: 5Rank: 5

积分
6557
发表于 2019-9-18 14:23:54 | 显示全部楼层 |阅读模式
在使用预训练的embedding层的时候,一定要注意词表的index,在word2vec中,
model.wv.index2word 这个是一个list, index就是词的index,这个是固定的,即便是换到linux平台,这个index也是不变的,所以使用这个。
w2v_for_s2s = Word2Vec.load('model/word2vec_6_3_word.bin')
word2idx = {"UNK": 0}
# vocab_list = [(k, w2v_for_s2s.wv[k]) for k, v in w2v_for_s2s.wv.vocab.items()]
index2word = w2v_for_s2s.wv.index2word
embeddings_matrix = np.zeros((len(index2word) + 1, w2v_for_s2s.vector_size))
print(index2word[:50])
for i in range(len(index2word)):
    word = index2word
    word2idx[word] = i + 1
    embeddings_matrix[i + 1] = w2v_for_s2s.wv[word]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表