- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 134 Articles for PyTorch

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

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

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

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

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

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

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

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

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

751 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 the following steps to compute the exponentials of the elements of an input tensor −Import the torch library. Make sure you have it already installed.import torchCreate a tensor and print it.t1 = torch.rand(4, 3) print("Tensor:", t1)Compute the exponential of the elements of the tensor. For this, use torch.exp(input) and optionally ... Read More