SGD Example, Chapter 4


I’m working through the Stochastic Gradient Descent example in Chapter 4. It was working well until I got to calculating the loss and the gradients.

I’ve been through it a few times and I have copied across the code to Colab so I don’t think there are any typos.

My output deviates from the fastbook example - see attached image. Any ideas, please?

Much appreciated.

Hi! Since the parameters are initialized as random numbers…

params = torch.randn(3).requires_grad_()

…, your output is expected to be different than in the book. As long as the loss decreases when you apply apply_step a few times, you’re good :slight_smile:

Great, thanks Johannes :slight_smile: I must have missed that !