The GAN model we'll use for generating adversarial examples is largely borrowed from https://github.com/mathcbc/advGAN_pytorch. Let's create two files named advGAN.py and models.py and put the following code in these files:
- advGAN.py: Within this file, you will see the following:
import torch.nn as nnimport torchimport numpy as npimport modelsimport torch.nn.functional as Fimport torchvisionimport osdef weights_init(m): classname = m.__class__.__name__ if classname.find('Conv') != -1: nn.init.normal_(m.weight.data, 0.0, 0.02) elif classname.find('BatchNorm') != -1: nn.init.normal_(m.weight.data, 1.0, 0.02) nn.init.constant_(m.bias.data, 0)class AdvGAN_Attack: def __init__(self, device, model, model_num_labels, ...