![](/img/trans.png)
[英]Can't suppress fasttext warning: 'load_model' does not return […]
[英]Fasttext for Python - module 'fasttext' has no attribute 'load_model'
請原諒我的新手,但 fasttext 在 python 上不適合我。 我正在使用運行 python 3.6 的 anaconda。 我的代碼如下(只是一個例子):
import fasttext
model = fasttext.load_model('/home/sproc/share/fastText/model.bin')
print(model.words)
這將返回以下錯誤:
Traceback (most recent call last):
File "/media/sf_VBoxShare/LiClipseWorkspace/test/testpack/fasttext.py", line 1, in <module>
import fasttext
File "/media/sf_VBoxShare/LiClipseWorkspace/test/testpack/fasttext.py", line 3, in <module>
model = fasttext.load_model('/home/sproc/share/fastText/model.bin')
AttributeError: module 'fasttext' has no attribute 'load_model'
在嘗試創建詞向量時,對 cbow 和 skipgram 做同樣的事情。 我檢查了.../site-packages/fasttext 目錄中的init .py 文件,它導入了上述屬性,但它們不是 model.py 模塊的一部分。 我猜這與共享對象文件有關,但我不確定。 任何幫助是極大的贊賞。
當我收到您遇到的錯誤時,這是一個對我有用的解決方案;
導入 FastText
from gensim.models.wrappers import FastText
加載二進制文件
model=FastText.load_fasttext_format('wiki.simple.bin')
重命名您的 python 文件。 不要將它命名為fasttext.py。如果你這樣命名,你通過“import fasttext.py”導入的將是你自己的文件。 您可以將其重命名為“fast_text.py”或其他名稱。
如果你安裝fastText包而不是舊的 fasttext,那么
import fastText
model = fastText.load_model('/home/sproc/share/fastText/model.bin')
應該按預期工作。
@spencerktm30 我建議您使用 pyfasttext 而不是 fasttext,它不再處於活動狀態並且有很多錯誤。 鏈接到pyfasttext
實際上,我在嘗試加載 C++ 預訓練模型時遇到了類似的問題,我不得不切換到使用 pyfasttext 來讓它工作。
所以這應該對你有用:
>>> from pyfasttext import FastText
>>> model = FastText('/home/sproc/share/fastText/model.bin')
將文件從 fasttext.py 重命名為另一個名稱,它將起作用。
顯然那里有不同的fasttext
python 庫!
fasttext != fasttext-win
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.