Repository: piskvorky/gensim

Issue:
Can not open and score a Word2vec model generated in v3.8.0 in v4.2.0
#### Problem description
Hello,

I'm trying to score a sentence in a script using gensim v4.2.0, with a model trained in v3.8.0. However, I meet the error `AttributeError: 'Word2Vec' object has no attribute 'syn1'`

#### Steps/code/corpus to reproduce

Link to the model file: https://filetransfer.io/data-package/DIMOegMO#link

```python
from gensim.models import Word2vec
from gensim.utils import SaveLoad

sv = SaveLoad()
model = sv.load('test.mdl')
model.score(['test'], total_sentences=1)
```

#### Versions


```python
>>> import platform; print(platform.platform())
Linux-5.4.0-135-generic-x86_64-with-Ubuntu-18.04-bionic
>>> import sys; print("Python", sys.version)
Python 3.6.9 (default, Nov 25 2022, 14:10:45) 
[GCC 8.4.0]
>>> import struct; print("Bits", 8 * struct.calcsize("P"))
Bits 64
>>> import numpy; print("NumPy", numpy.__version__)
NumPy 1.18.3
>>> import scipy; print("SciPy", scipy.__version__)
SciPy 1.4.1
>>> import gensim; print("gensim", gensim.__version__)
gensim 4.2.0
>>> from gensim.models import word2vec;print("FAST_VERSION", word2vec.FAST_VERSION)
FAST_VERSION 1
```

Thanks a lot !



Discussion:
The way to load (and save) Gensim models is using this pattern: https://radimrehurek.com/gensim/models/word2vec.html#usage-examples

It looks like you're doing something else – could that be the source of the error?


@piskvorky 
I'm currently saving the models as said in the documentation. Loading them instead with
```python
model = Word2Vec.load("word2vec.model")
```
didn't change anything
The model is build with the following parameters:
```python
model = Word2Vec(
            window=6,
            workers=6,
            sg=0,
            hs=1,
            seed=21,
            vector_size=300,
            min_count=4,
            batch_words=1000
        )
model.build_vocab(cleaned_content, progress_per=10000)
logging.root.level = logging.ERROR  # Temporarily reduce logging verbosity
model.train(
            cleaned_content,
            total_examples=model.corpus_count,
            epochs=60,
            report_delay=1
        )
```
I see. In that case, it should work. I'll try to check later, thanks for reporting.
@piskvorky 
Thanks a lot, in order to make it easier to debug, here is a fully working example script to reproduce the bug:

```cmd
docker run -v $PWD:/app -w /app python:3.8-slim bash -c "pip install gensim==3.8.0; python -c \"from gensim.models import Word2Vec; model = Word2Vec(window=6,workers=6,sg=0,hs=1,seed=21,size=300,min_count=1,batch_words=1000); cleaned_content=['test']; model.build_vocab(cleaned_content, progress_per=10000); model.train(cleaned_content,total_examples=model.corpus_count,epochs=60,report_delay=1); model.save('test.mdl')\""

docker run -v $PWD:/app -w /app python:3.8-slim bash -c "pip install gensim==4.2.0; python -c \"from gensim.models import Word2Vec; model = Word2Vec.load('test.mdl'); model.score(['test'])\""
```

Please let me know if I can help you further

Resolving patch:
diff --git a/gensim/models/word2vec.py b/gensim/models/word2vec.py
--- a/gensim/models/word2vec.py
+++ b/gensim/models/word2vec.py
@@ -1986,9 +1986,6 @@ def _load_specials(self, *args, **kwargs):
             for a in ('hashfxn', 'layer1_size', 'seed', 'syn1neg', 'syn1'):
                 if hasattr(self.trainables, a):
                     setattr(self, a, getattr(self.trainables, a))
-            if hasattr(self, 'syn1'):
-                self.syn1 = self.syn1
-                del self.syn1
             del self.trainables
         if not hasattr(self, 'shrink_windows'):
             self.shrink_windows = True


Test patch:
diff --git a/gensim/test/test_data/model-from-gensim-3.8.0.w2v b/gensim/test/test_data/model-from-gensim-3.8.0.w2v
new file mode 100644
Binary files /dev/null and b/gensim/test/test_data/model-from-gensim-3.8.0.w2v differ
diff --git a/gensim/test/test_word2vec.py b/gensim/test/test_word2vec.py
--- a/gensim/test/test_word2vec.py
+++ b/gensim/test/test_word2vec.py
@@ -275,6 +275,13 @@ def test_persistence(self):
         self.assertTrue(np.allclose(wv.vectors, loaded_wv.vectors))
         self.assertEqual(len(wv), len(loaded_wv))
 
+    def test_persistence_backwards_compatible(self):
+        """Can we still load a model created with an older gensim version?"""
+        path = datapath('model-from-gensim-3.8.0.w2v')
+        model = word2vec.Word2Vec.load(path)
+        x = model.score(['test'])
+        assert x is not None
+
     def test_persistence_from_file(self):
         """Test storing/loading the entire model trained with corpus_file argument."""
         with temporary_file(get_tmpfile('gensim_word2vec.tst')) as corpus_file: