Thanks for the reply. I just add a figure and code.
Assume batch_size =1,
the input is xi, we get yi after the net (modelA) and store yi into a shared tensor yi’,
For example, the figure shows the example of input i=2.
If the input is x3, then we get y3 and then y3’. So the tensor [y1’, y2’, y3’…] is used in different epochs.
If y2’= y2.clone(), then y2’ also requires grad, so the shared tensor needs grad, if have to set loss.backward(retain_graph=True), or get "trying to backward through the graph a second time, but the buffers have already been freed. ". But if retrain_graph = True, the graph will grow continually to be too big.
if y2’= y2.detach(), there’s no the above problem. but loss_2 will not require grad, so network cannot be updated with loss_2.
Can I combine loss_1 and loss_2 together, and call backward() once? Or I should loss_1.backward() and loss_2.backward()?