【问题标题】:DeprecationWarning: Call to deprecated `__getitem__`弃用警告:调用已弃用的`__getitem__`
【发布时间】:2020-04-20 20:21:30
【问题描述】:

我已经尝试解决这个错误几天了,但我无法找到原因,我尝试将 cod 从 embedding_matrix[ i ] = model[vocab[i]] 更改为 embedding_matrix[ i ] = model.wv[vocab[i]] 在这种情况下我没有得到弃用的错误,但我仍然得到该行的错误(25),有人可以告诉我有什么问题

  from gensim.models import Word2Vec
import re

vocab = []
for word in tokenizer.word_index:
    vocab.append( word )

def tokenize( sentences ):
    tokens_list = []
    vocabulary = []
    for sentence in sentences:
        sentence = sentence.lower()
        sentence = re.sub( '[^a-zA-Z]', ' ', sentence )
        tokens = sentence.split()
        vocabulary += tokens
        tokens_list.append( tokens )
    return tokens_list , vocabulary

p = tokenize( questions + answers )
model = Word2Vec( p[ 0 ] ) 

embedding_matrix = np.zeros( ( VOCAB_SIZE , 100 ) )
for i in range( len( tokenizer.word_index ) ):
    embedding_matrix[ i ] = model[vocab[i]]

# encoder_input_data
tokenized_questions = tokenizer.texts_to_sequences( questions )
maxlen_questions = max( [ len(x) for x in tokenized_questions ] )
padded_questions = preprocessing.sequence.pad_sequences( tokenized_questions , maxlen=maxlen_questions , padding='post' )
encoder_input_data = np.array( padded_questions )
print( encoder_input_data.shape , maxlen_questions )

我收到以下错误:

 /usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:25: DeprecationWarning: Call to deprecated `__getitem__` (Method will be removed in 4.0.0, use self.wv.__getitem__() instead).
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-11-29183abd3d2d> in <module>()
     23 embedding_matrix = np.zeros( ( VOCAB_SIZE , 100 ) )
     24 for i in range( len( tokenizer.word_index ) ):
---> 25     embedding_matrix[ i ] = model[vocab[i]]
     26 
     27 # encoder_input_data

提前谢谢你!

【问题讨论】:

    标签: artificial-intelligence lstm google-colaboratory chatbot seq2seq


    【解决方案1】:

    直接模型查看器在新的 gensim 版本中被贬低了,而是使用了辅助方法 wv,这无疑是这个错误。 根据文档https://radimrehurek.com/gensim/models/word2vec.html,训练后的词向量存储在 KeyedVectors 实例中,作为 model.wv。 更改第 25 行的代码:

    embedding_matrix[ i ] = model.wv[vocab[i]]

    【讨论】:

    • 欢迎。感谢您的贡献。由于 SO 上的低质量,不鼓励仅代码响应。请考虑编辑以添加解释如何/为什么解决 OP 的问题,甚至是文档链接。请注意,随着时间的推移,大多数支持来自高质量的答案,因为各种用户从您的帖子中学到了一些东西,以应用于他们自己的编码问题。
    • 谢谢!添加描述和有用信息
    猜你喜欢
    • 1970-01-01
    • 2020-12-06
    • 2015-03-23
    • 2019-07-09
    • 1970-01-01
    • 2021-02-19
    相关资源
    最近更新 更多