python - 无法重新创建用于训练 FastText 的 Gensim 文档。类型错误 : Either one of corpus_file or corpus_iterable value must be provided

标签 python nlp gensim fasttext

我正在尝试制作自己的 Fasttext 嵌入,所以我去了官方 Gensim 文档和 implemented this exact code below与精确 4.0版本。

from gensim.models import FastText
from gensim.test.utils import common_texts

model = FastText(vector_size=4, window=3, min_count=1)  # instantiate
model.build_vocab(sentences=common_texts)
model.train(sentences=common_texts, total_examples=len(common_texts), epochs=10)
令我惊讶的是,它给了我以下错误:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-6b2d1de02d90> in <module>
      1 model = FastText(vector_size=4, window=3, min_count=1)  # instantiate
----> 2 model.build_vocab(sentences=common_texts)
      3 model.train(sentences=common_texts, total_examples=len(common_texts), epochs=10)

~/anaconda3/lib/python3.8/site-packages/gensim/models/word2vec.py in build_vocab(self, corpus_iterable, corpus_file, update, progress_per, keep_raw_vocab, trim_rule, **kwargs)
    477 
    478         """
--> 479         self._check_corpus_sanity(corpus_iterable=corpus_iterable, corpus_file=corpus_file, passes=1)
    480         total_words, corpus_count = self.scan_vocab(
    481             corpus_iterable=corpus_iterable, corpus_file=corpus_file, progress_per=progress_per, trim_rule=trim_rule)

~/anaconda3/lib/python3.8/site-packages/gensim/models/word2vec.py in _check_corpus_sanity(self, corpus_iterable, corpus_file, passes)
   1484         """Checks whether the corpus parameters make sense."""
   1485         if corpus_file is None and corpus_iterable is None:
-> 1486             raise TypeError("Either one of corpus_file or corpus_iterable value must be provided")
   1487         if corpus_file is not None and corpus_iterable is not None:
   1488             raise TypeError("Both corpus_file and corpus_iterable must not be provided at the same time")

TypeError: Either one of corpus_file or corpus_iterable value must be provided
有人可以帮助这里发生的事情吗?

最佳答案

所以我找到了这个问题的答案。他们的论点 sentence 有问题同时:

model.build_vocab(sentences=common_texts)
model.train(sentences=common_texts, total_examples=len(common_texts), epochs=10)
您所要做的就是删除参数名称 或者简单地传递第一个参数,即 corpus_iterable
model.build_vocab(common_texts)
model.train(common_texts, total_examples=len(common_texts), epochs=10)
或者
model.build_vocab(corpus_iterable=common_texts)
model.train(corpus_iterable=common_texts, total_examples=len(common_texts), epochs=10)

关于python - 无法重新创建用于训练 FastText 的 Gensim 文档。类型错误 : Either one of corpus_file or corpus_iterable value must be provided,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67573416/

相关文章:

python-3.x - Gensim build_vocab 耗时过长

python - 将其中包含数字的文件分组和划分到单独的文件夹中

python - 分配数组的所有项目,除了给定索引的项目

python - pyOpt for python 3安装报错

java - 如何使用 WordNet (Java) 从不同单词中提取常见的上位词?

python - 值错误: cannot compute LDA over an empty collection (no terms)

python - 两个连续的 yield 语句如何在 python 中工作?

machine-learning - 如何使用 MLE 来训练 n-gram 模型?

python - count() 和 concordance() 给出不同的计数

python - 属性错误 : module 'gensim.utils' has no attribute 'smart_open'