Thank you for your reply. I want to be more explicit that, if (x_var, y_var) is given as a batch, then:
scores = self.model(x_var)
loss = self.loss_fn(scores, y_var)
loss.backward()

will only compute loss as a single number to be backpropagated to the input images in the batch. However, I think the perturbation on an image should be generated from its own loss. The loss should be computed by each individual image to be backpropagated. In this manner the loss should be a vector (whose length = batch size).
If this is true I guess we can still compute scores as above (which supports batching), but is there a good way to vectorize the loss computation and backpropagation in PyTorch?