# Pytorch相似度计算

1 余弦相似度

import torch
import torch.nn as nn
import math
class ConineSimilarity(nn.Module):

def forward(self, tensor_1,tensor_2):
normalized_tensor_1=tensor_1/tensor_1.norm(dim=-1,keepdim=True)
normalized_tensor_2=tensor_2/tensor_2.norm(dim=-1,keepdim=True)
return (normalized_tensor_1*normalized_tensor_2).sum(dim=-1)

print(input_1)
print(input_2)
con=ConineSimilarity()
CS=con(input_1,input_2)
CS2=torch.cosine_similarity(input_1,input_2)
print(CS)
print(CS2)


tensor([[ 0.5389,  1.7485, -0.5183,  1.5721, -0.3880],
[-1.3460,  1.8793, -1.9676,  0.8554, -2.6176],
[-0.5190, -0.4626, -1.5462,  0.5101, -0.3836]], requires_grad=True)
tensor([[-0.3151,  0.0500, -0.2004, -0.8795,  0.2460],
[ 0.1276,  1.0000,  0.6290, -0.0722, -0.1960],
[-0.6387, -0.7833, -0.1992,  0.3575,  0.7043]], requires_grad=True)



2 点乘相似度

class DotProductSimilarity(nn.Module):
def __init__(self,scale_output=False):
super(DotProductSimilarity,self).__init__()
self.scale_output=scale_output
def forward(self,tensor_1,tensor_2):
result=(tensor_1*tensor_2).sum(dim=-1)
if(self.scale_output):
result/=math.sqrt(tensor_1.size(-1))
return  result
print(input_1)
print(input_2)
con=ConineSimilarity()
CS=con(input_1,input_2)
print(CS)
dot=DotProductSimilarity()
DS=dot(input_1,input_2)
print(DS)


tensor([[ 0.2148,  0.2645,  0.8717, -1.4637,  1.0932],
[-0.2975, -0.1585,  0.1567, -1.8238, -0.1437],
[-0.8243, -0.0170, -0.7533, -1.7643,  2.2571]], requires_grad=True)
tensor([[-1.8424, -1.4204, -1.5639,  0.2739,  0.7922],
[-0.3378, -2.3861, -1.7581, -0.8220,  0.3456],
[ 0.5536,  0.8324, -1.0632, -1.5567,  0.6374]], requires_grad=True)


3 双线性相似度

b=x^T W y + b

class BiLinearSimilarity(nn.Module):
def __init__(self,tensor_1_dim,tensor_2_dim,activation=None):
super(BiLinearSimilarity,self).__init__()
self.weight_matrix=nn.Parameter(torch.Tensor(tensor_1_dim,tensor_2_dim))
self.bias=nn.Parameter(torch.Tensor(1))
self.activation=activation
self.reset_parameters()
def reset_parameters(self):
nn.init.xavier_uniform_(self.weight_matrix)
self.bias.data.fill_(0)
def forward(self, tensor_1,tensor_2):
intermediate=torch.matmul(tensor_1,self.weight_matrix)
result=(intermediate*tensor_2).sum(dim=-1)+self.bias
if self.activation is not None:
result=self.activation(result)
return result
print(input_1)
print(input_2)
con=ConineSimilarity()
CS=con(input_1,input_2)
print(CS)
dot=DotProductSimilarity()
DS=dot(input_1,input_2)
print(DS)
bilinear=BiLinearSimilarity(5,5)
BS=bilinear(input_1,input_2)
print(BS)
bilinear=BiLinearSimilarity(5,5)
BS=bilinear(input_1,input_2)
print(BS)


tensor([[ 0.1465, -0.8367,  0.5901, -0.7421,  0.7775],
[ 0.2198,  1.5482,  1.5923,  1.0962, -1.6559],
[-1.8222, -0.7740, -0.4819, -1.8031,  1.6413]], requires_grad=True)
tensor([[ 0.0053,  2.1358,  0.9435,  0.2379,  0.0689],
[-0.3503,  1.3089,  0.5033, -2.2339,  1.7092],
[-0.0255,  1.0277,  1.4797,  0.2870, -0.4866]], requires_grad=True)


