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

Undo the hash2index optimization #2370

Merged
merged 10 commits into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from 7 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
31 changes: 20 additions & 11 deletions gensim/models/fasttext.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,31 +690,37 @@ def build_vocab(self, sentences=None, corpus_file=None, update=False, progress_p
>>> model.train(sentences_2, total_examples=model.corpus_count, epochs=model.epochs)

"""
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.")
if not update:
self.wv.init_ngrams_weights(self.trainables.seed)
elif 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 "
mpenkov marked this conversation as resolved.
Show resolved Hide resolved
"by calling the :meth:`gensim.models.fasttext.FastText.build_vocab` method "
mpenkov marked this conversation as resolved.
Show resolved Hide resolved
"before doing an online update."
)
else:
self.vocabulary.old_vocab_len = len(self.wv.vocab)
self.trainables.old_hash2index_len = len(self.wv.hash2index)

return super(FastText, self).build_vocab(
retval = super(FastText, self).build_vocab(
sentences=sentences, corpus_file=corpus_file, update=update, progress_per=progress_per,
keep_raw_vocab=keep_raw_vocab, trim_rule=trim_rule, **kwargs)

if update:
self.wv.update_ngrams_weights(self.trainables.seed, self.vocabulary.old_vocab_len)

return retval

def _set_train_params(self, **kwargs):
#
# We need the wv.buckets_word member to be initialized in order to
# continue training. The _clear_post_train method destroys this
# variable, so we reinitialize it here, if needed.
#
# The .old_vocab_len and .old_hash2index_len members are set only to
# keep the init_ngrams_weights method happy.
# The .old_vocab_len member is set only to keep the init_ngrams_weights method happy.
#
if self.wv.buckets_word is None:
self.vocabulary.old_vocab_len = len(self.wv.vocab)
self.trainables.old_hash2index_len = len(self.wv.hash2index)
self.trainables.init_ngrams_weights(self.wv, update=True, vocabulary=self.vocabulary)

def _clear_post_train(self):
Expand Down Expand Up @@ -1068,6 +1074,9 @@ def load(cls, *args, **kwargs):
"""
try:
model = super(FastText, cls).load(*args, **kwargs)
if hasattr(model.wv, 'hash2index'):
gensim.models.keyedvectors._rollback_optimization(model.wv)

if not hasattr(model.trainables, 'vectors_vocab_lockf') and hasattr(model.wv, 'vectors_vocab'):
model.trainables.vectors_vocab_lockf = ones(model.wv.vectors_vocab.shape, dtype=REAL)
if not hasattr(model.trainables, 'vectors_ngrams_lockf') and hasattr(model.wv, 'vectors_ngrams'):
Expand Down
Loading