如何计算pytorch中一个矩阵中所有行相对于另一个矩阵中所有行的cosine_similarity [英] How to compute the cosine_similarity in pytorch for all rows in a matrix with respect to all rows in another matrix

查看:32
本文介绍了如何计算pytorch中一个矩阵中所有行相对于另一个矩阵中所有行的cosine_similarity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 pytorch 中,鉴于我有 2 个矩阵,我将如何计算每个矩阵中所有行与另一个矩阵中所有行的余弦相似度.

In pytorch, given that I have 2 matrixes how would I compute cosine similarity of all rows in each with all rows in the other.

例如

给定输入 =

matrix_1 = [a b] 
           [c d] 
matrix_2 = [e f] 
           [g h]

我希望输出为

输出=

 [cosine_sim([a b] [e f])  cosine_sim([a b] [g h])]
 [cosine_sim([c d] [e f])  cosine_sim([c d] [g h])] 

目前我正在使用 torch.nn.functional.cosine_similarity(matrix_1, matrix_2) 它返回该行的余弦值,只有另一个矩阵中的对应行.

At the moment I am using torch.nn.functional.cosine_similarity(matrix_1, matrix_2) which returns the cosine of the row with only that corresponding row in the other matrix.

在我的示例中,我只有 2 行,但我想要一个适用于多行的解决方案.我什至想处理每个矩阵中行数不同的情况.

In my example I have only 2 rows, but I would like a solution which works for many rows. I would even like to handle the case where the number of rows in the each matrix is different.

我意识到我可以使用扩展,但是我想在不使用如此大的内存占用的情况下做到这一点.

I realize that I could use the expand, however I want to do it without using such a large memory footprint.

推荐答案

通过手动计算相似度并玩转矩阵乘法+转置:

By manually computing the similarity and playing with matrix multiplication + transposition:

import torch
from scipy import spatial
import numpy as np

a = torch.randn(2, 2)
b = torch.randn(3, 2) # different row number, for the fun

# Given that cos_sim(u, v) = dot(u, v) / (norm(u) * norm(v))
#                          = dot(u / norm(u), v / norm(v))
# We fist normalize the rows, before computing their dot products via transposition:
a_norm = a / a.norm(dim=1)[:, None]
b_norm = b / b.norm(dim=1)[:, None]
res = torch.mm(a_norm, b_norm.transpose(0,1))
print(res)
#  0.9978 -0.9986 -0.9985
# -0.8629  0.9172  0.9172

# -------
# Let's verify with numpy/scipy if our computations are correct:
a_n = a.numpy()
b_n = b.numpy()
res_n = np.zeros((2, 3))
for i in range(2):
    for j in range(3):
        # cos_sim(u, v) = 1 - cos_dist(u, v)
        res_n[i, j] = 1 - spatial.distance.cosine(a_n[i], b_n[j])
print(res_n)
# [[ 0.9978022  -0.99855876 -0.99854881]
#  [-0.86285472  0.91716063  0.9172349 ]]

这篇关于如何计算pytorch中一个矩阵中所有行相对于另一个矩阵中所有行的cosine_similarity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