我正在使用fastText来获取类似的单词。我已经训练了我的模型,但是当我使用model.most_simlar
时,有一个错误。
from pprint import pprint
import gensim
from gensim.models import FastText
pprint(test_model.most_similar('room', topn=10))
这里是错误。
AttributeError: '_FastText' object has no attribute 'most_similar'
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-24-15ea06598df9> in <module>
2 import gensim
3 from gensim.models import FastText
----> 4 pprint(test_model.most_similar('部屋', topn=10))
AttributeError: '_FastText' object has no attribute 'most_similar'
您的代码没有显示您的test_model
对象来自哪里。(它是在哪里创建、训练或加载的?)
错误提到了_FastText
类型的对象,这使我认为它是而不是GensimFastText
模型-它没有以_
开头的字符命名。它可能来自其他FastText实现,比如Facebook Python包装器?
如果是,该对象没有.most_similar()
实用程序方法。看起来该库的示例需要外部计算来搜索附近的单词,例如本例中使用的find_nearest_neighbor()
实用函数:
https://github.com/facebookresearch/fastText/blob/a20c0d27cd0ee88a25ea0433b7f03038cd728459/python/doc/examples/compute_accuracy.py预示
如果你想使用Gensim的FastText
,你只需要使用这个类来训练你的模型,或者使用像Gensim的load_facebook_model()
:
这样的函数将Facebook代码训练的模型加载到Gensim类中。https://radimrehurek.com/gensim/models/fasttext.html gensim.models.fasttext.load_facebook_model
然后,你的模型将是gensim.models.fasttext.FastText
类型,并且在其.wv
属性中有一组词向量,可以对其进行.most_similar()
之类的操作:
from gensim.models.fasttext import load_facebook_model
ft_model = load_facebook_model(YOUR_PATH_TO_FACEBOOK_BIN_FILE)
print(ft_model.wv.most_similar('部屋')