余弦相似度
在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)