Consider a simple GAN model as shown here https://github.com/eriklindernoren/PyTorch-GAN/blob/master/implementations/gan/gan.py 1
L144 and L157 have a common computation: discriminator(gen_imgs). They only differ in that one detaches the generator from the computational graph. However, the forward pass of the discriminator is still computed twice.
Is it possible to compute discriminator(gen_imgs) only once, but detach gen_imgs from the graph after computation?
E.g something like
d_out = discriminator(gen_imgs)
g_loss = adversarial_loss(d_out, valid)
(...)
fake_loss = adversarial_loss(d_out.detach(gen_imgs), fake)