RuntimeError: not enough values to unpack when running .backward()

HX-tfd0: <p>Hello,</p>
<p>I’m using a pretrained model with its parameters frozen. The output of the model is a dictionary containing the output of the intermediate network layers. Now I do the following:</p>
<pre><code class="lang-auto">a = b.clone()
a = nn.Parameter(a, required_grad=True)
res = model(a)
</code></pre>
<p>where <code>b</code> is a input tensor to the model<br>
and then I did</p>
<pre><code class="lang-auto">for _, v in res.items():
     v.backward()
</code></pre>
<p>it causes a RuntimeError:</p>
<pre><code class="lang-auto">File ".../lib/python3.8/site-packages/torch/tensor.py", line 198, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph)
File ".../lib/python3.8/site-packages/torch/autograd/__init__.py", line 98, in backward
Variable._execution_engine.run_backward(
RuntimeError: not enough values to unpack (expected 2, got 0)
</code></pre>
<p>I’m not sure if I’m using <code>.backward()</code> wrong or it’s the model that causes this error…</p>
<p>would appreciate any help</p>

ptrblck: <p>Based on the stacktrace it seems the error is raised inside the backward method. Could you post a minimal, executable code snippet which would reproduce this issue, please?</p>

HX-tfd0: <p>I’m using this model: <a href="https://github.com/mit-han-lab/pvcnn" class="inline-onebox" rel="noopener nofollow ugc">GitHub - mit-han-lab/pvcnn: [NeurIPS 2019, Spotlight] Point-Voxel CNN for Efficient 3D Deep Learning</a>, pretrained on ShapeNet</p>
<p>I checked my code and it seems that the problem arises whenever I call <code>model.eval()</code> or <code>model.train(mode=False)</code></p>
<p>Here is a snippet of my code that should be runnable when instantiated with some custom model:</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn

model = ...
(maybe load state dict)
model.eval()
for p in model.parameters():
    p.requires_grad = False

a_= (create random tensor)
c = a_.clone()
c = nn.Parameter(c)

for _, v in model(c).items():
    v.sum().backward()
</code></pre>

ptrblck: <p>Which of the models in <code>shapenet</code> are you using and what are the input shapes to this model?</p>

HX-tfd0: <p>I was using <code>shapenet/pvcnn.py</code> with the following modified forward():</p>
<pre><code class="lang-auto">    def forward(self, inputs):
        # inputs : [B, in_channels + S, N]
        features = inputs[:, :self.in_channels, :] 
        num_points = features.size(-1)

        # prepare feature extraction for NST
        feats = {}

        coords = features[:, :3, :]
        out_features_list = [one_hot_vectors]
        for i in range(len(self.point_features)):
            features, _ = self.point_features[i]((features, coords)) # takes 6d (feat), 3d (coord) array
            feats["point_feature_{}".format(i)] = features

        # max pool out features to global feature
        global_feature = features.max(dim=-1, keepdim=True).values.repeat([1, 1, num_points])
        feats["global_feature_{}".format(i)] = global_feature

        return feats

</code></pre>
<p>The input is a [1, 6, N] tensor, where N is the number of 3d points</p>

HX-tfd0: <p>I did the same for <code>shapenet/pointnet.py</code> with the exception that the input size is [1, 3, N] and it worked somehow</p>