-
-
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
Fix datatype parameter for KeyedVectors.load_word2vec_format
. Fix #1682
#1819
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
b923043
load vector with high precision
pushpankar 35a8f8a
Merge branch 'develop' into fix_load_wv
pushpankar aaa7c2a
Test changes
pushpankar a8f44c5
Fix flake8 error
pushpankar 37b39f4
Fix path error
pushpankar 8e095d7
Merge branch 'develop' into fix_load_wv
pushpankar 310690d
Reformat code
pushpankar de98f2e
Fix precision loss issue for binary word2vec
pushpankar 805daf6
Fix precision loss during saving model in text format
pushpankar 466f37f
Fix binary file loading issue
pushpankar a76aec6
Merge branch 'develop' of https://github.com/RaRe-Technologies/gensim…
pushpankar 049fb91
Merge branch 'develop' of https://github.com/RaRe-Technologies/gensim…
pushpankar c157d79
Test other datatypes as well.
pushpankar 164cf63
Merge branch 'develop' of https://github.com/RaRe-Technologies/gensim…
pushpankar 991bcb6
Test type conversion
pushpankar 0904460
Fix build error
pushpankar 96d8aa5
Use better names
pushpankar 17f6b39
Merge branch 'develop' of https://github.com/RaRe-Technologies/gensim…
pushpankar 6f53175
Test type after conversion
pushpankar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
2 2 | ||
kangaroo.n.01 -0.0007369244245224787 -8.269973595356034e-05 | ||
horse.n.01 -0.0008546282343595379 0.0007694142576316829 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html | ||
|
||
""" | ||
Automated tests for checking various matutils functions. | ||
""" | ||
|
||
import logging | ||
import unittest | ||
|
||
import numpy as np | ||
|
||
from gensim.test.utils import datapath | ||
from gensim.models.keyedvectors import KeyedVectors | ||
|
||
|
||
class TestDataType(unittest.TestCase): | ||
def load_model(self, datatype): | ||
path = datapath('high_precision.kv.txt') | ||
kv = KeyedVectors.load_word2vec_format(path, binary=False, | ||
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. Hanging indent please (not vertical). 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. I guess you are talking about line 22-23. I have merged them. |
||
datatype=datatype) | ||
return kv | ||
|
||
def test_high_precision(self): | ||
kv = self.load_model(np.float64) | ||
self.assertAlmostEqual(kv['horse.n.01'][0], -0.0008546282343595379) | ||
self.assertEqual(kv['horse.n.01'][0].dtype, np.float64) | ||
|
||
def test_medium_precision(self): | ||
kv = self.load_model(np.float32) | ||
self.assertAlmostEqual(kv['horse.n.01'][0], -0.00085462822) | ||
self.assertEqual(kv['horse.n.01'][0].dtype, np.float32) | ||
|
||
def test_low_precision(self): | ||
kv = self.load_model(np.float16) | ||
self.assertAlmostEqual(kv['horse.n.01'][0], -0.00085449) | ||
self.assertEqual(kv['horse.n.01'][0].dtype, np.float16) | ||
|
||
def test_type_conversion(self): | ||
path = datapath('high_precision.kv.txt') | ||
binary_path = datapath('high_precision.kv.bin') | ||
model1 = KeyedVectors.load_word2vec_format(path, datatype=np.float16) | ||
model1.save_word2vec_format(binary_path, binary=True) | ||
model2 = KeyedVectors.load_word2vec_format(binary_path, datatype=np.float64, binary=True) | ||
self.assertAlmostEqual(model1["horse.n.01"][0], np.float16(model2["horse.n.01"][0])) | ||
self.assertEqual(model1["horse.n.01"][0].dtype, np.float16) | ||
self.assertEqual(model2["horse.n.01"][0].dtype, np.float64) | ||
|
||
|
||
if __name__ == '__main__': | ||
logging.root.setLevel(logging.WARNING) | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Please add a file header, like in the other test files.