-
Notifications
You must be signed in to change notification settings - Fork 24.5k
Doc bug about torch.linalg.inv #77954
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Yup. We'd accept a fix for this. |
In addition, there are some mistakes in the details. In // Validates input shapes for operations on batches of square matrices (inverse, cholesky, symeig, eig)
static inline void squareCheckInputs(const Tensor& self, const char* const f_name) {
TORCH_CHECK(self.dim() >= 2, f_name, ": The input tensor must have at least 2 dimensions.");
TORCH_CHECK(self.size(-1) == self.size(-2),
f_name,
": A must be batches of square matrices, "
"but they are ", self.size(-1), " by ", self.size(-2), " matrices");
} And in def squareCheckInputs(self, f_name):
assert self.dim() >= 2, f"{f_name}: The input tensor must have at least 2 dimensions."
# TODO: I think the error message has the -2 and -1 swapped. If you fix
# it fix the C++ squareCheckInputs too
assert self.size(-1) == self.size(-2), \
f"{f_name}: A must be batches of square matrices, but they are {self.size(-1)} by {self.size(-2)} matrices" Maybe they should be {self.size(-2)} by {self.size(-1)}. |
You'd better judge singular matrices, otherwise this operator will calculate incorrectly. Example: Python 3.9.7 (default, Sep 16 2021, 13:09:58)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> A = torch.tensor([[4.1, 2.8], [9.676, 6.608]])
>>> Ainv = torch.linalg.inv(A)
>>> Ainv
tensor([[ 22473374., -9522616.],
[-32907440., 13943831.]])
>>> torch.dist(A @ Ainv, torch.eye(2))
tensor(4.2426) |
Re. Re. singular matrices, we do try to throw an error when the matrix is singular, but sometimes I guess that it fails and we don't catch it given that detecting that a matrix is exactly singular. For example, that exact matrix does fail on my machine throwing a:
while it succeeds on CUDA. As such, if you want to invert matrices that may or may not be invertible, you'd be better off doing some checks as computing the distance to I plan to improve both the efficiency and the stability of |
Thanks for your reply. If you want to fix |
We will implement |
I tested this method, and it didn't solve the original problem. Python 3.9.7 (default, Sep 16 2021, 13:09:58)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> A = torch.tensor([[4.1, 2.8], [9.676, 6.608]])
>>> Ainv, LU = torch.solve(torch.eye(A.size(-1), device=A.device), A)
>>> Ainv
tensor([[ 22473374., -9522616.],
[-32907440., 13943831.]])
>>> torch.dist(A @ Ainv, torch.eye(A.size(-1)))
tensor(4.2426) |
We don't aim to solve the original problem, as we don't implement the backends for these functions. Note that detecting whether a matrix is singular is a very difficult problem because we don't work with exact precision. If you do care about these things in your application, you should make sure that the matrix that you pass the solver ( That being said, we could update the docs to be a bit less explicit about what happens when the matrix is almost singular. |
Alright, I get the message. |
Fixed in master in #77079 |
Uh oh!
There was an error while loading. Please reload this page.
📚 The doc issue
https://pytorch.org/docs/stable/generated/torch.linalg.inv.html
In Examples, line 7 ends with an extra bracket.
Suggest a potential alternative/fix
cc @svekars @holly1238 @jianyuh @nikitaved @pearu @mruberry @walterddr @IvanYashchuk @xwang233 @lezcano
The text was updated successfully, but these errors were encountered: