【问题标题】:FastText: TypeError: loadModel(): incompatible function argumentsFastText:TypeError:loadModel():不兼容的函数参数
【发布时间】:2021-11-24 12:49:03
【问题描述】:

编辑:在 Windows 上工作

我只想加载一个已经下载的 fasttext 嵌入模型,但出现错误(见底部)我找不到解决方案。这是代码:

import fasttext
from pathlib import Path

base_path = Path("..")
fasttext_model = base_path / "models" / "cc.de.300.bin"

class EmbeddingVectorizer:
    def __init__(self):

        self.embedding_model = fasttext.load_model(fasttext_model)

    def __call__(self, doc):
        """
        Convert address to embedding vectors
        :param address: The address to convert
        :return: The embeddings vectors
        """
        embeddings = []
        for word in doc:
            embeddings.append(self.embedding_model[word])
        return embeddings

embedding_model = EmbeddingVectorizer()

这是错误:

TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_2152/702628572.py in <module>
     15         return embeddings
     16 
---> 17 embedding_model = EmbeddingVectorizer()

~\AppData\Local\Temp/ipykernel_2152/702628572.py in __init__(self)
      2     def __init__(self):
      3 
----> 4         self.embedding_model = fasttext.load_model(fasttext_model)
      5 
      6     def __call__(self, doc):

~\Anaconda3\envs\project-relation-skill-extraction-master-thesis\lib\site-packages\fasttext\FastText.py in load_model(path)
    439     """Load a model given a filepath and return a model object."""
    440     eprint("Warning : `load_model` does not return WordVectorModel or SupervisedModel any more, but a `FastText` object which is very similar.")
--> 441     return _FastText(model_path=path)
    442 
    443 

~\Anaconda3\envs\project-relation-skill-extraction-master-thesis\lib\site-packages\fasttext\FastText.py in __init__(self, model_path, args)
     96         self.f = fasttext.fasttext()
     97         if model_path is not None:
---> 98             self.f.loadModel(model_path)
     99         self._words = None
    100         self._labels = None

TypeError: loadModel(): incompatible function arguments. The following argument types are supported:
    1. (self: fasttext_pybind.fasttext, arg0: str) -> None

Invoked with: <fasttext_pybind.fasttext object at 0x0000016C630257B0>, WindowsPath('../models/cc.de.300.bin')

fasttext 的文档并没有给我任何可能出错的线索。有什么猜测吗?谢谢!

【问题讨论】:

  • fasttext_model 字符串在哪里定义?
  • @StefanoFiorucci-anakin87 我添加了它:)
  • 我检查了路径是否指向现有文件。
  • 也许您的问题与 Windows 有关,如此处所示 (stackoverflow.com/questions/64602373/…)

标签: python-3.x nlp fasttext


【解决方案1】:

提供一个字符串,而不是 Path 对象。

根据错误...

TypeError: loadModel(): incompatible function arguments. The following argument types are supported:
    1. (self: fasttext_pybind.fasttext, arg0: str) -> None

Invoked with: <fasttext_pybind.fasttext object at 0x0000016C630257B0>, WindowsPath('../models/cc.de.300.bin')

...arg0(第一个位置参数)应该是 str,而它看到的是 WindowsPath 对象。

只使用str(facebook_model) 而不是只使用facebook_model 作为fasttext.load_model() 的参数可能就足够了。

但是,如果您对fasttext 代码的实际指向有任何进一步的困惑,您也可以查看并尝试str(facebook_model.resolve()),这样您一定会看到绝对 em> 文件的完整路径。

【讨论】:

  • 谢谢,现在可以了!将我使用 Windows 的信息编辑到问题描述中。
猜你喜欢
  • 1970-01-01
  • 2013-08-09
  • 2011-06-19
  • 2021-12-09
  • 1970-01-01
  • 1970-01-01
  • 2016-10-15
  • 2022-01-26
  • 1970-01-01
相关资源
最近更新 更多