Repository: piskvorky/gensim

Issue:
AttributeError: 'DocvecsArray' object has no attribute 'vector_size'
I have Doc2Vec models that were trained and saved before the Re-design "*2vec" implementations #1777 commit (specifically, they were trained in v3.2) . On trying to use them today, the above attribute error was raised when I tried to use `infer_vector`. This is because in gensim/models/doc2vec.py", line 543, in infer_vector the line:

```doctag_vectors, doctag_locks = self.trainables.get_doctag_trainables(doc_words, self.docvecs.vector_size)```

Attempts to access `self.docvecs.vector_size`, which before version 3.4.0 is not an attribute of the DocVecsArray class.

#### Steps to Reproduce
```
from gensim.models import Doc2Vec

model = Doc2Vec.load('some-3.2-model/model')
model.infer_vector(['foo'])

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-13-851ff3602fcf> in <module>()
----> 1 model.infer_vector(['foo'])

~/anaconda2/envs/faker/lib/python3.6/site-packages/gensim/models/doc2vec.py in infer_vector(self, doc_words, alpha, min_alpha, steps)
    541 
    542         """
--> 543         doctag_vectors, doctag_locks = self.trainables.get_doctag_trainables(doc_words, self.docvecs.vector_size)
    544         doctag_indexes = [0]
    545         work = zeros(self.trainables.layer1_size, dtype=REAL)

AttributeError: 'DocvecsArray' object has no attribute 'vector_size'
```
AttributeError: 'DocvecsArray' object has no attribute 'vector_size'
I have Doc2Vec models that were trained and saved before the Re-design "*2vec" implementations #1777 commit (specifically, they were trained in v3.2) . On trying to use them today, the above attribute error was raised when I tried to use `infer_vector`. This is because in gensim/models/doc2vec.py", line 543, in infer_vector the line:

```doctag_vectors, doctag_locks = self.trainables.get_doctag_trainables(doc_words, self.docvecs.vector_size)```

Attempts to access `self.docvecs.vector_size`, which before version 3.4.0 is not an attribute of the DocVecsArray class.

#### Steps to Reproduce
```
from gensim.models import Doc2Vec

model = Doc2Vec.load('some-3.2-model/model')
model.infer_vector(['foo'])

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-13-851ff3602fcf> in <module>()
----> 1 model.infer_vector(['foo'])

~/anaconda2/envs/faker/lib/python3.6/site-packages/gensim/models/doc2vec.py in infer_vector(self, doc_words, alpha, min_alpha, steps)
    541 
    542         """
--> 543         doctag_vectors, doctag_locks = self.trainables.get_doctag_trainables(doc_words, self.docvecs.vector_size)
    544         doctag_indexes = [0]
    545         work = zeros(self.trainables.layer1_size, dtype=REAL)

AttributeError: 'DocvecsArray' object has no attribute 'vector_size'
```


Discussion:
@JonathanHourany thanks for the report, yes, this looks like a bug. 

Can you share your model for reproducing a problem? 

CC: @manneshiva please fix it ASAP

@menshikh-iv I wish I could, but the models are for a client that won't grant approval. If it would help though, I could train a model on a small open corpus on my rolled back v3.1. install like the lee corpus and post that some time today or tomorrow
@JonathanHourany yes, this is a great idea!
@JonathanHourany thanks for the report, yes, this looks like a bug. 

Can you share your model for reproducing a problem? 

CC: @manneshiva please fix it ASAP

@menshikh-iv I wish I could, but the models are for a client that won't grant approval. If it would help though, I could train a model on a small open corpus on my rolled back v3.1. install like the lee corpus and post that some time today or tomorrow
@JonathanHourany yes, this is a great idea!

Resolving patch:
diff --git a/gensim/models/deprecated/doc2vec.py b/gensim/models/deprecated/doc2vec.py
--- a/gensim/models/deprecated/doc2vec.py
+++ b/gensim/models/deprecated/doc2vec.py
@@ -88,7 +88,6 @@ def load_old_doc2vec(*args, **kwargs):
         'dbow_words': old_model.dbow_words,
         'dm_concat': old_model.dm_concat,
         'dm_tag_count': old_model.dm_tag_count,
-        'docvecs': old_model.__dict__.get('docvecs', None),
         'docvecs_mapfile': old_model.__dict__.get('docvecs_mapfile', None),
         'comment': old_model.__dict__.get('comment', None),
         'size': old_model.vector_size,
diff --git a/gensim/models/keyedvectors.py b/gensim/models/keyedvectors.py
--- a/gensim/models/keyedvectors.py
+++ b/gensim/models/keyedvectors.py
@@ -1514,7 +1514,7 @@ def index_to_doctag(self, i_index):
         """Return string key for given i_index, if available. Otherwise return raw int doctag (same int)."""
         candidate_offset = i_index - self.max_rawint - 1
         if 0 <= candidate_offset < len(self.offset2doctag):
-            return self.ffset2doctag[candidate_offset]
+            return self.offset2doctag[candidate_offset]
         else:
             return i_index
 


Test patch:
diff --git a/gensim/test/test_doc2vec.py b/gensim/test/test_doc2vec.py
--- a/gensim/test/test_doc2vec.py
+++ b/gensim/test/test_doc2vec.py
@@ -103,6 +103,8 @@ def testLoadOldModel(self):
         self.assertTrue(model.docvecs.max_rawint == 299)
         self.assertTrue(model.docvecs.count == 300)
 
+        self.model_sanity(model)
+
         # Model stored in multiple files
         model_file = 'doc2vec_old_sep'
         model = doc2vec.Doc2Vec.load(datapath(model_file))
@@ -118,6 +120,8 @@ def testLoadOldModel(self):
         self.assertTrue(model.docvecs.max_rawint == 299)
         self.assertTrue(model.docvecs.count == 300)
 
+        self.model_sanity(model)
+
     def test_unicode_in_doctag(self):
         """Test storing document vectors of a model with unicode titles."""
         model = doc2vec.Doc2Vec(DocsLeeCorpus(unicode_tags=True), min_count=1)