...

/

Automatic Gradients with PyTorch

Automatic Gradients with PyTorch

Let’s see how we can compute automatic gradients with PyTorch and how PyTorch is different from plain Python and NumPy.

Creating a tensor with requires_grad=True argument

The following code creates a tensor called x just like before, but this time we give PyTorch an additional option requires_grad=True. We’ll see very soon what this option does.

Run the code to see what printing x does.

Press + to interact
import torch
# pytorch tensor
x = torch.tensor(3.5, requires_grad=True)
print("x:", x)

We can see that x has a value of 3.50003.5000 and is of type tensor. The output also shows that the tensor x has the requires_grad option set to True.

Functional relationship between variables

Let’s create a new variable y from x, just like before but this time using a different expression.

Press + to interact
import torch
# pytorch tensor
x = torch.tensor(3.5, requires_grad=True)
print("x:", x)
# y is defined as a function of x
y = (x-1) * (x-2) * (x-3)
print("y=(x-1) * (x-2) * (x-3):", y)

We can see that y is calculated from the expression (x-1) * (x-2) * (x-3).

As expected, the value of y is 1.87501.8750. This is because x is 3.53.5, so (3.5-1) * (3.5-2) * (3.5-3) is 1.87501.8750 ...