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