RuntimeError: 'NoneType' object is not subscriptable

morrohype: <p>Hello everyone,<br>
I get this error when calling lossfunc(model(x), y).backward(). I can’t figure out where the problem is.<br>
I trained the following network on CIFAR10:</p>
<pre><code class="lang-auto">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()
</code></pre>
<p>(the backpack packages expects models to be <a href="https://pytorch.org/docs/stable/nn.html#sequential" rel="nofollow noopener">sequences</a> of <a href="https://pytorch.org/docs/stable/nn.html" rel="nofollow noopener">PyTorch NN modules</a>)</p>
<p>Now I want to calculate the diagonalHessians (for uncertainty estimates), for one date of the trainloader using the following code<br>
(copied from  BackPACK package website <a href="http://backpack.pt" rel="nofollow noopener">backpack.pt</a>):</p>
<pre><code class="lang-auto">for batch_idx, (x, y) in enumerate(trainloader):
    X, y = x, y
    if batch_idx &gt; 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)
</code></pre>
<p>But I get the following error:</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-38-2fa093cbf3ef&gt; in &lt;module&gt;()
      9 
     10 with backpack(DiagHessian()):
---&gt; 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         """
--&gt; 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,
--&gt; 100         allow_unreachable=True)  # allow_unreachable flag
    101 
    102 

RuntimeError: 'NoneType' object is not subscriptable

</code></pre>
<p>can anyone help me with that? if you need more information just tell me!</p>

ptrblck: <p>Are you seeing this error only when using <code>backpack</code>?<br>
If so, I would recommend to create an issue in <a href="https://github.com/f-dangel/backpack/issues">the backpack gihub repo</a> for better visibility.</p>

morrohype: <p>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.</p>

ptrblck: <p>Thanks for the update.<br>
I would still recommend to post the issue in the linked GitHub repo, if the error is specific to <code>backpack</code>.<br>
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?</p>

morrohype: <p>Thank you for the answer again. I found the solution. The network was in <code>.eval()</code>. Turning it into train-mode with <code>model.train()</code> solved the error.</p>