# requires_grad=True tells PyTorch to compute the gradients for this tensor x = torch.tensor(2.0, requires_grad=True) # Define the forward pass y = x**2 z = y + 1 w = z**2 # Calculate the gradient w.backward() # print the gradient print(x.grad) # 40