Fasttext for Python - module 'fasttext' has no attribute 'load_model'

旧巷老猫 提交于 2020-08-23 09:29:08

问题


Please forgive my newbness here, but fasttext is not working for me on python. I am using anaconda running python 3.6. My code is as follows(just an example):

import fasttext 

model = fasttext.load_model('/home/sproc/share/fastText/model.bin')
print(model.words)

This returns the following error:

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'

Does the same thing with cbow and skipgram when trying to create word vectors. I check the init.py file from the .../site-packages/fasttext directory and it imports said attributes, but they are not part of the model.py module. I'm guessing this has something to do with the shared object file but I am not sure. Any help is greatly appreciated.


回答1:


Here is a solution that worked for me when I got the error you are getting;

Import FastText

from gensim.models.wrappers import FastText 

Load the binary

model=FastText.load_fasttext_format('wiki.simple.bin')



回答2:


Rename your python file . Don't name it as fasttext.py .If your name it like this , what you import by "import fasttext.py " will be your own file. You can rename it as 'fast_text.py' or something else .




回答3:


If you install fastText package instead of the old fasttext, then

import fastText 
model = fastText.load_model('/home/sproc/share/fastText/model.bin')

should work as expected.




回答4:


@spencerktm30 I recommend you using pyfasttext instead of fasttext which is no longer active and it has a lot of bugs. link to pyfasttext

Actually, I faced similar issue when trying to load a C++ pre trained model and I had to switch to using pyfasttext to get it to work.

So this should hopefully work for you:

>>> from pyfasttext import FastText
>>> model = FastText('/home/sproc/share/fastText/model.bin')



回答5:


Rename the file from fasttext.py to another name, it will work.




回答6:


Apparently there are different fasttext python libraries out there!
fasttext != fasttext-win



来源:https://stackoverflow.com/questions/45377306/fasttext-for-python-module-fasttext-has-no-attribute-load-model

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!