Skip to content

How does optimizer work when there are 3 backwards(real, fake, penalty)? #59

Description

@wook3024

I thought I should write like
"D_cost = D_fake - D_real + gradient_penalty
D_cost.backward()"
but I don't know why you use backward like that.

wgan-gp/gan_cifar10.py

Lines 203 to 226 in ae47a18

D_real = netD(real_data_v)
D_real = D_real.mean()
D_real.backward(mone)
# train with fake
noise = torch.randn(BATCH_SIZE, 128)
if use_cuda:
noise = noise.cuda(gpu)
noisev = autograd.Variable(noise, volatile=True) # totally freeze netG
fake = autograd.Variable(netG(noisev).data)
inputv = fake
D_fake = netD(inputv)
D_fake = D_fake.mean()
D_fake.backward(one)
# train with gradient penalty
gradient_penalty = calc_gradient_penalty(netD, real_data_v.data, fake.data)
gradient_penalty.backward()
# print "gradien_penalty: ", gradient_penalty
D_cost = D_fake - D_real + gradient_penalty
Wasserstein_D = D_real - D_fake
optimizerD.step()

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions