Pytorch torch.norm、torch.cosine_similarityコサイン類似度は、ベクトルまたはテンソル、ユークリッド距離で計算されます



Pytorch Torch Norm Torch



torch.cosine_similarity2つのベクトルがまたは類似性テンソル

>>> input1 = torch.randn(100、128)
>>> input2 = torch.randn(100、128)
>>>出力= 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))



ハミング距離、ハミング距離から

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