'NoneType' object has no attribute 'data' for some variables

I’m trying to get gradients of the loss function with respect to some variables in my code. I’m having trouble understanding why I can access the gradients for some variables and why not for some others.Here is a part of my code:

    pert=I+delta
    pert=torch.clamp(pert,0,1)
    I=Variable(I.to(device), requires_grad = True) 
    delta=Variable(delta.to(device), requires_grad = True) 
    pert=Variable(pert.to(device), requires_grad = True) 
    outputs = net(pert)
    targets=targets.long()  
    loss = criterion(outputs, targets)
    loss.backward()
    grad=pert.grad.data
    graddel=delta.grad.data
    gradI=I.grad.data

I can get the gradients for grad, but for graddel and gradI I get the error:
‘NoneType’ object has no attribute ‘data’

Any ideas?

It seems that tensor I and delta are not wrapped into the computational graph, thus their grad is None.
I have a dummy code here to explain it

a = torch.randn(3,3,)
b = torch.randn(3,3,)
c = a + b
a.requires_grad_()
b.requires_grad_()
c.requires_grad_()
c.sum().backward()

a.grad -> None
b.grad -> None

But you set requires_grad_() at first, like

a = torch.randn(3,3, requires_grad=True)

the grad of pert will be None, and the grad of delta and I will be None. Because here pert is non-leaf tensor, its gradient will not be stored.