【问题标题】:Gensim sort_by_descending_frequency changes most_similar resultsGensim sort_by_descending_frequency 变化 most_similar 结果
【发布时间】:2021-07-20 08:40:25
【问题描述】:

似乎在检索最相似的词向量时,按词频排序会改变Gensim中的结果。

排序前:

from gensim.models import FastText
from gensim.test.utils import common_texts  # some example sentences
print(len(common_texts))
model = FastText(vector_size=4, window=3, min_count=1)  # instantiate
model.build_vocab(corpus_iterable=common_texts)
model.train(corpus_iterable=common_texts, total_examples=len(common_texts), epochs=1)  

model.wv.most_similar(positive=["human"])
[('interface', 0.7432922720909119),
 ('minors', 0.6719315052032471),
 ('time', 0.3513716757297516),
 ('computer', 0.05815044790506363),
 ('response', -0.11714297533035278),
 ('graph', -0.15643596649169922),
 ('eps', -0.2679084539413452),
 ('survey', -0.34035828709602356),
 ('trees', -0.63677978515625),
 ('user', -0.6500451564788818)]

但是,如果我按频率降序对向量进行排序:

model.wv.sort_by_descending_frequency()

model.wv.most_similar(positive=["human"])
[('minors', 0.9638221263885498),
 ('time', 0.6335864067077637),
 ('interface', 0.40014874935150146),
 ('computer', 0.03224882856011391),
 ('response', -0.14850640296936035),
 ('graph', -0.2249641716480255),
 ('survey', -0.26847705245018005),
 ('user', -0.45202943682670593),
 ('eps', -0.497650682926178),
 ('trees', -0.6367797255516052)]

最相似的单词排名以及单词相似度都会发生变化。知道为什么吗?

更新:

在调用排序之前:

model.wv.index_to_key
['system',
 'graph',
 'trees',
 'user',
 'minors',
 'eps',
 'time',
 'response',
 'survey',
 'computer',
 'interface',
 'human']
model.wv.expandos['count']

数组([4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2])

调用排序后:

model.wv.index_to_key
['system',
 'user',
 'trees',
 'graph',
 'human',
 'interface',
 'computer',
 'survey',
 'response',
 'time',
 'eps',
 'minors']
model.wv.expandos['count']

数组([4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2])

【问题讨论】:

    标签: python nlp gensim word2vec fasttext


    【解决方案1】:

    报告的相似性变化绝对不应该发生,所以这里肯定出了问题。 (也许,缓存的子字信息没有重新排序。)

    但还要注意:

    • 该方法并不是特别适合在训练后使用 - 实际上,如果以这种方式使用,您应该会看到一条警告消息。
    • 在词汇发现阶段结束时,在所有 2Vec 算法中默认情况下应该已经发生这种排序 - 这是通常的行为,只是很少关闭。因此,再次请求它最多应该是无操作的。

    要深入了解可能出现的问题,您能否编辑您的问题以显示两者的值……

    • model.wv.index_to_key
    • model.wv.expandos['count']

    …之前. sort_by_descending_frequency()调用之后?

    【讨论】:

    • 谢谢@gojomo。我已经更新了我的问题。
    • 谢谢!更新表明重新排序不是“稳定的” - 并列的单词,并且按照特定顺序,在重新排序后可能处于不同的顺序。这对您来说可能是问题的一部分,但是单词对的 sim 值的更改肯定也是其他一些错误。不过,目前的解决方法是:不要拨打这个不必要的电话。你的话已经按频率排序了。我还将在相关的 Github 错误上添加一些 cmets:github.com/RaRe-Technologies/gensim/issues/3196
    猜你喜欢
    • 1970-01-01
    • 2018-07-21
    • 2019-07-01
    • 2022-01-19
    • 1970-01-01
    • 2018-06-16
    • 2018-09-12
    • 1970-01-01
    • 2021-03-27
    相关资源
    最近更新 更多