AttributeError: module ‘torch.linalg’ has no attribute ‘inv’ ( Solved )

AttributeError_ module 'torch.linalg' has no attribute 'inv' ( Solved )

Pytorch is a python framework built on the torch library. It is open source and free to use and contains a large number of libraries that are helpful in building applications on computer vision and NLP. But while using this module you may get error like AttributeError. The error like module ‘torch.linalg’ has no attribute ‘inv’.

In this tutorial, you will learn how to solve module ‘torch.linalg’ has no attribute ‘inv’ error in a simple way.

Why does module ‘torch.linalg’ has no attribute ‘inv’ Error occurs?

The root cause for getting the module ‘torch.linalg’ has no attribute ‘inv’ is that you are not using that version of the Pytorch that supports the ” inv ” function. It means that the inv() function is not included in the Pytorch version greater than 0.3.1.

You will get the AttributeError when you will run the below lines of code.

import torch
A = torch.tensor([[1.,2.],[3.,4.]])
A_inv = torch.linalg.inv(A)
A_inv

Output

AttributeError: module 'torch.linalg' has no attribute 'inv'

Solution of the module ‘torch.linalg’ has no attribute ‘inv’ Error

The solution to this error is very simple. You have to install the specific Pytorch module that is version 0.3.1  if you want to use the inv() function. But before that, you have to first uninstall the Pytorch module and then install the 0.3.1 version.

Run the below command to achieve the above tasks.

pip3 uninstall torch

pip3 install torch==0.3.1

Now you will not get this attribute ‘inv’ Error when you will run the below lines of code.

import torch
A = torch.tensor([[1.,2.],[3.,4.]])
A_inv = torch.linalg.inv(A)
A_inv

Output

Inverse of the tensor
The inverse of the tensor

The other solution is to use the new function included with the newest version of Pytorch. Pytorch has an inverse() function that calculates the inverse of Tensor.

You will get the same output after running the below lines of code.

 

import torch
A = torch.tensor([[1.,2.],[3.,4.]])
A_inv = torch.inverse(A)
A_inv

Output

Inverse of the tensor using the inverse function
Inverse of the tensor using the inverse function

Conclusion

Python interpreter output the exception error as AttributeError when you are using a function that does not include the version of the module. The above case was the same. If you are getting this no attribute ‘inv’ then the above solutions will work for you.

I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner