How to create tensors with gradients in PyTorch?



To create a tensor with gradients, we use an extra parameter "requires_grad = True" while creating a tensor.

  • requires_grad is a flag that controls whether a tensor requires a gradient or not.

  • Only floating point and complex dtype tensors can require gradients.

  • If requires_grad is false, then the tensor is same as the tensor without the requires_grad parameter.

Syntax

torch.tensor(value, requires_grad = True)

Parameters

  • value – tensor data, user-defined or randomly generated.

  • requires_grad – a flag, if True, the tensor is included in the gradient computation.

Output

It returns a tensor with requires_grad as True.

Steps

  • Import the required library. The required library is torch.

  • Define a tensor with requires_grad = True

  • Display the created tensor with gradients.

Let's have a couple of examples for a better understanding of how it works.

Example 1

In the following example, we created two tensors. One tensor is without requires_grad = True and the other is with requires_grad = True.

# import torch library import torch # create a tensor without gradient tensor1 = torch.tensor([1.,2.,3.]) # create another tensor with gradient tensor2 = torch.tensor([1.,2.,3.], requires_grad = True) # print the created tensors print("Tensor 1:", tensor1) print("Tensor 2:", tensor2)

Output

 Tensor 1: tensor([1., 2., 3.]) Tensor 2: tensor([1., 2., 3.], requires_grad=True)

Example 2

# import required library import torch # create a tensor without gradient tensor1 = torch.randn(2,2) # create another tensor with gradient tensor2 = torch.randn(2,2, requires_grad = True) # print the created tensors print("Tensor 1:
", tensor1) print("Tensor 2:
", tensor2)

Output

Tensor 1: tensor([[-0.9223, 0.1166], [ 1.6904, 0.6709]]) Tensor 2: tensor([[ 1.1912, -0.1402], [-0.2098, 0.1481]], requires_grad=True)

Example 3

In the following example, we created a tensor with gradients using numpy array.

# import the required libraries import torch import numpy as np # create a tensor of random numbers with gradients # generate 2x2 numpy array of random numbers v = np.random.randn(2,2) # create a tensor with above random numpy array tensor1 = torch.tensor(v, requires_grad = True) # print above created tensor print(tensor1)

Output

tensor([[ 0.7128, 0.8310], [ 1.6389, -0.3444]], dtype=torch.float64, requires_grad=True)
Updated on: 2021-12-06T10:54:45+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements