RuntimeError: 'NoneType' object is not subscriptable

Hello everyone,
I get this error when calling lossfunc(model(x), y).backward(). I can’t figure out where the problem is.
I trained the following network on CIFAR10:

from torchvision.models import vgg11

pt_vgg11 = vgg11(pretrained=False)

#print(pt_vgg11)

def seqVGG():
    
    features = nn.Sequential()
    features.add_module('features', nn.Sequential(*list(pt_vgg11.children())[:-1]))    
    features.add_module('flat', nn.Flatten())
    features.add_module('classifier', nn.Sequential(*list(pt_vgg11.children())[2]))
    
    return features

vgg11_seq = seqVGG()

(the backpack packages expects models to be sequences of PyTorch NN modules)

Now I want to calculate the diagonalHessians (for uncertainty estimates), for one date of the trainloader using the following code
(copied from BackPACK package website backpack.pt):

for batch_idx, (x, y) in enumerate(trainloader):
    X, y = x, y
    if batch_idx > 0:
        break

model = extend(vgg11_seq)
lossfunc = extend(CrossEntropyLoss())
loss = lossfunc(model(X), y)

with backpack(DiagHessian()):
    loss.backward()

for param in model.parameters():
    print(param.grad)
    print(param.diag_ggn_exact)

But I get the following error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-38-2fa093cbf3ef> in <module>()
      9 
     10 with backpack(DiagHessian()):
---> 11     loss.backward()
     12 
     13 for param in model.parameters():

~/anaconda3/lib/python3.7/site-packages/torch/tensor.py in backward(self, gradient, retain_graph, create_graph)
    196                 products. Defaults to ``False``.
    197         """
--> 198         torch.autograd.backward(self, gradient, retain_graph, create_graph)
    199 
    200     def register_hook(self, hook):

~/anaconda3/lib/python3.7/site-packages/torch/autograd/__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
     98     Variable._execution_engine.run_backward(
     99         tensors, grad_tensors, retain_graph, create_graph,
--> 100         allow_unreachable=True)  # allow_unreachable flag
    101 
    102 

RuntimeError: 'NoneType' object is not subscriptable

can anyone help me with that? if you need more information just tell me!

Are you seeing this error only when using backpack?
If so, I would recommend to create an issue in the backpack gihub repo for better visibility.

It only happens with the trained network. That means, after defining the network and initialising the weights it works. But when I try to do it after training the network it would throw the error.

Thanks for the update.
I would still recommend to post the issue in the linked GitHub repo, if the error is specific to backpack.
If that’s not the case and you are seeing the issue using PyTorch only, could you post a code snippet to reproduce this issue, please?

Thank you for the answer again. I found the solution. The network was in .eval(). Turning it into train-mode with model.train() solved the error.