python-计算两个矩阵的相似度。

余弦相似度
在pytorch中,有一个专门的函数用于计算相似度:torch.cosine_similarity()

https://pytorch.org/docs/stable/nn.functional.html#cosine-similarity
在这里插入图片描述

import torch
import torch.nn.functional as F
input1 = torch.randn(100, 128)
input2 = torch.randn(100, 128)
output = F.cosine_similarity(input1, input2)
print(output)
tensor([-0.0647,  0.0727, -0.1345,  0.0678, -0.0098, -0.0795,  0.0869,  0.0317,
         0.0162, -0.0012, -0.0688,  0.0857, -0.1271,  0.0013,  0.0806,  0.0332,
         0.0954,  0.0092,  0.0201,  0.0708, -0.1174, -0.0758,  0.0778, -0.0373,
         0.0948, -0.1601, -0.0242,  0.0104, -0.1832,  0.1664, -0.0563, -0.0109,
        -0.1107, -0.0043, -0.1357, -0.0793, -0.0288, -0.0144,  0.0233, -0.0632,
         0.1332, -0.0117, -0.0290,  0.1186,  0.1313,  0.0621, -0.0581,  0.0468,
        -0.1042, -0.0217, -0.0128,  0.0984, -0.0287, -0.1207, -0.0822,  0.0916,
         0.0322, -0.1047, -0.0340,  0.0739,  0.0983, -0.0360,  0.0565, -0.0245,
         0.0758,  0.0704,  0.0360,  0.0007, -0.0090,  0.0306,  0.0088, -0.0478,
        -0.0043,  0.0628,  0.0513, -0.0717,  0.0664,  0.0272,  0.0376,  0.0314,
         0.0923,  0.0866,  0.1894, -0.0680,  0.0293,  0.0464,  0.0950,  0.0656,
         0.0186, -0.0535,  0.1067, -0.0103, -0.0208, -0.1296,  0.0436, -0.0599,
        -0.0192,  0.1296,  0.0912, -0.0512])

相似度的概念是:求向量的相似度。这个函数表示对矩阵的哪个维度求相似,该dim值默认为1。
这里得到的是对应行的相似度。

在这里插入图片描述
俩图一减取平方,简单快捷。(PSNR)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值