how to calculate mahalanobis distance in pytorch?
What is the most efficient way to calculate the mahalanobis distance: in pytorch?


Based on SciPy's implementation of the mahalanobis distance, you would do this in PyTorch. Assuming u and v are 1D and cov is the 2D covariance matrix.
def mahalanobis(u, v, cov):
    delta = u - v
    m = torch.dot(delta, torch.matmul(torch.inverse(cov), delta))
    return torch.sqrt(m)

Note: scipy.spatial.distance.mahalanobis takes in the inverse of the covariance matrix.
