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
uandvare 1D andcovis the 2D covariance matrix.Note:
scipy.spatial.distance.mahalanobistakes in the inverse of the covariance matrix.