【问题标题】:increasing efficiency of cosine simarlity提高余弦相似度的效率
【发布时间】:2021-12-04 11:18:42
【问题描述】:

所以我试图在一个有 60000 行的中等大小的文件中找到类似的句子。现在为了实现这一点,我首先使用谷歌通用句子编码器创建了每一行的句子编码。然后我用它来比较余弦相似度并找到相似的句子

module_url = "https://tfhub.dev/google/universal-sentence-encoder/4" 
model = hub.load(module_url)

sentence_embeddings = model(sentences)


def cosine(u, v):
    return numpy.dot(u, v) / (numpy.linalg.norm(u) * numpy.linalg.norm(v))

for idx,query in list(enumerate(sentences)):
    for idx2,sente in enumerate(sentences):
        if idx1 == idx2:
            continu
        sim = cosine(sentence_embeddings[idx], sentence_embeddings[idx2])
        if sim >= .80:
            # store in output

因此,使用余弦相似度的60000^2 运算,我的设备需要几天时间才能执行此代码。有没有办法让我更快地做到这一点。我知道这可能是我使用 python 所能达到的最快速度,所以如果解决方案涉及使用其他语言,我也愿意接受它

非常感谢

【问题讨论】:

    标签: python numpy nlp sentence-similarity


    【解决方案1】:

    我有 2 个想法可能会对您有所帮助。

    1st:尝试使用 c++ 做同样的事情:使用 python 生成编码,然后将它们传输到 c++ 程序,这样可以更快地完成操作。

    2nd :尝试使用近似值。你可以看看 LSH (Localitysensitive hashing) 看看这个https://towardsdatascience.com/understanding-locality-sensitive-hashing-49f6d1f6134

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-11
      • 2020-08-12
      • 2011-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      • 2013-05-24
      相关资源
      最近更新 更多