Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Word2vec update before train error message. Fix #1162 #1205

Merged
merged 3 commits into from
Mar 13, 2017
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions gensim/models/word2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,13 @@ def update_weights(self):
for i in xrange(len(self.wv.syn0), len(self.wv.vocab)):
# construct deterministic seed from word AND seed argument
newsyn0[i-len(self.wv.syn0)] = self.seeded_vector(self.wv.index2word[i] + str(self.seed))

# Raise an error if an online update is run before initial training on a corpus
if not len(self.wv.syn0):
raise RuntimeError("You can do an online update of vocabulary on a pre-trained model. " \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please start the message by stating the cause of the warning: "The model has not yet been trained."

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note it isn't necessary any training has happened - just that no initial/prior vocabulary-discovery has happened (and the update=True case assumes that it has). So perhaps instead: "Cannot update vocabulary of model which has no prior vocabulary."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the error message as,

"You cannot do an online vocabulary-update of a model which has no prior vocabulary. First build the vocabulary of your model with a corpus before doing an online update."

@gojomo, it does make sense to flag the error earlier than waiting till update_weights is called. I made a few changes on another file locally ( Note that this change is not currently reflected on github. It is local to me.) and tested the following code change.

    def build_vocab(self, sentences, keep_raw_vocab=False, trim_rule=None, progress_per=10000, update=False):
        """
        Build vocabulary from a sequence of sentences (can be a once-only generator stream).
        Each sentence must be a list of unicode strings.

        """
        if update:
            if not len(self.wv.vocab):
                raise RuntimeError("You cannot do an online vocabulary-update of a model which has no prior vocabulary." \
                "First build the vocabulary of your model with a corpus " \
                "before doing an online update.")        
        self.scan_vocab(sentences, progress_per=progress_per, trim_rule=trim_rule)  # initial survey
        self.scale_vocab(keep_raw_vocab=keep_raw_vocab, trim_rule=trim_rule, update=update)  # trim by min_count & precalculate downsampling
        self.finalize_vocab(update=update)  # build tables & arrays

It works well with the cases that I have mentioned earlier. I also tested this,

import gensim
from nltk.corpus import brown, movie_reviews, treebank
b_sents = brown.sents()
b = gensim.models.Word2Vec(b_sents)
b.build_vocab(b_sents, keep_raw_vocab=False, trim_rule=None, progress_per=10000, update=True)
b.train(b_sents)

Executing the will not display any error message.

"Or first build the vocabulary of your model with a corpus and train it " \
"before doing an online update.")

self.wv.syn0 = vstack([self.wv.syn0, newsyn0])

if self.hs:
Expand Down