0

after train the model, I use infer_vector() to get the vector successfully. but after I save the model and load again, error appears as follows:

print "infer:", model.infer_vector(sents[0]).tolist()
File "/Users/zhangweimin/anaconda/lib/python2.7/site-packages/gensim/models/doc2vec.py", line 752, in infer_vector
    doctag_vectors=doctag_vectors, doctag_locks=doctag_locks)
File "gensim/models/doc2vec_inner.pyx", line 426, in gensim.models.doc2vec_inner.train_document_dm (./gensim/models/doc2vec_inner.c:5401)
TypeError: object of type 'NoneType' has no len()

the whole code is:`

model = train_d2v(labeled_docs, model_file, word_file, 3)

# OK
print "before infer:", model.infer_vector(sents[0]).tolist()

model = Doc2Vec.load(model_file)

print "sents[0]:", sents[0]
print "type:", type(model)
print "infer:", model.infer_vector(sents[0]).tolist() #ERROR`
cs95
  • 379,657
  • 97
  • 704
  • 746
Zafedom
  • 1
  • 1
  • 2
    What's your code for saving the model? – cs95 Jun 18 '17 at 09:35
  • What does `train_d2v()` do? Are you using the most recent version of `gensim` – gojomo Jun 18 '17 at 19:54
  • thx the version of my gensim is 1.0.0. I print gensim.__version__ get it. and I use model.save() to save model. – Zafedom Jun 19 '17 at 05:47
  • def train_d2v(docs, model_file, word_file, epoch_cnt = 10, if_shuffle=True, learn_rate = 0.025, dec_lr = 0.002): model = Doc2Vec(alpha=learn_rate, size=100, window=8, min_count=5, workers=4) log_info("build vocab...") model.build_vocab(docs) for epoch in range(epoch_cnt): model.train(docs) model.alpha -= dec_lr model.min_alpha = model.alpha # fix the learning rate, no decay model.save(model_file) return model – Zafedom Jun 19 '17 at 05:49
  • It may be best t edit the question to add information, so the formatting is correct. What do the earlier lines of your code print, before the error is shown? – gojomo Jun 19 '17 at 13:38
  • 2
    Thanks. When I update the gensim to version 2.1.0, it works and save model correctly. The old version is 1.0.0. Thank you very much. – Zafedom Jun 20 '17 at 01:52

1 Answers1

0

As per comments above, using a more-recent version of gensim (2.1.0) resolved the issue.

gojomo
  • 52,260
  • 14
  • 86
  • 115