Python PyTorch – torch.linalg.solve() Function
Last Updated :
10 Jun, 2022
In this article, we will discuss torch.linalg.solve() method in PyTorch.
Example:
Let's consider the linear equations :
6x + 3y = 1
3x - 4y = 2
Then M values can be - [[6,3],[3,-4]]
and t is [1,2]
torch.linalg.solve() Function
The torch.linalg.solve() method is used to solve a square system of linear equations with a unique solution. It will take two parameters, in which the first parameter is a matrix that is a tensor and the second parameter is also a tensor with one dimension.
Syntax: torch.linalg.solve(M, t)
Parameters:
- M is an tensor matrix
- t is a tensor vector.
Return: It will return a tensor.
Example1:
In this example, we will Solve the linear equation - 6x + 3y = 1, 3x - 4y = 2 and check the solution is true or not. Here M is [[6,3],[3,-4]] and t [1,2]. After that we will apply torch.linalg.solve() method to return unique tensor solution. Finally we will use torch.allclose() method to check the equation is true or not.
Python3
# import torch
import torch
'''
Let's consider the linear equations :
6x + 3y = 1
3x - 4y = 2
Then M values can be - [[6,3],[3,-4]]
and t is [1,2]
'''
# consider M which is an 2 D tensor that
# has 2 elements each
M = torch.tensor([[6., 3.], [3., -4.]])
# consider t which is 1D that has two elements
t = torch.tensor([1., 2.])
# Solve the equation using linalg.solve(M,t)
solved = torch.linalg.solve(M, t)
# display the solved solution
print(solved)
# check the solution is true or not using
# allclose() method
print(torch.allclose(M @ solved, t))
Output:
tensor([ 0.3030, -0.2727])
True
Example 2:
In this example, we will Solve the linear equation - 6x + 3y = 1,3x - 4y = 2 and check the solution is true or not. Here the elements of M is [[6,3],[3,-4]] and t is [0,2]. After that, we will apply torch.linalg.solve() method which will return a unique tensor solution. Finally, we will use a torch.allclose() method to check whether the equation is true or not.
Python3
# import torch
import torch
'''
Let's consider the linear equations :
5x - 3y = 0
3x - 4y = 2
Then M values can be - [[5,-3],[3,-4]]
and t is [0,2]
'''
# consider M which is an 2 D tensor that
# has 2 elements each
M = torch.tensor([[5., -3.], [3., -4.]])
# consider t which is an 1 D tensor that
# has 2 elements
t = torch.tensor([0., 2.])
# Solve the equation using linalg.solve(M,t)
# method
solved = torch.linalg.solve(M, t)
# display the solved solution
print(solved)
# check the solution is true or not using
# allclose() method
print(torch.allclose(M @ solved, t))
Output:
tensor([-0.5455, -0.9091])
True
Example 3:
In this example, we will Solve the linear equation - 9x - y = 0, 3x - 4y = 0 and check the solution is true or not. Here the elements of M is [[9,-1],[3,-4]] and t [0,0]. After that, we will apply the torch.linalg.solve() method which will return a unique tensor solution, and last we will use a torch.allclose() method to check whether the equation is true or not.
Python3
# import torch
import torch
'''
Solve the linear equation - 9x - y = 0,
3x - 4y = 0 and check the solution is true or not
Here the elements of M is - [[9,-1],[3,-4]] and t - [0,0].
'''
# consider M which is an 2 D tensor that
# has 2 elements each
M = torch.tensor([[9., -1.], [3., -4.]])
# consider t which is an 1 D tensor that
# has 2 elements
t = torch.tensor([0., 0.])
# Solve t using linalg.solve(M,t) method
solved = torch.linalg.solve(M, t)
# display the solved solution
print(solved)
# check the solution is true or not using
# allclose() method
print(torch.allclose(M @ solved, t))
Output:
tensor([0., -0.])
True
Similar Reads
Python PyTorch â torch.linalg.cond() Function
In this article, we are going to discuss how to compute the condition number of a matrix in PyTorch. we can get the condition number of a matrix by using torch.linalg.cond() method. torch.linalg.cond() method This method is used to compute the condition number of a matrix with respect to a matrix n
3 min read
Python PyTorch â torch.polar() Function
In this article, we will discuss the torch.polar() method in Pytorch using Python. torch.polar() method torch.polar() method is used to construct a complex number using absolute value and angle. The data types of these absolute values and angles must be either float or double. If the absolute value
3 min read
Convert PyTorch Tensor to Python List
PyTorch, a widely-used open-source machine learning library, is known for its flexibility and ease of use in building deep learning models. A fundamental component of PyTorch is the tensor, a multi-dimensional array that serves as the primary data structure for model training and inference. However,
3 min read
Python PyTorch linalg.svd() method
PyTorch linalg.svd() method computes the singular value decomposition (SVD) of a matrix. 2D tensors are matrices in PyTorch. This method supports both real and complex-valued matrices (float, double, cfloat, and cdouble dtypes). Â It takes input a matrix or a batch of matrices and returns decompositi
4 min read
Converting an image to a Torch Tensor in Python
In this article, we will see how to convert an image to a PyTorch Tensor. A tensor in PyTorch is like a NumPy array containing elements of the same dtypes. Â A tensor may be of scalar type, one-dimensional or multi-dimensional. To convert an image to a tensor in PyTorch we use PILToTensor() and ToTe
3 min read
Python PyTorch â RandomHorizontalFlip() Function
In this article, we will discuss the RandomHorizontalFlip() Method in PyTorch Python. RandomHorizontalFlip() method RandomHorizontalFlip() method of torchvision.transforms module is used to horizontally flip the given image at a random angle with a given probability. This method accepts a PIL and te
2 min read
Python PyTorch â backward() Function
In this article, we are going to discuss the backward() method in Pytorch with detailed examples. backward() MethodThe backward() method in Pytorch is used to calculate the gradient during the backward pass in the neural network. If we do not call this backward() method then gradients are not calcul
2 min read
Python PyTorch stack() method
PyTorch torch.stack() method joins (concatenates) a sequence of tensors (two or more tensors) along a new dimension. It inserts new dimension and concatenates the tensors along that dimension. This method joins the tensors with the same dimensions and shape. We could also use torch.cat() to join ten
5 min read
Python - PyTorch trunc() method
PyTorch torch.trunc() method returns a new tensor with the truncated integer values of the elements of input/ after removing the decimal portion of the number. Syntax: torch.trunc(input, out=None) Arguments input: This is input tensor. out: The output tensor. Return: It returns a Tensor. Let's see t
1 min read
Visualization of ConvNets in Pytorch - Python
Convolutional Neural Networks (ConvNets or CNNs) are a category of Neural Networks that have proven very effective in areas such as image recognition and classification. Understanding the behavior of ConvNets can be a complex task, especially when working with large image datasets. To help with this
5 min read