def update_netd(self): """ Update D network: Ladv = |f(real) - f(fake)|_2 """ ## # Feature Matching. self.netd.zero_grad() # -- # Train with real self.label.data.resize_(self.input.size(0)).fill_(self.real_label) self.out_d_real, self.feat_real = self.netd(self.input) # self.err_d_real = self.bce_criterion(self.out_d_real,self.label) # Train with fake self.label.data.resize_(self.input.size(0)).fill_(self.fake_label) self.fake, self.latent_i, = self.netg(self.input) self.out_d_fake, self.feat_fake = self.netd(self.fake.detach()) # self.err_d_fake = self.bce_criterion(self.out_d_fake, self.label) # -- # self.err_d = self.err_d_real + self.err_d_fake self.err_d = l2_loss(self.feat_real, self.feat_fake) # self.err_d = self.err_d_fake + self.err_d_l2 # self.err_d_real = self.err_d # self.err_d_fake = self.err_d self.err_d.backward() self.optimizer_d.step()
def update_netd(self): """ Update D network: Ladv = |f(real) - f(fake)|_2 """ ## # Feature Matching. self.netd.zero_grad() # -- # Train with real self.out_d_real, self.feat_real = self.netd(self.input) # -- # Train with fake self.fake, self.latent_i, self.latent_o = self.netg(self.input) self.out_d_fake, self.feat_fake = self.netd(self.fake.detach()) # -- self.err_d = l2_loss(self.feat_real, self.feat_fake) self.err_d.backward() self.optimizer_d.step()
def update_netd(self): """ Update D network: Ladv = |f(real) - f(fake)|_2 """ ## # Feature Matching. self.netd.zero_grad() # -- # Train with real with torch.no_grad(): self.label.resize_(self.opt.batchsize).fill_(self.real_label) self.out_d_real, self.feat_real = self.netd(self.input) # -- # Train with fake with torch.no_grad(): self.label.resize_(self.opt.batchsize).fill_(self.fake_label) self.fake, self.latent_i, self.latent_o = self.netg(self.input) self.out_d_fake, self.feat_fake = self.netd(self.fake.detach()) # -- self.err_d = l2_loss(self.feat_real, self.feat_fake) self.err_d_real = self.err_d self.err_d_fake = self.err_d self.err_d.backward() self.optimizer_d.step()