Hi Ryan!



 machina:

The goal of this network is to train G to produce adversarial examples that look like real data, and fool F.


As I understand it, your have three models here:  G is gen in the
code you posted above, D is disc, and F, I suppose, is target.
For this part of the training I’ll assume that the parameters of gen
and target are (temporarily) frozen, and that you are training just
the parameters of gen.
You want the model target to misclassify, that is (going back to the
code you posted), f_loss is large.  You also want disc to mistake
fake for real, that is, lossG is also large.
Pytorch optimizers seek to minimize the losses they’re given, so you
should simply flip the signs of f_loss and lossG and backpropagate
normally through gen.  (Based on your clarification that you want gen
to fool target (as well as disc), you should – unlike what I said in my
first reply – also flip the sign of f_loss.)  Thus:
loss_G_Final = - (f_loss + lossG)
opt_gen.zero_grad()
loss_G_Final.backward()
opt_gen.step()

(And again, either in separate training steps or in a combined
procedure, you presumably also want to be training disc to do
as well it can on the fake-vs-real task you are giving it, even as
you train gen to keep getting better at tricking disc.)
Best.
K. Frank