【问题标题】:Number of words in vocabulary gensim word2vec词汇表中的单词数 gensim word2vec
【发布时间】:2021-04-07 17:29:05
【问题描述】:

如何为 gensim Word2Vec 打印模型词汇表中的单词数?

import gensim
model = gensim.models.KeyedVectors.load_word2vec_format("GoogleNews-vectors-negative300.bin", binary=True)

我试过了

len(model.wv.vocab)

但我得到了这个错误

Traceback (most recent call last):
  File ".\word2vec.py", line 4, in <module>
    len(model.wv.vocab)
AttributeError: 'KeyedVectors' object has no attribute 'wv'

还有这个

len(model.vocab)

它给出了这个错误

Traceback (most recent call last):
  File ".\word2vec.py", line 4, in <module>
    len(model.vocab)
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\gensim\models\keyedvectors.py", line 645, in vocab
    raise AttributeError(
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.

【问题讨论】:

    标签: python gensim word2vec


    【解决方案1】:

    here 上找到更新版本

    len(model.vectors)
    

    【讨论】:

    • 更一般地说,您可以使用len(model)。一旦您的模型实际上是像 KeyedVectors 这样的数据对象(而不是像算法对象 Word2Vec,它将在其 .wv 属性中包含一个内部 KeyedVectors),您可以直接做更多的事情就可以了。在这种情况下,使用更明确的变量名可能很有用,例如kv_model。其中的单词列表在kv_model.index_to_key 中,因为每个单词都是查找向量(或其他每个键属性)的键。
    猜你喜欢
    • 2016-06-06
    • 2023-03-24
    • 2017-07-19
    • 2021-06-26
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    相关资源
    最近更新 更多