如何让 word2vec 加载字符串?问题:'dict'对象没有属性'_load_specials'

How do I get word2vec to load a string? problem:'dict' object has no attribute '_load_specials'

我在使用word2vec和lstm的时候遇到了问题,代码是:

def input_transform(string):
    words=jieba.lcut(string)
    words=np.array(words).reshape(1,-1)
    model=Word2Vec.load('lstm_datamodel.pkl')
    combined=create_dictionaries(model,words)
    return combined

def lstm_predict(string):
    print ('loading model......')
    with open('lstm_data.yml', 'r') as f:
        yaml_string = yaml.load(f)
    model = model_from_yaml(yaml_string)

    print ('loading weights......')
    model.load_weights('lstm_data.h5')
    model.compile(loss='binary_crossentropy',
              optimizer='adam',metrics=['accuracy'])
    data=input_transform(string)
    data.reshape(1,-1)
    #print data
    result=model.predict_classes(data)
    if result[0][0]==1:
        print (string,' positive')
    else:
        print (string,' negative')

错误是:

Traceback (most recent call last):
File "C:\Python36\lib\site-packages\gensim\models\word2vec.py", line 1312, in load
model = super(Word2Vec, cls).load(*args, **kwargs)
File "C:\Python36\lib\site-packages\gensim\models\base_any2vec.py", line 1244, in load
model = super(BaseWordEmbeddingsModel, cls).load(*args, **kwargs)
File "C:\Python36\lib\site-packages\gensim\models\base_any2vec.py", line 603, in load
return super(BaseAny2VecModel, cls).load(fname_or_handle, **kwargs)
File "C:\Python36\lib\site-packages\gensim\utils.py", line 423, in load
obj._load_specials(fname, mmap, compress, subname)
AttributeError: 'dict' object has no attribute '_load_specials'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:/GitHub/reviewsentiment/veclstm.py", line 211, in <module>
lstm_predict(string)
File "C:/GitHub/reviewsentiment/veclstm.py", line 191, in lstm_predict
data=input_transform(string)
File "C:/GitHub/reviewsentiment/veclstm.py", line 177, in input_transform
model=Word2Vec.load('lstm_datamodel.pkl')
File "C:\Python36\lib\site-packages\gensim\models\word2vec.py", line 1323, in load
return load_old_word2vec(*args, **kwargs)
File "C:\Python36\lib\site-packages\gensim\models\deprecated\word2vec.py", line 153, in load_old_word2vec
old_model = Word2Vec.load(*args, **kwargs)
File "C:\Python36\lib\site-packages\gensim\models\deprecated\word2vec.py", line 1618, in load
model = super(Word2Vec, cls).load(*args, **kwargs)
File "C:\Python36\lib\site-packages\gensim\models\deprecated\old_saveload.py", line 88, in load
obj._load_specials(fname, mmap, compress, subname)
AttributeError: 'dict' object has no attribute '_load_specials'enter code here

很抱歉包含了这么多代码。

这是我第一次在Whosebug上提问,我已经尽力自己寻找答案,但没有成功。那你能帮我吗?非常感谢!

线路上出现错误...

model=Word2Vec.load('lstm_datamodel.pkl')

...所以您提供的所有 other/later 代码都是无关紧要和多余的。

您的文件名后缀 lstm_datamodel.pkl 表明它可能是通过 Python 的 pickle() 工具创建的。 gensim Word2Vec.load() 方法只期望加载由模块自己的 save() 例程保存的模型, 而不是 任何 pickled 对象。

gensim native save() 确实使用了 pickle 进行部分保存,但不是全部,因此不会期望提供的文件中有完全腌制的对象。

这可能是您遇到问题的原因。您可以尝试完全基于 Python pickle:

的负载
model = pickle.load('lstm_datamodel.pkl')

或者,如果您可以在文件中重建模型,但一定要通过本机 gensim model.save(filename) 保存它,这也可能会解决问题。