-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Fixes issues while loading word2vec
and doc2vec
models saved using old Gensim versions. Fix #2000, #1977
#2012
Changes from 10 commits
4edf610
dd1630c
f8b4e32
e09eba7
b572679
310e652
86e2575
b71a8c4
dc54d38
fd5e5b1
89edbe7
463f491
9871304
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,6 +122,31 @@ def testLoadOldModel(self): | |
|
||
self.model_sanity(model) | ||
|
||
# load really old model | ||
model_file = 'd2v-lee-v0.13.0' | ||
model = doc2vec.Doc2Vec.load(datapath(model_file)) | ||
self.model_sanity(model) | ||
|
||
# Test loading doc2vec models from all previous versions | ||
old_versions = [ | ||
'0.12.0', '0.12.1', '0.12.2', '0.12.3', '0.12.4', | ||
'0.13.0', '0.13.1', '0.13.2', '0.13.3', '0.13.4', | ||
'1.0.0', '1.0.1', '2.0.0', '2.1.0', '2.2.0', '2.3.0', | ||
'3.0.0', '3.1.0', '3.2.0', '3.3.0', '3.4.0' | ||
] | ||
|
||
saved_models_dir = datapath('old_d2v_models') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better |
||
for old_version in old_versions: | ||
model = doc2vec.Doc2Vec.load(os.path.join(saved_models_dir, 'd2v_{}.mdl'.format(old_version))) | ||
self.assertTrue(len(model.wv.vocab) == 3) | ||
self.assertTrue(model.wv.vectors.shape == (3, 4)) | ||
self.assertTrue(model.docvecs.vectors_docs.shape == (2, 4)) | ||
self.assertTrue(model.docvecs.count == 2) | ||
# check if inferring vectors for new documents and similarity search works. | ||
doc0_inferred = model.infer_vector(list(DocsLeeCorpus())[0].words) | ||
sims_to_infer = model.docvecs.most_similar([doc0_inferred], topn=len(model.docvecs)) | ||
self.assertTrue(sims_to_infer) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add here Also, please try to update model (as for w2v) |
||
def test_unicode_in_doctag(self): | ||
"""Test storing document vectors of a model with unicode titles.""" | ||
model = doc2vec.Doc2Vec(DocsLeeCorpus(unicode_tags=True), min_count=1) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -192,15 +192,15 @@ def testOnlineLearningAfterSave(self): | |
model_neg.train(new_sentences, total_examples=model_neg.corpus_count, epochs=model_neg.iter) | ||
self.assertEqual(len(model_neg.wv.vocab), 14) | ||
|
||
def onlineSanity(self, model): | ||
def onlineSanity(self, model, trained_model=False): | ||
terro, others = [], [] | ||
for l in list_corpus: | ||
if 'terrorism' in l: | ||
terro.append(l) | ||
else: | ||
others.append(l) | ||
self.assertTrue(all(['terrorism' not in l for l in others])) | ||
model.build_vocab(others) | ||
model.build_vocab(others, update=trained_model) | ||
model.train(others, total_examples=model.corpus_count, epochs=model.iter) | ||
self.assertFalse('terrorism' in model.wv.vocab) | ||
model.build_vocab(terro, update=True) | ||
|
@@ -764,6 +764,8 @@ def testLoadOldModel(self): | |
self.assertTrue(model.trainables.vectors_lockf.shape == (12,)) | ||
self.assertTrue(model.vocabulary.cum_table.shape == (12,)) | ||
|
||
self.onlineSanity(model, trained_model=True) | ||
|
||
# Model stored in multiple files | ||
model_file = 'word2vec_old_sep' | ||
model = word2vec.Word2Vec.load(datapath(model_file)) | ||
|
@@ -774,12 +776,37 @@ def testLoadOldModel(self): | |
self.assertTrue(model.trainables.vectors_lockf.shape == (12,)) | ||
self.assertTrue(model.vocabulary.cum_table.shape == (12,)) | ||
|
||
self.onlineSanity(model, trained_model=True) | ||
|
||
# load really old model | ||
model_file = 'w2v-lee-v0.12.0' | ||
model = word2vec.Word2Vec.load(datapath(model_file)) | ||
self.onlineSanity(model, trained_model=True) | ||
|
||
# test for max_final_vocab for model saved in 3.3 | ||
model_file = 'word2vec_3.3' | ||
model = word2vec.Word2Vec.load(datapath(model_file)) | ||
self.assertEqual(model.max_final_vocab, None) | ||
self.assertEqual(model.vocabulary.max_final_vocab, None) | ||
|
||
# Test loading word2vec models from all previous versions | ||
old_versions = [ | ||
'0.12.0', '0.12.1', '0.12.2', '0.12.3', '0.12.4', | ||
'0.13.0', '0.13.1', '0.13.2', '0.13.3', '0.13.4', | ||
'1.0.0', '1.0.1', '2.0.0', '2.1.0', '2.2.0', '2.3.0', | ||
'3.0.0', '3.1.0', '3.2.0', '3.3.0', '3.4.0' | ||
] | ||
|
||
saved_models_dir = datapath('old_w2v_models') | ||
for old_version in old_versions: | ||
model = word2vec.Word2Vec.load(os.path.join(saved_models_dir, 'w2v_{}.mdl'.format(old_version))) | ||
self.assertTrue(len(model.wv.vocab) == 3) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add |
||
self.assertTrue(model.wv.vectors.shape == (3, 4)) | ||
# check if similarity search and online training works. | ||
self.assertTrue(len(model.wv.most_similar('sentence')) == 2) | ||
model.build_vocab(list_corpus, update=True) | ||
model.train(list_corpus, total_examples=model.corpus_count, epochs=model.iter) | ||
|
||
@log_capture() | ||
def testBuildVocabWarning(self, l): | ||
"""Test if warning is raised on non-ideal input to a word2vec model""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Magic: definitely deserves a comment.