Chapter 4: PyTorch for Automatic Gradient Descent

Exercise: Gradient Descent with Automatic Gradient

face Luca Grillotti

Earlier in this lesson, you implemented the function gradient_descent_torch, performing a gradient descent on torch tensors.

But there, you were calculating the gradients by hand!

Exercise: Automate gradient computation!

Try implementing a function gradient_descent_torch_autograd(...) that does the same thing as gradient_descent_torch from earlier, but using the automatic gradient computation of PyTorch.

def gradient_descent_torch_autograd(initial_theta, learning_rate, number_steps):
    """
    Args:
        initial_theta (torch.Tensor): Initial value of theta
        learning_rate (float)
        number_steps (int): number of 1-step gradient descent to perform.

    Returns:
        final_theta (torch.Tensor): Final value of theta after multiple 1-step gradient descents with automatic gradient.
    """

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def get_gradient_autograd(tensor):
    tensor_with_grad = torch.nn.Parameter(tensor)  # considering tensor parameter (with grad)
    loss = tensor_with_grad * tensor_with_grad  # loss = theta * theta (considering square function)
    loss.backward()  # Computing gradients and fill tensor_with_grad.grad accordingly
    return tensor_with_grad.grad

def gradient_descent_torch_autograd(initial_theta, learning_rate, number_steps):
    """
    Args:
        initial_theta (torch.Tensor): Initial value of theta
        learning_rate (float)
        number_steps (int): number of 1-step gradient descent to perform.

    Returns:
        final_theta (torch.Tensor): Final value of theta after multiple 1-step gradient descents with automatic gradient.
    """
    tensor = initial_theta
    for _ in range(number_steps):
        tensor = tensor - learning_rate * get_gradient_autograd(tensor)
        print(tensor)
    return tensor

initial_theta = torch.Tensor([1])
gradient_descent_torch_autograd(initial_theta, learning_rate=0.2, number_steps=20)