Introduction to Deep Learning with PyTorch
Chapter 4: PyTorch for Automatic Gradient Descent
Exercise: Automatic Gradient Descent
A few chapters before, you implemented the function gradient_descent_torch_autograd
, performing a gradient descent on
torch tensors with automatic computation of gradients.
But before, you were performing the gradient descents by hand!
Exercise 1
Try implementing a function gradient_descent_torch_optimiser(...)
that does the same thing
as gradient_descent_torch_autograd
, but using the automatic update from the SGD
class of torch.optim
def gradient_descent_torch_optimiser(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 several gradient descents performed with the SGD torch optimiser.
"""
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Exercise 2
So far, we only considered the square function as loss function. Try implementing a
function gradient_descent_norm_torch_optimiser(...)
that performs automatic gradient descent on the loss:
L(\theta) = \| \theta \|_2^2, where \theta is a 2-dimensional vector.
In other words, L(\theta)=\theta_1^2+\theta_2^2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|