赞
踩
torch.cosine_similarity 可以对两个向量或者张量计算相似度
>>> input1 = torch.randn(100, 128)
>>> input2 = torch.randn(100, 128)
>>> output = torch.cosine_similarity(input1, input2, dim=1)
print(output.shape)
torch.Size([100])
hg1 = torch.FloatTensor(torch.randn([12]))
hg2 = torch.FloatTensor(torch.randn([12]))
torch.cosine_similarity(hg1, hg2, dim=0)
tensor(-0.1543)
pytorch计算欧式距离
torch.dist(hg1, hg2, p=2)
如果自己写的话就是:(因为很简单,大多数人自己写),Pytorch里封装了这个距离函数
torch.sqrt(torch.sum((hg1-hg2)**2))
Hamming 距离,汉密尔顿距离
torch.dist(hg1, hg2, p=1)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。