Answers.Body: <p>Since you are using Pytorch, an optimizer can only update parameters wrapped inside it since initializing.</p>
  <p>Therefore, even if the gradient of discriminator parameters exists while updating generator, discriminator parameters are still stable if you don't call <code>disc_opt.step()</code>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-16T18:36:40.887Z
Answers.Text: you are correct
Answers.UserDisplayName: Don Feto
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-12T07:10:01.297Z
Answers.LastActivityDate: 2022-08-12T07:10:01.297Z
Answers.UserDisplayName: CuCaRot
Body: <p>When we train the GAN we usually train the discriminator first then the generator, first we stop the generator from updating its weight by removing it from the computation graph, using fake_image.detach()</p>
  <pre><code>noise=get_noise(num_images,z_dim,device=device)
  fake_images=gen(noise)
  
  disc_fake_pred = disc(fake_images.detach()) 
  disc_fake_loss=criterion(disc_fake_pred,torch.zeros_like(disc_fake_pred)) 
  disc_real_pred = disc(real)
  disc_real_loss=criterion(disc_real_pred,torch.ones_like(disc_real_pred))
      
  disc_loss = (disc_real_loss + disc_fake_loss)/2
  disc_loss.backward()
  disc_opt.step()
  </code></pre>
  <p>But when we want to train the discriminator we don't stop discriminator weights all we do is just leave the generator weights in the computational graph, this means discriminator and generator weights will be updated since we didn't stop it</p>
  <pre><code>gen_opt.zero_grad()
  z_noise = get_noise(num_images, z_dim, device)
  fake_images = gen(z_noise)
  disc_fake_pred = disc(fake_images) 
  gen_loss = criterion(disc_fake_pred, torch.ones_like(disc_fake_pred)) 
  gen_loss.backward()
  gen_opt.step()
  </code></pre>
  <p>But in</p>
  
Tags: <deep-learning><generative-adversarial-networks><pytorch><discriminator><dc-gan>
Title: Does the Weights of Discriminator get updated when traning Generators in GANs?
UserDisplayName: Don Feto
ThreadText: # Does the Weights of Discriminator get updated when traning Generators in GANs?
  
  (Asked by: username_0 on 2022-08-12)
  
  When we train the GAN we usually train the discriminator first then the generator, first we stop the generator from updating its weight by removing it from the computation graph, using fake_image.detach()
  noise=get_noise(num_images,z_dim,device=device)
  fake_images=gen(noise)
  
  disc_fake_pred = disc(fake_images.detach()) 
  disc_fake_loss=criterion(disc_fake_pred,torch.zeros_like(disc_fake_pred)) 
  disc_real_pred = disc(real)
  disc_real_loss=criterion(disc_real_pred,torch.ones_like(disc_real_pred))
      
  disc_loss = (disc_real_loss + disc_fake_loss)/2
  disc_loss.backward()
  disc_opt.step()
  
  But when we want to train the discriminator we don't stop discriminator weights all we do is just leave the generator weights in the computational graph, this means discriminator and generator weights will be updated since we didn't stop it
  gen_opt.zero_grad()
  z_noise = get_noise(num_images, z_dim, device)
  fake_images = gen(z_noise)
  disc_fake_pred = disc(fake_images) 
  gen_loss = criterion(disc_fake_pred, torch.ones_like(disc_fake_pred)) 
  gen_loss.backward()
  gen_opt.step()
  
  But in
  
  **Tags:** deep-learning, generative-adversarial-networks, pytorch, discriminator, dc-gan
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-08-12. Score: 1 | Accepted answer
  
  Since you are using Pytorch, an optimizer can only update parameters wrapped inside it since initializing.
  
  Therefore, even if the gradient of discriminator parameters exists while updating generator, discriminator parameters are still stable if you don't call `disc_opt.step()`.
  
  > **Comments:**
  > **username_0**: you are correct  
  
  ---
  
  