If I do inside torch.no_grad() z_hat=E(x_hat) and then distance(z_hat,z.detach()).backward(), no gradients are produced. What I meant here



 Sandro_Giacomuzzi:

i.e. the network is blind to anything that happens after the no_grad() section


is, that if you would further process z_hat outside torch.no_grad() then of course you can have gradients again for these modules. In that sense applying torch.no_grad() at some position is like clipping the gradient flow at that position in the graph.