Repository: piskvorky/gensim

Issue:
Unable to load KeyedVectors models created with gensim==3.2.0: AttributeError: Can't get attribute 'EuclideanKeyedVectors'
<!--
If your issue is a usage or a general question, please submit it here instead:
- Mailing List: https://groups.google.com/forum/#!forum/gensim
For more information, see Recipes&FAQ: https://github.com/RaRe-Technologies/gensim/wiki/Recipes-&-FAQ
-->

<!-- Instructions For Filing a Bug: https://github.com/RaRe-Technologies/gensim/blob/develop/CONTRIBUTING.md -->

#### Description

Hi there,

I can't seem to be able to load trimmed `KeyedVectors` models created with gensim 3.2.0 with any newer version of gensim (3.3.0 - 3.6.0) because:

```
AttributeError: Can't get attribute 'EuclideanKeyedVectors' on <module 'gensim.models.keyedvectors' from '/usr/local/lib/python3.7/site-packages/gensim/models/keyedvectors.py'>
```

#### Steps/Code/Corpus to Reproduce

To replicate:

1) Create a model with gensim==3.2.0, convert it to `KeyedVectors` and then save it:

```shell
pip3.7 install -qU gensim==3.2.0
```

```python
from gensim.models import Word2Vec
from gensim.test.utils import common_texts

model = Word2Vec(common_texts, size=100, window=5, min_count=1, workers=4)
trimmed_model = model.wv
trimmed_model.save('test.word2vec')
```

2) Try loading the model with gensim>=3.2.0:

```shell
pip3.7 install -qU gensim==3.6.0	# same with 3.3.0, 3.4.0 and 3.5.0 too
```

```python
from gensim.models import KeyedVectors

word_vectors = KeyedVectors.load('test.word2vec')
```

#### Expected Results

Model should get loaded and be functional.


#### Actual Results

`KeyedVectors.load()` fails with:

```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/gensim/models/keyedvectors.py", line 212, in load
    return super(BaseKeyedVectors, cls).load(fname_or_handle, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/gensim/utils.py", line 422, in load
    obj = unpickle(fname)
  File "/usr/local/lib/python3.7/site-packages/gensim/utils.py", line 1361, in unpickle
    return _pickle.load(f, encoding='latin1')
AttributeError: Can't get attribute 'EuclideanKeyedVectors' on <module 'gensim.models.keyedvectors' from '/usr/local/lib/python3.7/site-packages/gensim/models/keyedvectors.py'>
```


#### Versions
<!--
Please run the following snippet and paste the output below.
import platform; print(platform.platform())
import sys; print("Python", sys.version)
import numpy; print("NumPy", numpy.__version__)
import scipy; print("SciPy", scipy.__version__)
import gensim; print("gensim", gensim.__version__)
from gensim.models import word2vec;print("FAST_VERSION", word2vec.FAST_VERSION)
-->

```
>>> import platform; print(platform.platform())
Darwin-18.0.0-x86_64-i386-64bit
>>> import sys; print("Python", sys.version)
Python 3.7.0 (default, Sep 18 2018, 18:47:08) 
[Clang 10.0.0 (clang-1000.10.43.1)]
>>> import numpy; print("NumPy", numpy.__version__)
NumPy 1.15.2
>>> import scipy; print("SciPy", scipy.__version__)
SciPy 1.1.0
>>> import gensim; print("gensim", gensim.__version__)
gensim 3.6.0
>>> from gensim.models import word2vec;print("FAST_VERSION", word2vec.FAST_VERSION)
FAST_VERSION 0
```


<!-- Thanks for contributing! -->


Unable to load KeyedVectors models created with gensim==3.2.0: AttributeError: Can't get attribute 'EuclideanKeyedVectors'
<!--
If your issue is a usage or a general question, please submit it here instead:
- Mailing List: https://groups.google.com/forum/#!forum/gensim
For more information, see Recipes&FAQ: https://github.com/RaRe-Technologies/gensim/wiki/Recipes-&-FAQ
-->

<!-- Instructions For Filing a Bug: https://github.com/RaRe-Technologies/gensim/blob/develop/CONTRIBUTING.md -->

#### Description

Hi there,

I can't seem to be able to load trimmed `KeyedVectors` models created with gensim 3.2.0 with any newer version of gensim (3.3.0 - 3.6.0) because:

```
AttributeError: Can't get attribute 'EuclideanKeyedVectors' on <module 'gensim.models.keyedvectors' from '/usr/local/lib/python3.7/site-packages/gensim/models/keyedvectors.py'>
```

#### Steps/Code/Corpus to Reproduce

