I have a model trained this way:
G = model() 
out1 = G(inp)
out2 = G(out1)
criterion(out2,y)

my question, if I calculate the gradient this way it will be calculated on the unfolded model G. I would like to calculate the gradient on the the second call only and taking the output of the first model as the input. Does detach() fix my question here or I should re-create a variable ?