Found 135 Articles for PyTorch

How to crop an image at center in PyTorch?

Shahid Akhtar Khan
Updated on 06-Jan-2022 09:43:59

4K+ Views

To crop an image at its center, we apply CenterCrop(). It's one of the transforms provided by the torchvision.transforms module. This module contains many important transformations that can be used to perform manipulation on the image data.CenterCrop() transformation accepts both PIL and tensor images. A tensor image is a PyTorch tensor with shape [C, H, W], where C is the number of channels, H is the image height and W is the image width.This transform also accepts a batch of tensor images. A batch of tensor images is a tensor with [B, C, H, W]. B is the number of ... Read More

How to convert a PyTorch tensor with gradient to a numpy array?

Shahid Akhtar Khan
Updated on 06-Jan-2022 09:34:56

5K+ Views

To convert a Torch tensor with gradient to a Numpy array, first we have to detach the tensor from the current computing graph. To do it, we use the Tensor.detach() operation. This operation detaches the tensor from the current computational graph. Now we cannot compute the gradient with respect to this tensor. After the detach() operation, we use the .numpy() method to convert it to a Numpy array.If a tensor with requires_grad=True is defined on GPU, then to convert this tensor to a Numpy array, we have to perform one more step. First we have to move the tensor to ... Read More

How to check if an object is a PyTorch Tensor?

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 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

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 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

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 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

22K+ 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

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 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

How to normalize a tensor in PyTorch?

Shahid Akhtar Khan
Updated on 31-Oct-2023 03:57:21

23K+ Views

A tensor in PyTorch can be normalized using the normalize() function provided in the torch.nn.functional module. This is a non-linear activation function.It performs Lp normalization of a given tensor over a specified dimension.It returns a tensor of normalized value of the elements of original tensor.A 1D tensor can be normalized over dimension 0, whereas a 2D tensor can be normalized over both dimensions 0 and 1, i.e., column-wise or row-wise.An n-dimensional tensor can be normalized over dimensions (0, 1, 2, ..., n-1).Syntaxtorch.nn.functional.normalize(input, p=2.0, dim = 1)ParametersInput – Input tensorp – Power (exponent) value in norm formulationdim – Dimension over which ... Read More

Advertisements