Loop over all examples in test set
for data, target in test_loader:
  # Send the data and label to the device
  data, target = data.to(device), target.to(device)
  # Set requires_grad attribute of tensor. Important for Attack
  data.requires_grad = True

  # Forward pass the data through the model
  output,lastlr,softlr = model(data,0,0)

  init_pred = output.max(1, keepdim=True)[1] # get the index of the max log-probability

  # If the initial prediction is wrong, dont bother attacking, just move on
  if init_pred.item() != target.item(): # va solo se batch_size = 1
     continue

  # Calculate the loss
  loss = F.nll_loss(output, target)
  # Zero all existing gradients
  model.zero_grad()
  # Calculate gradients of model in backward pass
  loss.backward()
  # Collect datagrad
  data_grad = data.grad.data

Thanks for the quick reply, ptrblck. The instruction data.requires_grad = True is actually there.