【问题标题】:gensim - fasttext - Why `load_facebook_vectors` doesn't work?gensim - fasttext - 为什么`load_facebook_vectors` 不起作用?
【发布时间】:2020-05-28 07:27:12
【问题描述】:

我尝试从 fastext - wiki word vectors 加载预训练的 FastText 向量。

我的代码在下面,它运行良好。

from gensim.models import FastText
model = FastText.load_fasttext_format('./wiki.en/wiki.en.bin')

但是,警告信息有点烦人。

gensim_fasttext_pretrained_vector.py:13: DeprecationWarning: Call to deprecated `load_fasttext_format` (use load_facebook_vectors (to use pretrained embeddings)

消息说,load_fasttext_format 将被弃用,所以最好使用load_facebook_vectors

所以我决定更改代码。我更改的代码如下所示。

from gensim.models import FastText
model = FastText.load_facebook_vectors('./wiki.en/wiki.en.bin')

但是,错误发生了,错误信息是这样的。

Traceback (most recent call last):
  File "gensim_fasttext_pretrained_vector.py", line 13, in <module>
    model = FastText.load_facebook_vectors('./wiki.en/wiki.en.bin')
AttributeError: type object 'FastText' has no attribute 'load_facebook_vectors'

我不明白为什么会发生这些事情。 我只是改变了消息所说的内容,但它不起作用。 如果您对此有所了解,请告诉我。

总是,谢谢你们的帮助。

【问题讨论】:

    标签: python gensim fasttext


    【解决方案1】:

    你快到了,你需要改变两件事:

    • 首先是fasttext全小写,不是Fasttext
    • 其次,要使用load_facebook_vectors,您需要先创建一个datapath对象,然后再使用它。

    所以,你应该这样做:

    from gensim.models import fasttext
    from gensim.test.utils import datapath
    
    wv = fasttext.load_facebook_vectors(datapath("./wiki.en/wiki.en.bin"))
    

    【讨论】:

    • 感谢您的帮助。正如你所说,它在你所说的 chaging 代码之后工作。
    • 很高兴我能帮上忙
    • 我很高兴它在工作;但是请注意,您不需要使用datapath。模块 gensim.models.fasttext 上的 load_facebook_vectors() 方法将采用纯字符串,只要它指向有效文件即可。
    • 如果您使用datapath,实用程序函数将在/gensim/test/test_data/ 目录中查找文件,因此如果您在自己选择的目录中下载了fasttext 文件,则不会找到该文件。
    猜你喜欢
    • 2021-03-31
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-14
    • 1970-01-01
    • 2018-11-11
    相关资源
    最近更新 更多