【问题标题】:Unable to recreate Gensim docs for training FastText. TypeError: Either one of corpus_file or corpus_iterable value must be provided无法重新创建用于训练 FastText 的 Gensim 文档。 TypeError:必须提供 corpus_file 或 corpus_iterable 值之一
【发布时间】:2021-05-17 16:13:30
【问题描述】:

我正在尝试制作自己的 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

有人可以帮助这里发生的事情吗?

【问题讨论】:

    标签: python nlp gensim fasttext


    【解决方案1】:

    所以我找到了答案。他们在两个参数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)
    

    【讨论】:

    • 这在 Gensim 4.0.0 及更高版本中发生了变化:参数名称不再是 sentences,而是 corpus_iterable(如果您提供的 Python 序列可以迭代多次,如list 或流式从磁盘迭代,如提供的示例LineSentence) 或corpus_file(如果您已经准备好您的语料库作为单个文件,每行一个文本,空格分隔令牌)。
    • 我实际上构建了自己的课程,而且,我使用的是给定的文档,所以我认为必须更改文档。
    • 他们需要更新他们的文档!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 2018-11-27
    • 2022-01-16
    相关资源
    最近更新 更多