Introduction to Deep Learning with PyTorch
Chapter 4: PyTorch for Automatic Gradient Descent
Exercise: Gradient Descent with Automatic Gradient
A few chapters before, you implemented the function gradient_descent_torch
, performing a gradient descent on torch
tensors.
But before, you were calculating the gradients by hand!
Exercise: Try implementing a function gradient_descent_torch_autograd(...)
that does the same thing
as gradient_descent_torch
, 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 |
|