-
-
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
Word2Vec/Doc2Vec offer model-minimization method Fix issue #446 #987
Changes from 1 commit
2e9d2a5
a2efb8c
26e6042
ba8c8c4
51a64ba
c730984
a7cd9ba
a8cb0e7
18ca26f
a258241
9acf119
66fe5e3
85891f3
4395b75
06c6028
aa3942a
5f96aa0
84f174e
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 |
---|---|---|
|
@@ -280,23 +280,29 @@ def models_equal(self, model, model2): | |
self.assertEqual(len(model.docvecs.offset2doctag), len(model2.docvecs.offset2doctag)) | ||
self.assertTrue(np.allclose(model.docvecs.doctag_syn0, model2.docvecs.doctag_syn0)) | ||
|
||
def test_discard_model_parameters(self): | ||
"""Test doc2vec model after discard_model_parameters""" | ||
def test_delete_temporary_training_data(self): | ||
"""Test doc2vec model after delete_temporary_training_data""" | ||
for i in [0, 1]: | ||
for j in [0, 1]: | ||
model = doc2vec.Doc2Vec(sentences, size=5, min_count=1, hs=i, negative=j) | ||
model.discard_model_parameters(remove_doctags_vectors=True) | ||
if i == 0 and j == 0: | ||
continue | ||
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. we can actually do hs and negative sampling... |
||
model = doc2vec.Doc2Vec(sentences, size=5, min_count=1, window=4, hs=i, negative=j) | ||
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 asserts that it has all the attributes that are about to be deleted |
||
model.delete_temporary_training_data(keep_doctags_vectors=False, keep_inference=False) | ||
self.assertTrue(len(model['human']), 10) | ||
self.assertTrue(model.vocab['graph'].count, 5) | ||
if (i == 1): | ||
self.assertTrue(hasattr(model, 'syn1')) | ||
else: | ||
self.assertTrue(not hasattr(model, 'syn1')) | ||
if (j == 1): | ||
self.assertTrue(hasattr(model, 'syn1neg')) | ||
else: | ||
self.assertTrue(not hasattr(model, 'syn1neg')) | ||
self.assertTrue(hasattr(model, 'syn0_lockf')) | ||
self.assertTrue(not hasattr(model, 'syn1')) | ||
self.assertTrue(not hasattr(model, 'syn1neg')) | ||
self.assertTrue(not hasattr(model, 'syn0_lockf')) | ||
self.assertTrue(model.docvecs and not hasattr(model.docvecs, 'doctag_syn0')) | ||
self.assertTrue(model.docvecs and not hasattr(model.docvecs, 'doctag_syn0_lockf')) | ||
model = doc2vec.Doc2Vec(list_corpus, dm=1, dm_mean=1, size=24, window=4, hs=1, negative=0, alpha=0.05, min_count=2, iter=20) | ||
model.delete_temporary_training_data(keep_doctags_vectors=True, keep_inference=True) | ||
self.assertTrue(model.docvecs and hasattr(model.docvecs, 'doctag_syn0')) | ||
self.assertTrue(hasattr(model, 'syn1')) | ||
self.model_sanity(model) | ||
model = doc2vec.Doc2Vec(list_corpus, dm=1, dm_mean=1, size=24, window=4, hs=0, negative=1, alpha=0.05, min_count=2, iter=20) | ||
model.delete_temporary_training_data(keep_doctags_vectors=True, keep_inference=True) | ||
self.assertTrue(hasattr(model, 'syn1neg')) | ||
|
||
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. Seems I "sync' in git without "commit", when I added self.docvecs, 'doctag_syn0' checks :) will fix it |
||
@log_capture() | ||
def testBuildVocabWarning(self, l): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -482,22 +482,24 @@ def models_equal(self, model, model2): | |
most_common_word = max(model.vocab.items(), key=lambda item: item[1].count)[0] | ||
self.assertTrue(numpy.allclose(model[most_common_word], model2[most_common_word])) | ||
|
||
def testDiscardModelParameters(self): | ||
"""Test word2vec model after discard_model_parameters""" | ||
def testDeleteTemporaryTrainingData(self): | ||
"""Test word2vec model after delete_temporary_training_data""" | ||
for i in [0, 1]: | ||
for j in [0, 1]: | ||
model = word2vec.Word2Vec(sentences, size=10, min_count=0, seed=42, hs=i, negative=j) | ||
model.discard_model_parameters(replace=True) | ||
model.delete_temporary_training_data(replace=True) | ||
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 assert that it has the attributes that are about to get deleted |
||
self.assertTrue(len(model['human']), 10) | ||
self.assertTrue(len(model.vocab), 12) | ||
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. Please tests that necessary attributes are indeed deleted |
||
self.assertTrue(model.vocab['graph'].count, 3) | ||
self.assertTrue(not hasattr(model, 'syn1')) | ||
self.assertTrue(not hasattr(model, 'syn1neg')) | ||
self.assertTrue(not hasattr(model, 'syn0_lockf')) | ||
|
||
def testNormalizeAfterTrainingData(self): | ||
model = word2vec.Word2Vec(sentences, min_count=1) | ||
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. this is a separate test. |
||
model.save_word2vec_format(testfile(), binary=True) | ||
norm_only_model = word2vec.Word2Vec.load_word2vec_format(testfile(), binary=True) | ||
norm_only_model.discard_model_parameters(replace=True) | ||
norm_only_model.delete_temporary_training_data(replace=True) | ||
self.assertFalse(numpy.allclose(model['human'], norm_only_model['human'])) | ||
|
||
@log_capture() | ||
|
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.
Can we rename the
replace
parameter toreplace_word_vectors_with_normalized
?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.
I called the parameter this way because we have init_sims(replace=False), with the parameter of the same idea. Should we rename parameter of init_sims to?
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.
in the
init_sims
context it is self-explanatory. But in thedelete_temporary_training_data
it looks strange