PairwiseDistance#
- class torch.nn.PairwiseDistance(p=2.0, eps=1e-06, keepdim=False)[source]#
计算输入向量之间的成对距离,或输入矩阵列之间的成对距离。
距离是使用
p
-范数计算的,并加上常数eps
以避免除以零,如果p
为负值,即:其中 是全1向量,
p
-范数定义如下:- 参数
- 形状
输入1: 或 ,其中 N = 批次维度,D = 向量维度
输入2: 或 ,形状与输入1相同
输出: 或 ,具体取决于输入维度。如果
keepdim
为True
,则输出为 或 ,具体取决于输入维度。
示例
>>> pdist = nn.PairwiseDistance(p=2) >>> input1 = torch.randn(100, 128) >>> input2 = torch.randn(100, 128) >>> output = pdist(input1, input2)