AttributeError: 'NoneType' object has no attribute 'data', Training error while fgsm attack

699 Views Asked by At

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.

1

There are 1 best solutions below

0
On

when first creating tensor with require_grad=True, its gradient initial value will be None, which cause the attribute error when trying to access its data attribute.

AttributeError: 'NoneType' object has no attribute 'data'

so you should do the following: First create trainable images, and pass it to the model as follows

for i in range(num_iter):
            # Forward pass to get logits
            tr_images = torch.tensor(images, dtype=torch.float32, requires_grad=True) 
             # create trainable images(requires_grad=True)

            logits = self(tr_images)
            # Calculate loss
            loss = self.criterion(logits, masks)

            # Zero gradients
            self.optim.zero_grad() 

            # Backward pass to compute gradients
            loss.backward()


            # Get the gradients of the loss w.r.t. the input image
            data_grad = tr_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)