pytorch 计算相似度,相关系数

torch.cosine_similarity 可以对两个向量或者张量计算相似度

结果:可能为负数,只能判断方向,相似度精度比较低。

比如1和10000的相似度为1,1和-1的相似度为-1。

import torch

input1 = torch.randn(10, 12)
input2 = torch.randn(10, 12)
output = torch.cosine_similarity(input1, input2, dim=1)
print(output)


hg1 = torch.FloatTensor([-1])
hg2 = torch.FloatTensor([1])
value=torch.cosine_similarity(hg1, hg2, dim=0)
print(value)


pytorch计算欧式距离

torch.dist(hg1, hg2, p=2)

如果自己写的话就是:(因为很简单,大多数人自己写),Pytorch里封装了这个距离函数

torch.sqrt(torch.sum((hg1-hg2)**2))

Hamming 距离,汉密尔顿距离

torch.dist(hg1, hg2, p=1)


 

一些计算向量相似度的函数,例如Cosine, BiLinear, TriLinear, Muiltihead等
 

import torch
i

你可能感兴趣的:(pytorch知识宝典,pytorch,人工智能,python)