To replicate:

1) Create a model with gensim==3.2.0, convert it to `KeyedVectors` and then save it:

```shell
pip3.7 install -qU gensim==3.2.0
```

```python
from gensim.models import Word2Vec
from gensim.test.utils import common_texts

model = Word2Vec(common_texts, size=100, window=5, min_count=1, workers=4)
trimmed_model = model.wv
trimmed_model.save('test.word2vec')
```

2) Try loading the model with gensim>=3.2.0:

```shell
pip3.7 install -qU gensim==3.6.0	# same with 3.3.0, 3.4.0 and 3.5.0 too
```

```python
from gensim.models import KeyedVectors

word_vectors = KeyedVectors.load('test.word2vec')
```

#### Expected Results

Model should get loaded and be functional.


#### Actual Results

`KeyedVectors.load()` fails with:

```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/gensim/models/keyedvectors.py", line 212, in load
    return super(BaseKeyedVectors, cls).load(fname_or_handle, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/gensim/utils.py", line 422, in load
    obj = unpickle(fname)
  File "/usr/local/lib/python3.7/site-packages/gensim/utils.py", line 1361, in unpickle
    return _pickle.load(f, encoding='latin1')
AttributeError: Can't get attribute 'EuclideanKeyedVectors' on <module 'gensim.models.keyedvectors' from '/usr/local/lib/python3.7/site-packages/gensim/models/keyedvectors.py'>
```


#### Versions
<!--
Please run the following snippet and paste the output below.
import platform; print(platform.platform())
import sys; print("Python", sys.version)
import numpy; print("NumPy", numpy.__version__)
import scipy; print("SciPy", scipy.__version__)
import gensim; print("gensim", gensim.__version__)
from gensim.models import word2vec;print("FAST_VERSION", word2vec.FAST_VERSION)
-->

```
>>> import platform; print(platform.platform())
Darwin-18.0.0-x86_64-i386-64bit
>>> import sys; print("Python", sys.version)
Python 3.7.0 (default, Sep 18 2018, 18:47:08) 
[Clang 10.0.0 (clang-1000.10.43.1)]
>>> import numpy; print("NumPy", numpy.__version__)
NumPy 1.15.2
>>> import scipy; print("SciPy", scipy.__version__)
SciPy 1.1.0
>>> import gensim; print("gensim", gensim.__version__)
gensim 3.6.0
>>> from gensim.models import word2vec;print("FAST_VERSION", word2vec.FAST_VERSION)
FAST_VERSION 0
```


<!-- Thanks for contributing! -->




Discussion:
@pypt thanks for the report: reproduced (`python2`, gensim `3.2.0` + `3.6.0`), looks like this can be related to #1777 again, argh @manneshiva.
Still reproducible with 3.7.2.
@pypt thanks for the report: reproduced (`python2`, gensim `3.2.0` + `3.6.0`), looks like this can be related to #1777 again, argh @manneshiva.
Still reproducible with 3.7.2.

Resolving patch:
diff --git a/gensim/models/keyedvectors.py b/gensim/models/keyedvectors.py
--- a/gensim/models/keyedvectors.py
+++ b/gensim/models/keyedvectors.py
@@ -186,6 +186,11 @@
 )
 from gensim.similarities.termsim import TermSimilarityIndex, SparseTermSimilarityMatrix
 
+#
+# For backwards compatibility, see https://github.com/RaRe-Technologies/gensim/issues/2201
+#
+from gensim.models.deprecated.keyedvectors import EuclideanKeyedVectors  # noqa
+
 logger = logging.getLogger(__name__)
 
 


Test patch:
diff --git a/gensim/test/test_data/old_keyedvectors_320.dat b/gensim/test/test_data/old_keyedvectors_320.dat
new file mode 100644
Binary files /dev/null and b/gensim/test/test_data/old_keyedvectors_320.dat differ
diff --git a/gensim/test/test_keyedvectors.py b/gensim/test/test_keyedvectors.py
--- a/gensim/test/test_keyedvectors.py
+++ b/gensim/test/test_keyedvectors.py
@@ -348,6 +348,13 @@ def test_identity(self):
         self.assertTrue(np.all(np.array([6, 7, 8]) == n[2]))
 
 
+class Gensim320Test(unittest.TestCase):
+    def test(self):
+        path = datapath('old_keyedvectors_320.dat')
+        vectors = gensim.models.keyedvectors.KeyedVectors.load(path)
+        self.assertTrue(vectors.word_vec('computer') is not None)
+
+
 if __name__ == '__main__':
     logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.DEBUG)
     unittest.main()