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/Doc2Vec offer model-minimization method Fix issue #446 #987

Merged
merged 18 commits into from
Nov 13, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 9 additions & 6 deletions gensim/models/doc2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,15 +778,18 @@ def __str__(self):
segments.append('t%d' % self.workers)
return '%s(%s)' % (self.__class__.__name__, ','.join(segments))

def discard_model_parameters(self, remove_doctags_vectors=False):
def delete_temporary_training_data(self, keep_doctags_vectors=True, keep_inference=True):
"""
Discard parameters that are used in training and score. Use if you're sure you're done training a model.
Use `remove_doctags_vectors` if you don't want to save doctags vectors.
Useful in case when you only need to use infer_vector,
but don't want to use docvecs's most_similar, similarity etc. methods.
Use `remove_doctags_vectors` if you don't want to save doctags vectors,
in this case you can't to use docvecs's most_similar, similarity etc. methods.
Use `no_inference` if you don't want to store parameters that is used for infer_vector method (you will not be able to use infer_vector)
"""
self._minimize_model(self.hs, self.negative > 0, True)
if self.docvecs and hasattr(self.docvecs, 'doctag_syn0') and remove_doctags_vectors:
if keep_inference:
self._minimize_model(self.hs, self.negative > 0, True)
else:
self._minimize_model(False, False, False)
if self.docvecs and hasattr(self.docvecs, 'doctag_syn0') and not keep_doctags_vectors:
del self.docvecs.doctag_syn0
if self.docvecs and hasattr(self.docvecs, 'doctag_syn0_lockf'):
del self.docvecs.doctag_syn0_lockf
Expand Down
2 changes: 1 addition & 1 deletion gensim/models/word2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -1761,7 +1761,7 @@ def _minimize_model(self, save_syn1 = False, save_syn1neg = False, save_syn0_loc
del self.syn0_lockf
self.model_trimmed_post_training = True

def discard_model_parameters(self, replace=False):
def delete_temporary_training_data(self, replace=False):
Copy link
Contributor

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 to replace_word_vectors_with_normalized?

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

Copy link
Contributor

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 the delete_temporary_training_data it looks strange

"""
Discard parameters that are used in training and score. Use if you're sure you're done training a model.
If `replace` is set, forget the original vectors and only keep the normalized
Expand Down
32 changes: 19 additions & 13 deletions gensim/test/test_doc2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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'))

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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):
Expand Down
10 changes: 6 additions & 4 deletions gensim/test/test_word2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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()
Expand Down