我有一个python类特性器,检查是否存在(Google新闻向量嵌入文件),如果存在则加载,否则抛出错误消息。问题是当我试图加载它时,我得到一个关于attributeerror的错误,我不知道要改变什么。
尝试加载文件嵌入
from gensim.models import KeyedVectors
from gensim import models
word2vec_path = 'D:mt 111QuestionAnswerGoogleNews-vectors-negative300.bin'
这个类检查嵌入文件
是否存在。class Featurizer:
def __init__(self, embedding_file):
if not os.path.exists(embedding_file):
raise IOError("Embeddings file does not exist: %s" %embedding_file)
punctuation = string.punctuation
punctuation = punctuation + "’" + "“" + "?" + "‘"
self.punctuation = punctuation
print('INFO: Loading word vectors...')
self.word2vec = KeyedVectors.load_word2vec_format(
embedding_file,
binary=True)
print('INFO: Done! Using %s word vectors from pre-trained word2vec.'
%len(self.word2vec.vocab))
当使用类特性器加载嵌入时
featurizer = Featurizer(word2vec_path)
返回一个关于
类的错误---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
d:mt 111QuestionAnswertraining_model.ipynb Cell 13' in <cell line: 1>()
----> 1 featurizer = Featurizer(word2vec_path)
d:mt 111QuestionAnswertraining_model.ipynb Cell 4' in Featurizer.__init__(self, embedding_file)
11 print('INFO: Loading word vectors...')
12 self.word2vec = KeyedVectors.load_word2vec_format(
13 'GoogleNews-vectors-negative300.bin',
14 binary=True)
16 print('INFO: Done! Using %s word vectors from pre-trained word2vec.'
---> 17 %len(self.word2vec.vocab))
File d:mt 111QuestionAnswervenvlibsite-packagesgensimmodelskeyedvectors.py:735, in KeyedVectors.vocab(self)
733 @property
734 def vocab(self):
--> 735 raise AttributeError(
736 "The vocab attribute was removed from KeyedVector in Gensim 4.0.0.n"
737 "Use KeyedVector's .key_to_index dict, .index_to_key list, and methods "
738 ".get_vecattr(key, attr) and .set_vecattr(key, attr, new_val) instead.n"
739 "See https://github.com/RaRe-Technologies/gensim/wiki/Migrating-from-Gensim-3.x-to-4"
740 )
AttributeError: The vocab attribute was removed from KeyedVector in Gensim 4.0.0.
Use KeyedVector's .key_to_index dict, .index_to_key list, and methods .get_vecattr(key, attr) and .set_vecattr(key, attr, new_val) instead.
可能是什么问题,我如何纠正它。
在你的class init
中,embedding_file
值是一个字符串,但你在初始化你的类时给了它另一种数据类型(键值向量),所以不是这样:
featurizer = Featurizer(w2v_model)
你应该这样使用SMTH:
featurizer = Featurizer(word2vec_path)