for it in range(100000): timer.checkpoint('start D') # Sample data # ============ Train D ============# D_solver.zero_grad() z = Variable(torch.randn(mb_size, Z_dim)).cuda() X, c = celebA.batch_next(mb_size,0) X = Variable(X).cuda() AE_x = D(X) d_loss_real = torch.mean(torch.abs(AE_x - X)) # z = Variable(torch.FloatTensor(mb_size, Z_dim)) c_d = Variable(model.set_label_celebA(c.float(),mb_size)).cuda() c = Variable(c).cuda() # Dicriminator forward-loss-backward-update fake_image = G(torch.cat([z,c.float()],1)) AE_fake = D(fake_image.detach()) d_loss_fake = torch.mean(torch.abs(AE_fake - fake_image.detach())) d_loss = d_loss_real - k_t * d_loss_fake d_loss.backward() D_solver.step() # ============ Train G ============# # zero the grad buffer G_solver.zero_grad() timer.checkpoint('start G')
gamma = 0.5 lambda_k = 0.001 X, c_fixed = celebA.batch_next(mb_size, 0) z_fixed = Variable(torch.randn(mb_size, Z_dim), volatile=True).cuda() c_fixed = Variable(c_fixed, volatile=True).cuda() zc_fixed = torch.cat([z_fixed, c_fixed.float()], 1) ones_label = Variable(torch.ones(mb_size)).cuda() zeros_label = Variable(torch.zeros(mb_size)).cuda() for it in range(100000): if it % 10 == 0: # timer.checkpoint('start Ev') E_solver.zero_grad() X, c = celebA.batch_next(mb_size, label=1, shuffle=True) c_v = Variable(model.set_label_celebA(c, batch_size=mb_size)).cuda() X = Variable(X).cuda() # X.data.resize_(mb_size, 1, 32, 28) E_anch = E(torch.cat([X, c_v], 1)) E_loss1 = criterion(E_anch, ones_label) E_loss1.backward() X2, c2 = celebA.batch_next(mb_size, label=c, shuffle=False) c_v2 = Variable(model.set_label_celebA(c2, batch_size=mb_size)).cuda() X2 = Variable(X2).cuda() E_real = E(torch.cat([X2, c_v2], 1)) E_loss2 = criterion(E_real, ones_label) E_loss2.backward() X3, c3 = celebA.batch_next(mb_size, label=c,