ptrblck:

ong as output is attached to the graph, the code should work.


Thank you @ptrblck for you reply.
I removed the requires_grad setting to make the sobel filter static. The problem is, when I run the code - after the first loss.backward() - the network output becomes nan. I believe it was some problem when computing grad.
If I change the loss for MSE doing:
def my_mse(output, target):
out = torch.mean(torch.pow(output-target,2))
everything works fine. This way I suppose that there is no problem in the rest of the code.
Do you have any idea about what I am missing?
Thanks a lot