Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shouldn't it be D_real.backward(one)? #9

Closed
ypxie opened this issue Jun 6, 2017 · 6 comments
Closed

shouldn't it be D_real.backward(one)? #9

ypxie opened this issue Jun 6, 2017 · 6 comments

Comments

@ypxie
Copy link

ypxie commented Jun 6, 2017

  ```
   D_real.backward(mone)
   # train with fake
    noise = torch.randn(BATCH_SIZE, 128)
    if use_cuda:
        noise = noise.cuda()
    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)
@ypxie ypxie changed the title should it be D_real.backward(one)? shouldn't it be D_real.backward(one)? Jun 6, 2017
@caogang
Copy link
Owner

caogang commented Jun 6, 2017

No, D_real is what we want to maximize, so we minimize the loss (-D_real)

@ypxie
Copy link
Author

ypxie commented Jun 6, 2017

Thanks for your reply. That makes sense, but why does author of Wgan do the opposite in
https://github.com/martinarjovsky/WassersteinGAN/blob/master/main.py ?

@caogang
Copy link
Owner

caogang commented Jun 6, 2017

Maybe, the output of net_d is the loss or error in the implementation of wgan. It is up to the definition of net_d

@ypxie
Copy link
Author

ypxie commented Jun 7, 2017

Hello, Thanks for the explaining. I have a question, since you backward through the network twice, why is retain_variable=True not used in the code? And why not directly use D_cost.backward() once ?

@caogang
Copy link
Owner

caogang commented Jun 8, 2017

I use retain_graph=True in autograd.grad to support twice backward which is the same as retain_variable=True. And I want get the gradient w.r.t. input without accumulating into .grad, just get the gradient. So I use autograd.grad instead of backward(). https://github.com/caogang/wgan-gp/blob/master/gan_toy.py#L204-L207

@caogang caogang assigned caogang and unassigned caogang Jun 8, 2017
@caogang caogang closed this as completed Aug 16, 2017
@eifuentes
Copy link

@ypxie @caogang see the WGAN author's comment in this issue martinarjovsky/WassersteinGAN#9 the two approaches are equivalent as long as you are consistent.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants