I have the following code:
(pimg is PIL.Image)
def fgsm(src_img_file, target, model):
toTens = transforms.ToTensor()
PIL_image = pimg.open(src_img_file)
img = toTens(PIL_image)
img.unsqueeze_(0) # torch.nn takes only minibatches, so add fake batch dim
img.requires_grad_(True)

criterion = nn.CrossEntropyLoss()
target_tensor = torch.tensor([target])

model.eval()
epsilon = 0.001
for epochs in range(1, 100):
    pred = model(img)
    print ("model pred", pred.max(1)[1])
    loss = criterion(pred, target_tensor)
    loss.backward()
    img = torch.clamp(img + epsilon * torch.sign(img.grad), 0.0, 1.0) # make sure it is a valid image

after 1 iteration, I get the following error message:
Traceback (most recent call last):
File “run_mnist.py”, line 161, in 
fgsm(src_img, tgt, model)
File “run_mnist.py”, line 120, in fgsm
img = torch.clamp(img + epsilon * torch.sign(img.grad), 0.0, 1.0) # make sure it is a valid image
TypeError: sign(): argument ‘input’ (position 1) must be Tensor, not NoneType
Why does this happen? How can I fix it?