'NoneType' object has no attribute 'data'

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

2 Likes

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?

4 Likes

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

Worked for me too! Thanks!

Hi Thank you for share your experience! It helped me solve my problem! So happy!!!

thanks for your question and answer