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

Make negative ns_exponent work correctly #3250

Merged
merged 8 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion gensim/models/word2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def __init__(

self.hs = int(hs)
self.negative = int(negative)
self.ns_exponent = ns_exponent
self.ns_exponent = float(ns_exponent)
self.cbow_mean = int(cbow_mean)
self.compute_loss = bool(compute_loss)
self.running_training_loss = 0
Expand Down Expand Up @@ -1947,6 +1947,7 @@ def _load_specials(self, *args, **kwargs):
# for backward compatibility, add/rearrange properties from prior versions
if not hasattr(self, 'ns_exponent'):
self.ns_exponent = 0.75
self.ns_exponent = float(self.ns_exponent)
if self.negative and hasattr(self.wv, 'index_to_key'):
self.make_cum_table() # rebuild cum_table from vocabulary
if not hasattr(self, 'corpus_count'):
Expand Down
9 changes: 9 additions & 0 deletions gensim/test/test_doc2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,15 @@ def test_train_warning(self, loglines):
def test_load_on_class_error(self):
"""Test if exception is raised when loading doc2vec model on instance"""
self.assertRaises(AttributeError, load_on_instance)

def test_negative_ns_exp(self):
# We expect that model should train, save, load and continue training without any exceptions
model = doc2vec.Doc2Vec(sentences, ns_exponent=-1, min_count=1, workers=1)
tmpf = get_tmpfile('d2v_negative_exp.tst')
model.save(tmpf)
loaded_model = doc2vec.Doc2Vec.load(tmpf)
loaded_model.train(sentences, total_examples=model.corpus_count, epochs=1)
assert loaded_model.ns_exponent == -1, loaded_model.ns_exponent
# endclass TestDoc2VecModel


Expand Down
9 changes: 9 additions & 0 deletions gensim/test/test_fasttext.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,15 @@ def test_vectors_for_all_without_inference(self):
predicted = vectors_for_all['responding']
assert np.allclose(expected, predicted)

def test_negative_ns_exp(self):
# We expect that model should train, save, load and continue training without any exceptions
model = FT_gensim(sentences, ns_exponent=-1, min_count=1, workers=1)
tmpf = get_tmpfile('fasttext_negative_exp.tst')
model.save(tmpf)
loaded_model = FT_gensim.load(tmpf)
loaded_model.train(sentences, total_examples=model.corpus_count, epochs=1)
assert loaded_model.ns_exponent == -1, loaded_model.ns_exponent


@pytest.mark.parametrize('shrink_windows', [True, False])
def test_cbow_hs_training(shrink_windows):
Expand Down
9 changes: 9 additions & 0 deletions gensim/test/test_word2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,15 @@ def test_compute_training_loss(self):
training_loss_val = model.get_latest_training_loss()
self.assertTrue(training_loss_val > 0.0)

def test_negative_ns_exp(self):
# We expect that model should train, save, load and continue training without any exceptions
model = word2vec.Word2Vec(sentences, ns_exponent=-1, min_count=1, workers=1)
tmpf = get_tmpfile('w2v_negative_exp.tst')
model.save(tmpf)
loaded_model = word2vec.Word2Vec.load(tmpf)
loaded_model.train(sentences, total_examples=model.corpus_count, epochs=1)
assert loaded_model.ns_exponent == -1, loaded_model.ns_exponent


# endclass TestWord2VecModel

Expand Down