title: Mahalanobis distance returns NaN in Pytorch
body: <p>I'm testing new metrics to measure distance between weight matrices in Pytorch, right now I'm trying to use Mahalanobis. For that I reshape every matrix into a vector and concat then into one matrix and then use this matrix to calculate the mahalanobis distance between any two rows of this matrix. Problem is some of those are getting me negative results and the square root of a negative throws me NaN.</p>
  
  <p>I know the covariance matrix has to be Positive Defined, I guess I'm messing up there, or maybe my ideia of using mahalanobis in this case is not possible? </p>
  
  <p>Here's the code I'm using, where I'm passing to it a X with shape (64,121), that being 64 (11x11) matrices</p>
  
  <pre><code>def _mahalanobis(X):
      VI = torch.inverse(cov(X)) #covariance matrix
      total_dist = 0
      for i,v in enumerate(X):
          dist = 0
          for j,u in enumerate(X):
              if i == j:
                  continue
              x = (v-u).unsqueeze(0).t()
              y = (v - u).unsqueeze(0)
  
              dist = torch.sqrt(torch.mm(torch.mm(y,VI),x))
  
              total_dist +=dist
              print(dist)
      return total_dist
  
  
  ## Returns the covariance matrix of m
      def cov(m, rowvar=False):
  
          if m.dim() &gt; 2:
              raise ValueError('m has more than 2 dimensions')
          if m.dim() &lt; 2:
              m = m.view(1, -1)
          if not rowvar and m.size(0) != 1:
              m = m.t()
          # m = m.type(torch.double)  # uncomment this line if desired
          fact = 1.0 / (m.size(1) - 1)
          m -= torch.mean(m, dim=1, keepdim=True)
          mt = m.t()  # if complex: mt = m.t().conj()
          return fact * m.matmul(mt).squeeze()
  </code></pre>