'NoneType' object has no attribute 'data'


(Peter Ham) #1

I’m trying to perform gradient backward propagation like this,

    this_out = model(this_inp)
    model.zero_grad()
    this_diff = torch.randn(1,1)
    this_diff[0] = 1.0
    this_diff = this_diff.double()
    this_out.backward(this_diff)
    for f in model.parameters():
            _ = f.data.add_(f.grad.data * learning_rate)

and it says “f.grad” data is a NoneType


(Peter Ham) #2

Sorry I just found that when I define the network, there are some layers not used in forward pass (which means they are not included in the network). Delete these unused layers helps to solve the problem. But why should we use all layers defined in the init function? Won’t PyTorch automatically ignore these layers?


#3

Thank you for sharing the error information and solution, which really helped me to solve my problem.


(ahsueh1996) #4

Worked for me too! Thanks!