How to implement an iterative update rule?

Hi

I want to implement the following algorithm, taken from this book, section 13.6

Pytorch requires me to compute a loss for w and for theta.
I am struggling on coming up with that loss term, because it requires to add some factor that depends on the current weights to the derivative - something I don’t really know how to do.

I would appreciate some small code snippet to see how something like this is done with pytorch.

This same question on stackoverflow, with clearer formulas