PairwiseDistance¶
- class torch.nn.PairwiseDistance(p=2.0, eps=1e-06, keepdim=False)[source][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)