【问题标题】:DCGAN understanding generator update stepDCGAN 理解生成器更新步骤
【发布时间】:2021-12-12 19:02:46
【问题描述】:

这是 Pytorch 中的一些 DCGAN 示例: https://pytorch.org/tutorials/beginner/dcgan_faces_tutorial.html#training

我想知道为什么我们在更新生成器之前不将鉴别器梯度归零? (我在原始代码中添加了一行# netD.zero_grad() # Q: why we don't do this?) 是不是因为梯度在某个单独的变量中累积,不影响生成器中的梯度计算,只会影响权重的更新,但是在生成器更新步骤中,我们只更新生成器的权重,所以没关系?

这是训练循环中的代码部分:

############################
# (1) Update D network: maximize log(D(x)) + log(1 - D(G(z)))
###########################
## Train with all-real batch
netD.zero_grad()
# Format batch
real_cpu = data[0].to(device)
b_size = real_cpu.size(0)
label = torch.full((b_size,), real_label, dtype=torch.float, device=device)
# Forward pass real batch through D
output = netD(real_cpu).view(-1)
# Calculate loss on all-real batch
errD_real = criterion(output, label)
# Calculate gradients for D in backward pass
errD_real.backward()
D_x = output.mean().item()

## Train with all-fake batch
# Generate batch of latent vectors
noise = torch.randn(b_size, nz, 1, 1, device=device)
# Generate fake image batch with G
fake = netG(noise)
label.fill_(fake_label)
# Classify all fake batch with D
output = netD(fake.detach()).view(-1)
# Calculate D's loss on the all-fake batch
errD_fake = criterion(output, label)
# Calculate the gradients for this batch, accumulated (summed) with previous gradients
errD_fake.backward()
D_G_z1 = output.mean().item()
# Compute error of D as sum over the fake and the real batches
errD = errD_real + errD_fake
# Update D
optimizerD.step()

############################
# (2) Update G network: maximize log(D(G(z)))
###########################
netG.zero_grad()
# netD.zero_grad() # Q: why we don't do this?
label.fill_(real_label)  # fake labels are real for generator cost
# Since we just updated D, perform another forward pass of all-fake batch through D
output = netD(fake).view(-1)
# Calculate G's loss based on this output
errG = criterion(output, label)
# Calculate gradients for G
errG.backward()
D_G_z2 = output.mean().item()
# Update G
optimizerG.step()

【问题讨论】:

    标签: python deep-learning pytorch generative-adversarial-network


    【解决方案1】:

    ..梯度累积在一些单独的变量中..

    是的。那是正确的。它们是两个存储(读作“变量”)——一个用于参数,另一个用于其梯度。

    .. 不影响生成器中的梯度计算..

    也正确。让我们看看相关部分的计算图

    (x) -----> netG ----> (fake) -> netD -> (output)
                 ^                   ^
    (G_params) --|                   |
                                     |
                    (D_params) ------|
    

    这里,() 里面的东西是张量,其余的是“纯计算单元”。我已将参数明确描述为它们自己的计算单元的输入。当您点击.backward() 时,会发生以下计算(向后阅读)

    (GRAD_x) <----- netG <---- (GRAD_fake) <- netD <- (GRAD_output)
                      |                         ^
    (GRAD_G_params)<--|                         |
                                                |
                           (D_params) ----------|
    

    我在后向传递中用它们的梯度(即GRAD_*)替换了每个变量。

    你看,没有GRAD_D_params,因为它只是需要进一步进入netG,因为netG在产生D_params中没有任何作用 (它们是仅与netD 关联的独立实体)。

    简单地说,仅仅因为流“通过”netD 并不意味着它需要计算/使用梯度 w.r.t 它的参数。 GRAD_fake 的计算不需要GRAD_D_params(但它需要D_params),这就是为什么没有理由清理旧的GRAD_D_params。但从技术上讲,清理GRAD_D_params 也没有什么坏处。


    评论中的问题:GRAD_fake是如何用GRAD_output和D_params获得的?

    它的简单链式法则。让我们重命名变量

    fake -> x
    netD -> f()
    output -> y
    D_params -> p
    GRAD_output -> del_L/del_y
    

    然后前向计算简化为y = f(x, p)。不管f()是什么形式,计算x的梯度w.r.t有些损失L

    需要两件事,1.,基本上是GRAD_output; 2.或等价的,这基本上是一个涉及(x, p)(不是它们的派生词)的复杂函数。

    【讨论】:

    • 您能否详细说明仅使用GRAD_output 和D_params 是如何获得GRAD_fake 的?
    • 查看最后的编辑。 @mrgloom
    【解决方案2】:

    生成器和判别器被定义为单独的模型,每个模型都存储其梯度

    在您链接的示例中,他们已经为鉴别器损失完成了backwards。对于您提到的下一步(2) Update G network,他们只关心生成器损失,因此他们只重置Generator 梯度以根据该步骤进行的计算进行更新。

    当循环完成并在步骤 (1) Update D network 中再次开始时,它们会再次重置鉴别器梯度,以便能够基于步骤 1 操作进行反向传播

    我假设他们不重置D 渐变,因为在步骤(2) 上不需要它们,他们不关心这些值。仅在步骤(1) 他们将重置以计算所做计算的损失。

    【讨论】:

    • 在errG.backward()我们先经过netD,所以问题是为什么之前存储在netD中的梯度不影响netG梯度计算?
    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多