code: def main(_):
      # Load training and test data
      data = ld_mnist()
  
      # Instantiate model, loss, and optimizer for training
      if FLAGS.model == "cnn":
          net = CNN(in_channels=1)
  
      elif FLAGS.model == "pynet":
          net = PyNet(in_channels=1)
      else:
          raise NotImplementedError
  
      device = "cuda" if torch.cuda.is_available() else "cpu"
      if device == "cuda":
          net = net.cuda()
      loss_fn = torch.nn.CrossEntropyLoss(reduction="mean")
      optimizer = torch.optim.Adam(net.parameters(), lr=1e-3)
  
      # Train vanilla model
      net.train()
      for epoch in range(1, FLAGS.nb_epochs + 1):
          train_loss = 0.0
          for x, y in data.train:
              x, y = x.to(device), y.to(device)
              if FLAGS.adv_train:
                  # Replace clean example with adversarial example for adversarial training
                  x = projected_gradient_descent(
                      net, x, FLAGS.eps, 0.01, 40, np.inf
                  )
              optimizer.zero_grad()
              loss = loss_fn(net(x), y)
              loss.backward()
              optimizer.step()
              train_loss += loss.item()
          print(
              "epoch: {}/{}, train loss: {:.3f}".format(
                  epoch, FLAGS.nb_epochs, train_loss
              )
          )
  
      # Evaluate on clean and adversarial data
      net.eval()
      report = EasyDict(
          nb_test=0, correct=0, correct_fgm=0, correct_pgd=0
      )
      for x, y in data.test:
          x, y = x.to(device), y.to(device)
          x_fgm = fast_gradient_method(net, x, FLAGS.eps, np.inf)
          x_pgd = projected_gradient_descent(
              net, x, FLAGS.eps, 0.01, 40, np.inf
          )
          _, y_pred = net(x).max(
              1
          )  # model prediction on clean examples
          _, y_pred_fgm = net(x_fgm).max(
              1
          )  # model prediction on FGM adversarial examples
          _, y_pred_pgd = net(x_pgd).max(
              1
          )  # model prediction on PGD adversarial examples
          report.nb_test += y.size(0)
          report.correct += y_pred.eq(y).sum().item()
          report.correct_fgm += y_pred_fgm.eq(y).sum().item()
          report.correct_pgd += y_pred_pgd.eq(y).sum().item()
      print(
          "test acc on clean examples (%): {:.3f}".format(
              report.correct / report.nb_test * 100.0
          )
      )
      print(
          "test acc on FGM adversarial examples (%): {:.3f}".format(
              report.correct_fgm / report.nb_test * 100.0
          )
      )
      print(
          "test acc on PGD adversarial examples (%): {:.3f}".format(
              report.correct_pgd / report.nb_test * 100.0
          )
      )
  
file_path: cleverhans/tutorials/torch/mnist_tutorial.py
imports: from absl import app, flags
  from easydict import EasyDict
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  import torchvision
  from datasets import MNISTDataset
  from cleverhans.torch.attacks.fast_gradient_method import fast_gradient_method
  from cleverhans.torch.attacks.projected_gradient_descent import projected_gradient_descent
label_desc: Use image node_id 2 for calling a global function with example usage: main(_) without return types