泡菜装载:ImportError:没有名为doc2vec_ext的模块
这是我要处理的结构:
src/
processing/
station_level/
train_paragraph_vectors.py
doc2vec_ext.py
word_embeddings_station_level.py
我已经像这样训练并在word_embeddings_station_level.py
中存储了一个模型:
I have trained and stored a model in word_embeddings_station_level.py
like this:
from src.doc2vec_ext import WeightedDoc2Vec
# ...
model = WeightedDoc2Vec(
# ...
)
train(model, vocab, station_sentences, num_epochs)
# Saving the model -> pickles it
model.save(open(model_file, "w"))
到目前为止,它运行良好.但是,我想像这样在train_paragraph_vectors.py
中加载该模型:
This is working fine so far. However, I want to load that model in train_paragraph_vectors.py
like this:
import sys
from src import doc2vec_ext
sys.modules["doc2vec_ext"] = doc2vec_ext
if __name__ == "__main__":
# ...
model = doc2vec_ext.WeightedDoc2Vec.load(station_level_sentence_vectors)
但是我得到了:
Traceback (most recent call last):
File "E:/python/kaggle/seizure_prediction/src/processing/station_level/train_paragraph_vectors.py", line 57, in <module>
model = doc2vec_ext.WeightedDoc2Vec.load(station_level_sentence_vectors)
File "C:\Python27\lib\site-packages\gensim\models\word2vec.py", line 1684, in load
model = super(Word2Vec, cls).load(*args, **kwargs)
File "C:\Python27\lib\site-packages\gensim\utils.py", line 248, in load
obj = unpickle(fname)
File "C:\Python27\lib\site-packages\gensim\utils.py", line 911, in unpickle
return _pickle.loads(f.read())
ImportError: No module named doc2vec_ext
doc2vec_ext.py
在这里您可以看到,我只是从gensim.models.Doc2Vec
类继承而来,做了一些事情:
Here you can see, that I just inherit from the gensim.models.Doc2Vec
class and do some stuff:
class WeightedDoc2Vec(Doc2Vec):
def __init__(self, dm=1,window=5, f_size=0, size=100, min_count=1, negative=0, dbow_words=1, alpha=0.015, workers=8, seed=42, dm_weighted=False, dm_stacked=False):
Doc2Vec.__init__(self,
# Constructor arguments ..
)
# ...
我不知道这是什么问题.我尝试做sys.modules[]
,但是它仍然无法正常工作.
I don't know what's the problem here. I've tried to do the sys.modules[]
but it's still not working properly.
如何加载存储的模型?
重要提示:
我注意到我什至无法从同一模块加载.如果我尝试将模型加载到创建模型的文件中(此处为word_embeddings_station_level.py
),它仍然无法正常工作,给我同样的错误.
I noticed that I can't even load from the same module. If I try to load the model in the file where it was created (here word_embeddings_station_level.py
) it's still not working giving me the same error.
当像泡菜一样保存和加载二进制数据时,您需要使用二进制模式,而不是文本模式.
When saving and loading binary data like a pickle, you need to use binary mode, not text mode.
model.save(open(model_file, "wb")) # 'b' for binary