Backward() and weight update for two linked nets

I have gone through the Introduction to Pytorch and searched information on backward() on the forum but an aspect of autograd remains unclear to me. Suppose we have two nets, A and B:

A(input) = a
output = B(a)

B_solver = optim.Adam(B.parameters(), lr=learning_rate_B)
B_loss = loss(output, target)
B_loss.backward()
B_solver.step()

The way backward() is described, this would calculate gradients all the way back through net A. Am I right in thinking that B_solver.step() will not update the weights for A? If so, then what would be a natural way of updating the weights of A? We do not have an explicit loss function for A alone. Doing the obvious, we quickly run into the need to retain the computational graph (which is discouraged in the documentation):

A_solver = optim.Adam(A.parameters(), lr=learning_rate_A)
A_solver.step()
A(input) = a
B(a) = output

This is not a very clear code. You cannot assign a returning to a variable

@JuanFMontesinos Sorry about that, corrected in the original post.

Still what you are doing is a bit strange.

class Dummy():
    def __init__(self):
        self.x = {'dummy':None}
    def __call__(self, *args, **kwargs):
        return self.x['dummy']
        
b=Dummy()
b()=a
  File "<ipython-input-8-e815e88d0509>", line 1
    b()=a
         ^
SyntaxError: can't assign to function call

I would say it’s not possible to return something from an object call and assign it to a variable
as you are doing there. My IDE agrees.

A(input) = a

Assuming that you meant this:

a=A(input)
output = B(a)

B_solver = optim.Adam(B.parameters(), lr=learning_rate_B)
B_loss = loss(output, target)
B_loss.backward()
B_solver.step()

A weights won’t be updated as the haven’t been passed to the optimizer. You can update them just by passing also A to the optimizer

1 Like