How does one implement adversarial examples in pytorch?

I wanted to reproduce:

from the paper https://arxiv.org/pdf/1312.6199.pdf. I was wondering, how does one actually implement this in pytorch? My main confusion is that for loss_f I am using a torch.nn.CrossEntropy() criterion for example. Do i just need to change the code that I already have from:

loss = criterion(outputs+r, labels)
loss.backward()

to:

loss = criterion(outputs+r, labels)
loss = loss + c * r.norm(2)
loss.backward()

or something along those lines. I know its not quite right cuz I did not explicitly show how I implemented x+r or the hypercube constraint but those are parts that I still need to figure out.


1 Like

probably not forget to include variable r in optimum/optimizer.