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.
Based on SciPy's implementation of the mahalanobis distance, you would do this in PyTorch. Assuming
u
andv
are 1D andcov
is the 2D covariance matrix.Note:
scipy.spatial.distance.mahalanobis
takes in the inverse of the covariance matrix.