【问题标题】:Gensim - how to deal with model word::tagGensim - 如何处理模型 word::tag
【发布时间】:2018-06-12 20:05:08
【问题描述】:

我正在尝试使用 gensim 加载预训练的 word2vec 模型。虽然模型是有标签的,所以每个词都有一个标签,标签告诉这个词代表的是什么词性。

例如:

big::adj 0.041660 0.045049 -0.204449 0.102298 0.045326 -0.172079 0.197417 -0.012363 0.127003 0.040437 -0.003397 0.048288 0.072291 0.044205 -0.055407 -0.075357 -0.154024 0.021732 0.224021 -0.243452 -0.048776 -0.002823 0.110283 -0.052014 0.104335 -0.108122 -0.033678 -0.098096 -0.012307 0.086673 -0.028013 0.005308 -0.196080 0.002180 -0.004461 0.021646 -0.051721 -0.123485 -0.230521 0.106092 -0.206776 0.137945 0.020572 0.071123 0.042434 0.123633 -0.001925 -0.172347 -0.040973 0.135886 0.057297 -0.027319 0.066697 0.138673 -0.028331 -0.094053 -0.160371 0.158397 0.053368 -0.002126 -0.111501 0.030450 -0.054284 -0.004832 -0.065144 0.030546 -0.011896 -0.103835 -0.007947 0.120997 0.178889 -0.155029 -0.054059 -0.313675 0.061776 -0.060536 0.038848 -0.097532 -0.038358 -0.032634 0.108534 0.067584 0.044829 0.003414 0.028115 -0.010523 0.131776 0.071750 0.045095 0.046262 0.001212 -0.005994 -0.022401 -0.036971 -0.024755 0.096701 -0.026736 -0.029698 -0.107293 -0.038610

谁能指出我,如何加载这样的模型,所以我可以要求模型['big']?现在,当我尝试 KeyedVectors.load() 时,它只是不起作用。

【问题讨论】:

  • 你的模型是从哪里来的?我猜你必须在尝试为它们获取向量之前标记单词,所以你必须model['big::adj']而不是model['big']

标签: load gensim corpus


【解决方案1】:

如您的示例数据所示,您必须在使用这些词之前给它们一个词性标签。来自您的向量中包含的自述文件 (opis.txt):

Tzn że w samym modelu znajdują się słowa zapisane w następujący sposób:
    lemmat::pos np. pszczoła::noun

谷歌翻译:

That is, in the model itself there are words written in the following way:
    lemma :: pos eg. bee :: noun

自述文件还提供了有效词性列表。

因为词向量是这样训练的,没有词性的词没有有效的向量。

如果出于某种原因您想要一个没有词性的向量,您可以遍历所有可能的词性并取向量的平均值,如下所示:

vectors = pretrained_vectors
word = 'bee'
parts_of_speech = ['noun', 'verb', etc...]
vec = zero_vector
pos_count = 0
for part in parts_of_speech:
    key = word + '::' + part
    if key in vectors:
        vec += vectors[word + '::' + part]
        pos_count += 1

vec = vec / pos_count
# if actually using code like this, use numpy and watch for zero division

也就是说,我怀疑生成的向量可能存在问题。更好的选择是找到不使用词性的预训练向量,例如those provided with Fasttext

【讨论】:

    猜你喜欢
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-29
    相关资源
    最近更新 更多