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

Hello,

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:

a = b.clone()
a = nn.Parameter(a, required_grad=True)
res = model(a)

where b is a input tensor to the model
and then I did

for _, v in res.items():
     v.backward()

it causes a RuntimeError:

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)

I’m not sure if I’m using .backward() wrong or it’s the model that causes this error…

would appreciate any help

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?

I’m using this model: GitHub - mit-han-lab/pvcnn: [NeurIPS 2019, Spotlight] Point-Voxel CNN for Efficient 3D Deep Learning, pretrained on ShapeNet

I checked my code and it seems that the problem arises whenever I call model.eval() or model.train(mode=False)

Here is a snippet of my code that should be runnable when instantiated with some custom model:

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()

Which of the models in shapenet are you using and what are the input shapes to this model?

I was using shapenet/pvcnn.py with the following modified forward():

    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

The input is a [1, 6, N] tensor, where N is the number of 3d points

I did the same for shapenet/pointnet.py with the exception that the input size is [1, 3, N] and it worked somehow