Skip to content

torch.potrf fails on a positive definite matrix #199

@bamos

Description

@bamos

Hi, I could potentially be overlooking something simple here, but I think I'm hitting a rare edge case in torch.potrf. I've temporarily uploaded a 100x100 Tensor here that is positive definite. Numpy can compute the Cholesky on it, but Torch throws an error. As a hacky workaround I use torch.Tensor(np.linalg.cholesky(X.cpu().numpy())).type_as(X) if the factorization in Torch fails.

Source

#!/usr/bin/env python3

import numpy as np
import numpy.random as npr
import torch

with open('X.pt', 'rb') as f: X = torch.load(f)
X_np = X.numpy()

print('=== X is positive-definite, the minimum eigenvalue is:',
      np.min(np.linalg.eigvals(X_np)))

print('\n\n=== Cholesky with np:')
print(np.linalg.cholesky(X_np))

print('\n\n=== Cholesky with Torch:')
print(torch.potrf(X))

Output

=== X is positive-definite, the minimum eigenvalue is: 1.94934e-05


=== Cholesky with np:
[[  8.96033478   0.           0.         ...,   0.           0.           0.        ]
 [  0.12424465   7.53945875   0.         ...,   0.           0.           0.        ]
 [  2.70835567   0.68308622   7.09788656 ...,   0.           0.           0.        ]
 ...,
 [ -5.82428885   7.08884764 -10.74139786 ...,   0.26134527   0.           0.        ]
 [  0.87781757   1.51691735  -2.78839183 ...,   0.1106414    0.07375667
    0.        ]
 [  0.40712687   4.66132307  -4.44510603 ...,   0.2638523    0.01533402
    0.04457394]]


=== Cholesky with Torch:
Traceback (most recent call last):
  File "./t2.py", line 17, in <module>
    print(torch.potrf(X))
RuntimeError: Lapack Error in potrf : the leading minor of order 100 is not positive definite at /home/bamos/src/pytorch/torch/lib/TH/generic/THTensorLapack.c:567

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions