Python由于gensim版本原因Word2vec调用时3个报错问题解决办法

Python由于gensim版本原因Word2vec调用时3个报错问题解决办法,第1张

报错1:TypeError: init() got an unexpected keyword argument ‘size‘
根据官方手册,将 size改为vector_size
报错2:TypeError: init() got an unexpected keyword argument ‘iter‘
根据官方手册,将 iter改为epochs
报错3:TypeError: ‘Word2Vec’ object is not subscriptable
根据官方手册,原始调用词向量直接使用命令model即可,现命令为model.wv
例如:我将报错的三个部分依次进行修改,即可避免以上三个报错。

同时,第三个报错可以结合以下代码进行参考理解:

//为了获得某个词的词向量
model = Word2Vec(sentences, min_count=1)

## 原来的代码:出现报错的地方
print(model['sentence'])

## 修改后的代码
print(model.wv['sentence'])

整体代码:

//整体代码
from gensim.models import Word2Vec
# define training data
sentences = [['this', 'is', 'the', 'first', 'sentence', 'for', 'word2vec'],
            ['this', 'is', 'the', 'second', 'sentence'],
            ['yet', 'another', 'sentence'],
            ['one', 'more', 'sentence'],
            ['and', 'the', 'final', 'sentence']]
# train model
model = Word2Vec(sentences, min_count=1)
# summarize the loaded model
print(model)
# summarize vocabulary
words = list(model.wv.key_to_index)
print(words)
# access vector for one word
print(model.wv['sentence'])   
# save model
model.save('model.bin')
# load model
new_model = Word2Vec.load('model.bin')
print(new_model)

参考文档: 第三个报错的理解代码参考的这个老哥

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/942207.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-18
下一篇 2022-05-18

发表评论

登录后才能评论

评论列表(0条)

保存