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

Fixed incompatability in persistence for older versions #1723

Merged
merged 2 commits into from
Nov 20, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions gensim/models/wrappers/fasttext.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,14 @@ def load_fasttext_format(cls, model_file, encoding='utf8'):
model.load_binary_data(encoding=encoding)
return model

@classmethod
def load(cls, *args, **kwargs):
model = super(FastText, cls).load(*args, **kwargs)
if hasattr(model.wv, 'syn0_all'):
setattr(model.wv, 'syn0_ngrams', model.wv.syn0_all)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's with this setattr / delattr? Why not a simple (and more readable) model.wv.syn0_ngrams = model.wv.syn0_all?

delattr(model.wv, 'syn0_all')
return model

@classmethod
def delete_training_files(cls, model_file):
"""Deletes the files created by FastText training"""
Expand Down
Binary file added gensim/test/test_data/ft_model_2.3.0
Binary file not shown.
16 changes: 16 additions & 0 deletions gensim/test/test_fasttext_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,22 @@ def testConsistentDtype(self):
oov_embedding = self.test_model[oov_word]
self.assertEqual(vocab_embedding.dtype, oov_embedding.dtype)

def testPersistenceForOldVersions(self):
"""Test backward compatibility for models saved with versions < 3.0.0"""
old_model_path = datapath('ft_model_2.3.0')
loaded_model = fasttext.FastText.load(old_model_path)
self.assertEqual(loaded_model.vector_size, 10)
self.assertEqual(loaded_model.wv.syn0.shape[1], 10)
self.assertEqual(loaded_model.wv.syn0_ngrams.shape[1], 10)
# in-vocab word
in_expected_vec = numpy.array([-2.44566941, -1.54802394, -2.61103821, -1.88549316, 1.02860415,
Copy link
Owner

@piskvorky piskvorky Nov 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bad indentation (please use hanging indent).

1.19031894, 2.01627707, 1.98942184, -1.39095843, -0.65036952])
self.assertTrue(numpy.allclose(loaded_model["the"], in_expected_vec, atol=1e-4))
# out-of-vocab word
out_expected_vec = numpy.array([-1.34948218, -0.8686831, -1.51483142, -1.0164026, 0.56272298,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dtto

0.66228276, 1.06477463, 1.1355902, -0.80972326, -0.39845538])
self.assertTrue(numpy.allclose(loaded_model["random_word"], out_expected_vec, atol=1e-4))


if __name__ == '__main__':
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.DEBUG)
Expand Down