def euclidean_dist(x, y):#用于计算欧氏距离
"""
Args:
x: pytorch Variable, with shape [m, d]
y: pytorch Variable, with shape [n, d]
Returns:
dist: pytorch Variable, with shape [m, n]
"""
m, n = x.size(0), y.size(0)
xx = torch.pow(x, 2).sum(1, keepdim=True).expand(m, n)
yy = torch.pow(y, 2).sum(1, keepdim=True).expand(n, m).t()
dist = xx + yy
dist.addmm_(1, -2, x, y.t())#dist = dist - 2*(x @ y.t())
dist = dist.clamp(min=1e-12).sqrt() # for numerical stability
return dist
欧氏距离的代码使用与公式的分析()
最新推荐文章于 2024-09-01 21:08:00 发布