Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to recreate Gensim docs for training FastText. TypeError: Either one of corpus_file or corpus_iterable value must be provided

I am trying to make my own Fasttext embeddings so I went to official Gensim documentation and implemented this exact code below with exact 4.0 version.

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)

And to my surprise it is giving me errors as:

---------------------------------------------------------------------------
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

Can someone please help what is happening here?

like image 245
Deshwal Avatar asked Dec 23 '22 15:12

Deshwal


1 Answers

So I found the answer to this. They have a problem with the argument sentence in both:

model.build_vocab(sentences=common_texts)
model.train(sentences=common_texts, total_examples=len(common_texts), epochs=10)

All you have to do is to remove the argument name or simply pass the first argument which is corpus_iterable

model.build_vocab(common_texts)
model.train(common_texts, total_examples=len(common_texts), epochs=10)

OR

model.build_vocab(corpus_iterable=common_texts)
model.train(corpus_iterable=common_texts, total_examples=len(common_texts), epochs=10)
like image 86
Deshwal Avatar answered Dec 28 '22 11:12

Deshwal