Shahid Akhtar Khan has Published 216 Articles

How to check if an object is a PyTorch Tensor?

Shahid Akhtar Khan

Shahid Akhtar Khan

Updated on 06-Dec-2021 12:44:53

6K+ 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 ... Read More

What does "with torch no_grad" do in PyTorch?

Shahid Akhtar Khan

Shahid Akhtar Khan

Updated on 06-Dec-2021 12:35:44

5K+ 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 ... Read More

What does backward() do in PyTorch?

Shahid Akhtar Khan

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 ... Read More

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

Shahid Akhtar Khan

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 ... Read More

How to find the transpose of a tensor in PyTorch?

Shahid Akhtar Khan

Shahid Akhtar Khan

Updated on 06-Dec-2021 12:20:28

7K+ 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 ... Read More

How to get the rank of a matrix in PyTorch?

Shahid Akhtar Khan

Shahid Akhtar Khan

Updated on 06-Dec-2021 11:43:25

1K+ 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 ... Read More

PyTorch – How to get the exponents of tensor elements?

Shahid Akhtar Khan

Shahid Akhtar Khan

Updated on 06-Dec-2021 11:32:17

1K+ Views

To find the exponential of the elements of an input tensor, we can apply Tensor.exp() or torch.exp(input). Here, input is the input tensor for which the exponentials are computed. Both the methods return a new tensor with the exponential values of the elements of the input tensor.SyntaxTensor.exp()ortorch.exp(input) StepsWe could use ... Read More

PyTorch – torch.log2() Method

Shahid Akhtar Khan

Shahid Akhtar Khan

Updated on 06-Dec-2021 11:28:13

380 Views

We use the torch.log2() method to compute logarithm to the base 2 of the elements of a tensor. It returns a new tensor with the logarithm values of the elements of the original input tensor. It takes a tensor as the input parameter and outputs a tensor.Syntaxtorch.log2(input)where input is a ... Read More

What does Tensor.detach() do in PyTorch?

Shahid Akhtar Khan

Shahid Akhtar Khan

Updated on 06-Dec-2021 11:24:29

11K+ Views

Tensor.detach() is used to detach a tensor from the current computational graph. It returns a new tensor that doesn't require a gradient.When we don't need a tensor to be traced for the gradient computation, we detach the tensor from the current computational graph.We also need to detach a tensor when ... Read More

How to compute gradients in PyTorch?

Shahid Akhtar Khan

Shahid Akhtar Khan

Updated on 06-Dec-2021 11:20:48

11K+ Views

To compute the gradients, a tensor must have its parameter requires_grad = true. The gradients are same as the partial derivatives.For example, in the function y = 2*x + 1, x is a tensor with requires_grad = True. We can compute the gradients using y.backward() function and the gradient can ... Read More

Advertisements