Found 26504 Articles for Server Side Programming

Python – scipy.linalg.cosm

Syed Abeed
Updated on 24-Dec-2021 10:10:45

220 Views

The cosm() function of scipy.linalg package is used to compute the cosine of an input matrix. This routine uses expm to compute the matrix exponentials.Syntaxscipy.linalg.cosm(x)where x is the input array.Example 1Let us consider the following example −# Import the required libraries from scipy import linalg import numpy as np # Define the input array q = np.array([[121 , 10] , [77 , 36]]) print("Array Input :", q) # Calculate the Cosine r = linalg.cosm(q) # Display the Cosine of matrix print("Cosine of Q: ", r)OutputThe above program will generate the following output − Array Input : ... Read More

Python – scipy.linalg.sinm()

Syed Abeed
Updated on 24-Dec-2021 10:08:49

233 Views

The sinm() function scipy.linalg package is used to compute the sine of an input matrix. This routine uses expm to compute the matrix exponentials.Syntaxscipy.linalg.sinm(x)where x is the inputer array.Example 1Let us consider the following example −# Import the required libraries from scipy from scipy import linalg import numpy as np # Define the input array X = np.array([[110, 12], [79, 23]]) print("Input Matrix, X:", X) # Calculate the Sine of the matrix n = linalg.sinm(X) # Display the Sine print("Sine of X: ", n)OutputIt will generate the following output − Input Matrix, X: [[110 12] ... Read More

Python – scipy.linalg.expm

Syed Abeed
Updated on 24-Dec-2021 10:07:01

2K+ Views

The expm() function of scipy.linalg package is used to compute the matrix exponential using Padé approximation. A Padé approximant is the "best" approximation of a function by a rational function of given order. Under this technique, the approximant's power series agrees with the power series of the function it is approximating.Syntaxscipy.linalg.expm(x)where x is the input matrix to be exponentiated.Example 1Let us consider the following example −# Import the required libraries from scipy import linalg import numpy as np # Define the input array e = np.array([[100 , 5] , [78 , 36]]) print("Input Array :", e) # Calculate ... Read More

How to check if an object is a PyTorch Tensor?

Shahid Akhtar Khan
Updated on 06-Dec-2021 12:44:53

8K+ Views

To check if an object is a tensor or not, we can use the torch.is_tensor() method. It returns True if the input is a tensor; False otherwise.Syntaxtorch.is_tensor(input)Parametersinput – The object to be checked, if it is a tensor or not .OutputIt returns True if the input is a tensor; else False.StepsImport the required library. The required library is torch.Define a tensor or other object.Check if the created object is a tensor or not using torch.is_tensor(input).Display the result.Example 1# import the required library import torch # create an object x x = torch.rand(4) print(x) # check if the above ... Read More

What does "with torch no_grad" do in PyTorch?

Shahid Akhtar Khan
Updated on 06-Dec-2021 12:35:44

6K+ Views

The use of "with torch.no_grad()" is like a loop where every tensor inside the loop will have requires_grad set to False. It means any tensor with gradient currently attached with the current computational graph is now detached from the current graph. We no longer be able to compute the gradients with respect to this tensor.A tensor is detached from the current graph until it is within the loop. As soon as it is out of the loop, it is again attached to the current graph if the tensor was defined with gradient.Let's take a couple of examples for a better ... Read More

What does backward() do in PyTorch?

Shahid Akhtar Khan
Updated on 06-Dec-2021 12:33:18

3K+ Views

The backward() method is used to compute the gradient during the backward pass in a neural network.The gradients are computed when this method is executed.These gradients are stored in the respective variables.The gradients are computed with respect to these variables, and the gradients are accessed using .grad.If we do not call the backward() method for computing the gradient, the gradients are not computed.And, if we access the gradients using .grad, the result is None.Let's have a couple of examples to demonstrate how it works.Example 1In this example, we attempt to access the gradients without calling the backward() method. We notice ... Read More

PyTorch – How to check if a tensor is contiguous or not?

Shahid Akhtar Khan
Updated on 06-Dec-2021 12:29:28

2K+ Views

A contiguous tensor is a tensor whose elements are stored in a contiguous order without leaving any empty space between them. A tensor created originally is always a contiguous tensor. A tensor can be viewed with different dimensions in contiguous manner.A transpose of a tensor creates a view of the original tensor which follows non-contiguous order. The transpose of a tensor is non-contiguous.SyntaxTensor.is_contiguous()It returns True if the Tensor is contiguous; False otherwise.Let's take a couple of example to demonstrate how to use this function to check if a tensor is contiguous or non-contiguous.Example 1# import torch library import torch ... Read More

How to find the transpose of a tensor in PyTorch?

Shahid Akhtar Khan
Updated on 06-Dec-2021 12:20:28

8K+ Views

To transpose a tensor, we need two dimensions to be transposed. If a tensor is 0-D or 1-D tensor, the transpose of the tensor is same as is. For a 2-D tensor, the transpose is computed using the two dimensions 0 and 1 as transpose(input, 0, 1).SyntaxTo find the transpose of a scalar, a vector or a matrix, we can apply the first syntax defined below.And for any dimensional tensor, we can apply the second syntax.For

How to move a Torch Tensor from CPU to GPU and vice versa?

Shahid Akhtar Khan
Updated on 06-Nov-2023 03:42:20

26K+ Views

A torch tensor defined on CPU can be moved to GPU and vice versa. For high-dimensional tensor computation, the GPU utilizes the power of parallel computing to reduce the compute time.High-dimensional tensors such as images are highly computation-intensive and takes too much time if run over the CPU. So, we need to move such tensors to GPU.SyntaxTo move a torch tensor from CPU to GPU, following syntax/es are used −Tensor.to("cuda:0") or Tensor.to(cuda)And, Tensor.cuda()To move a torch tensor from GPU to CPU, the following syntax/es are used −Tensor.to("cpu")And, Tensor.cpu()Let's take a couple of examples to demonstrate how a tensor can be ... Read More

How to get the rank of a matrix in PyTorch?

Shahid Akhtar Khan
Updated on 06-Dec-2021 11:43:25

2K+ Views

The rank of a matrix can be obtained using torch.linalg.matrix_rank(). It takes a matrix or a batch of matrices as the input and returns a tensor with rank value(s) of the matrices. torch.linalg module provides us many linear algebra operations.Syntaxtorch.linalg.matrix_rank(input)where input is the 2D tensor/matrix or batch of matrices.StepsWe could use the following steps to get the rank of a matrix or batch of matrices −Import the torch library. Make sure you have it already installed.import torch Create a 2D tensor/matrix or a batch of matrices and print it.t = torch.tensor([[1., 2., 3.], [4., 5., 6.]]) print("Tensor:", t)Compute the rank ... Read More

Advertisements