Thank you so much, albanD. Yes, now it appears to work fine, though I need to do more testing. However, I also need to progress in my understanding: setting the flag requires_grad = True in z appeared to me to be necessary in order to be able to compute the gradient of the value returned by forward() and thus optimize the model parameters.
Thanks again.