code: def main():
      # Training settings
      parser = argparse.ArgumentParser(
          description="Opacus MNIST Example",
          formatter_class=argparse.ArgumentDefaultsHelpFormatter,
      )
      parser.add_argument(
          "-b",
          "--batch-size",
          type=int,
          default=64,
          metavar="B",
          help="Batch size",
      )
      parser.add_argument(
          "--test-batch-size",
          type=int,
          default=1024,
          metavar="TB",
          help="input batch size for testing",
      )
      parser.add_argument(
          "-n",
          "--epochs",
          type=int,
          default=10,
          metavar="N",
          help="number of epochs to train",
      )
      parser.add_argument(
          "-r",
          "--n-runs",
          type=int,
          default=1,
          metavar="R",
          help="number of runs to average on",
      )
      parser.add_argument(
          "--lr",
          type=float,
          default=0.1,
          metavar="LR",
          help="learning rate",
      )
      parser.add_argument(
          "--sigma",
          type=float,
          default=1.0,
          metavar="S",
          help="Noise multiplier",
      )
      parser.add_argument(
          "-c",
          "--max-per-sample-grad_norm",
          type=float,
          default=1.0,
          metavar="C",
          help="Clip per-sample gradients to this norm",
      )
      parser.add_argument(
          "--delta",
          type=float,
          default=1e-5,
          metavar="D",
          help="Target delta",
      )
      parser.add_argument(
          "--device",
          type=str,
          default="cuda",
          help="GPU ID for this process",
      )
      parser.add_argument(
          "--save-model",
          action="store_true",
          default=False,
          help="Save the trained model",
      )
      parser.add_argument(
          "--disable-dp",
          action="store_true",
          default=False,
          help="Disable privacy training and just train with vanilla SGD",
      )
      parser.add_argument(
          "--secure-rng",
          action="store_true",
          default=False,
          help="Enable Secure RNG to have trustworthy privacy guarantees. Comes at a performance cost",
      )
      parser.add_argument(
          "--data-root",
          type=str,
          default="../mnist",
          help="Where MNIST is/will be stored",
      )
      args = parser.parse_args()
      device = torch.device(args.device)
  
      train_loader = torch.utils.data.DataLoader(
          datasets.MNIST(
              args.data_root,
              train=True,
              download=True,
              transform=transforms.Compose(
                  [
                      transforms.ToTensor(),
                      transforms.Normalize((MNIST_MEAN,), (MNIST_STD,)),
                  ]
              ),
          ),
          batch_size=args.batch_size,
          num_workers=0,
          pin_memory=True,
      )
      test_loader = torch.utils.data.DataLoader(
          datasets.MNIST(
              args.data_root,
              train=False,
              transform=transforms.Compose(
                  [
                      transforms.ToTensor(),
                      transforms.Normalize((MNIST_MEAN,), (MNIST_STD,)),
                  ]
              ),
          ),
          batch_size=args.test_batch_size,
          shuffle=True,
          num_workers=0,
          pin_memory=True,
      )
      run_results = []
      for _ in range(args.n_runs):
          model = SampleConvNet().to(device)
  
          optimizer = optim.SGD(
              model.parameters(), lr=args.lr, momentum=0
          )
          privacy_engine = None
  
          if not args.disable_dp:
              privacy_engine = PrivacyEngine(
                  secure_mode=args.secure_rng
              )
              (
                  model,
                  optimizer,
                  train_loader,
              ) = privacy_engine.make_private(
                  module=model,
                  optimizer=optimizer,
                  data_loader=train_loader,
                  noise_multiplier=args.sigma,
                  max_grad_norm=args.max_per_sample_grad_norm,
              )
  
          for epoch in range(1, args.epochs + 1):
              train(
                  args,
                  model,
                  device,
                  train_loader,
                  optimizer,
                  privacy_engine,
                  epoch,
              )
          run_results.append(test(model, device, test_loader))
  
      if len(run_results) > 1:
          print(
              "Accuracy averaged over {} runs: {:.2f}% ± {:.2f}%".format(
                  len(run_results),
                  np.mean(run_results) * 100,
                  np.std(run_results) * 100,
              )
          )
  
      repro_str = (
          f"mnist_{args.lr}_{args.sigma}_"
          f"{args.max_per_sample_grad_norm}_{args.batch_size}_{args.epochs}"
      )
      torch.save(run_results, f"run_results_{repro_str}.pt")
  
      if args.save_model:
          torch.save(model.state_dict(), f"mnist_cnn_{repro_str}.pt")
  
file_path: opacus/examples/mnist.py
imports: import argparse
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  import torch.optim
  from opacus import PrivacyEngine
  from torchvision import datasets, transforms
  from tqdm import tqdm
label_desc: Use image node_id 3 for calling a global function with example usage: main() without return types