4 欧式距离

input_1=torch.randn(3,5,requires_grad=True)
print(input_1)
print(input_2)
con=ConineSimilarity()
CS=con(input_1,input_2)
print(CS)
dot=DotProductSimilarity()
DS=dot(input_1,input_2)
print(DS)
bilinear=BiLinearSimilarity(5,5)
BS=bilinear(input_1,input_2)
print(BS)
bilinear=BiLinearSimilarity(5,5)
BS=bilinear(input_1,input_2)
print(BS)
import torch.nn.functional as F
e=F.pairwise_distance(input_1,input_2)
print(e)


tensor([[-1.0758,  1.2539, -1.4611, -0.5173, -0.3719],
[-0.3430,  1.0673,  0.3724,  0.2630, -0.0029],
[ 0.0570,  0.9101,  0.3809,  0.9970,  0.1919]], requires_grad=True)
tensor([[ 0.0319, -0.7283, -1.2269,  0.0922,  1.3412],
[-1.9286,  0.4859, -1.2347,  2.1949, -0.3295],
[-0.3811,  0.1348, -0.4247, -1.1917,  0.6632]], requires_grad=True)


5 皮尔逊相关系数

class PearsonCorrelation(nn.Module):
def forward(self,tensor_1,tensor_2):
x = tensor_1
y = tensor_2

vx = x - torch.mean(x)
vy = y - torch.mean(y)

cost = torch.sum(vx * vy) / (torch.sqrt(torch.sum(vx ** 2)) * torch.sqrt(torch.sum(vy ** 2)))
return cost
print(input_1)
print(input_2)
pearson=PearsonCorrelation()
PC=pearson(input_1,input_2)
print(PC)


tensor([[-0.3064,  0.5541,  0.5944,  1.2654,  0.0281],
[ 1.0192, -0.8178,  0.2624, -1.4695,  0.3750],
[-1.7883,  0.5552, -0.1381, -0.6302,  0.6420]], requires_grad=True)
tensor([[ 0.8128, -0.3573, -0.8618,  1.2499,  0.5793],
[ 1.7729,  0.1705, -1.6051, -2.5970,  1.2015],
[-0.4328,  0.4294,  0.7364,  1.7886,  0.4203]], requires_grad=True)


09-10 3397

07-12 4816

01-08 3029

07-11 2217

09-25 494

08-12 405

12-07 156

03-30 202

03-16 238

06-27 1万+

02-19 7788

11-29 3万+

12-17 4834

06-01 364

04-04 173

03-10 94

12-14 2659

03-19 80万+

04-14 56万+

02-19 16万+

02-27 7万+

02-28 4万+

03-01 12万+

03-01 11万+

03-03 6205

03-04 12万+

03-05 9763

03-05 5万+

03-08 6万+

03-08 1万+

04-25 6万+

03-10 12万+

03-10 17万+

03-10 6385

03-12 10万+

03-13 10万+

03-16 1万+

03-19 7万+

03-20 5086

03-23 1万+

03-24 3万+

03-25 2万+

03-25 8万+

03-27 1万+

03-29 20万+

03-29 9万+

03-30 14万+

05-21 2938

03-31 1万+

05-25 5231

03-23 1万+

04-02 3万+

05-06 2万+

04-05 1万+

04-06 6万+

04-09 1万+

04-09 7万+

04-09 4724

04-10 4273

04-11 2万+

04-15 5万+

04-18 1万+

04-18 3801

04-18 4万+

04-20 3万+

04-24 2万+

04-26 3681

04-24 4154

04-30 7539

05-16 4万+

05-08 3万+

05-10 1927

05-11 3万+

05-13 6769

05-19 5696

05-13 9774

05-14 3678

05-14 6149

05-16 1957

#### 如何用Python提高办公（Excel）效率？

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客