class_docstr: >>> GAN(img_shape=(1, 8, 8))  # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
  GAN(
    (generator): Generator(
      (model): Sequential(...)
    )
    (discriminator): Discriminator(
      (model): Sequential(...)
    )
  )
class_docstr_tok: [">",">",">","GAN","(","img_shape=","(","1",",","8",",","8",")",")","#","doctest",":","+ELLIPSIS","+NORMALIZE_WHITESPACE","GAN","(","(","generator",")",":","Generator","(","(","model",")",":","Sequential","(","...",")",")","(","discriminator",")",":","Discriminator","(","(","model",")",":","Sequential","(","...",")",")",")"]
code: def training_step(self, batch):
      imgs, _ = batch
  
      opt_g, opt_d = self.optimizers()
  
      # sample noise
      z = torch.randn(imgs.shape[0], self.hparams.latent_dim)
      z = z.type_as(imgs)
  
      # Train generator
      # ground truth result (ie: all fake)
      # put on GPU because we created this tensor inside training_loop
      valid = torch.ones(imgs.size(0), 1)
      valid = valid.type_as(imgs)
  
      self.toggle_optimizer(opt_g)
      # adversarial loss is binary cross-entropy
      g_loss = self.adversarial_loss(self.discriminator(self(z)), valid)
      opt_g.zero_grad()
      self.manual_backward(g_loss)
      opt_g.step()
      self.untoggle_optimizer(opt_g)
  
      # Train discriminator
      # Measure discriminator's ability to classify real from generated samples
      # how well can it label as real?
      valid = torch.ones(imgs.size(0), 1)
      valid = valid.type_as(imgs)
  
      self.toggle_optimizer(opt_d)
      real_loss = self.adversarial_loss(self.discriminator(imgs), valid)
  
      # how well can it label as fake?
      fake = torch.zeros(imgs.size(0), 1)
      fake = fake.type_as(imgs)
  
      fake_loss = self.adversarial_loss(
          self.discriminator(self(z).detach()), fake
      )
  
      # discriminator loss is the average of these
      d_loss = (real_loss + fake_loss) / 2
  
      opt_d.zero_grad()
      self.manual_backward(d_loss)
      opt_d.step()
      self.untoggle_optimizer(opt_d)
  
      self.log_dict({"d_loss": d_loss, "g_loss": g_loss})
  
imports: from argparse import ArgumentParser, Namespace
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from lightning.pytorch import cli_lightning_logo
  from lightning.pytorch.core import LightningModule
  from lightning.pytorch.demos.mnist_datamodule import MNISTDataModule
  from lightning.pytorch.trainer import Trainer
  from lightning.pytorch.utilities.imports import _TORCHVISION_AVAILABLE
label_desc: Use image node_id 4 for calling the GAN obj's underlying member method code with example usage: obj.training_step(batch) without return types