Note: You should be careful with your optimizer because even if your gradients are 0, if you have weight decay or momentum, your weights are going to be updated.
I think your problem is when you do trained_model[i][0][k]=foo: You modify the weights inplace in a differentiable manner and the autograd engine does not know how to handle that. Hence the error.
I don’t think you want the gradients to flow back to network 2 right? In that case, you want to update your weights in a non-differentiable manner by wrapping your function in a with torch.no_grad(): statement.