【发布时间】: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