I am attempting to implement adversarial training for the Fast Gradient Sign Method, which is a widely-used adversarial attack (see section 6 of [1412.6572] Explaining and Harnessing Adversarial Examples 1). This involves creating a custom loss function which includes a gradient operation. What is the proper way to do this? I don’t believe it is sufficient to use .backward inside the loss computation, because this would set the .grad attribute on all my tensors as a side effect.
From reading the documentation, it seems that the function I need is torch.autograd.grad, but the docs don’t clearly specify what side effects this function has. Will it modify the .grad attributes of my tensors?