def euclidean_dist(x, y): 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.clamp(min=1e-12).sqrt() # for numerical stability return dist
def pairwise_distance(x, y): m, n = x.size(0), y.size(0) x = x.view(m, -1) y = y.view(n, -1) dist = torch.pow(x, 2).sum(1).unsqueeze(1).expand(m, n) + \ torch.pow(y, 2).sum(1).unsqueeze(1).expand(n, m).t() # dist.addmm_(1, -2, x, y.t()) dist.addmm_(x, y.t(), beta=1., alpha=-2.) return dist