I am trying to train a code, but I get this error. Here is my code:
for i in range(num_iter):
# Forward pass to get logits
logits = self(images)
# Calculate loss
loss = self.criterion(logits, masks)
# Zero gradients
self.optim.zero_grad()
# Backward pass to compute gradients
loss.backward()
images = images.detach()
images = torch.tensor(images, dtype=torch.float32, requires_grad=True)
# Get the gradients of the loss w.r.t. the input image
data_grad = images.grad.data
# Generate the perturbed image using FGSM
perturbed_images = fgsm(images, epsilon, data_grad)
# Re-classify the perturbed image
logits_perturbed = self(perturbed_images)
code is just a slice of the entire code.
The error I see is:
File "/home/xx/xx/xx.py", line 73, in training_step
data_grad = images.grad.data
AttributeError: 'NoneType' object has no attribute 'data'
Trying to train the model, expected it to train well, but it does not work.
when first creating tensor with
require_grad=True
, its gradient initial value will beNone
, which cause the attribute error when trying to access its data attribute.so you should do the following: First create trainable images, and pass it to the model as follows