Getting too many values to unpack while freezing the parameters

I am trying to use transfer learning. I want to freeze the parameters of the model apart from the batch norm layers. I am using the below code but getting too many values error.

for name,param in model_transfer.parameters():
    if ("bn" not in name):
        param.requires_grad = False


ValueError                                Traceback (most recent call last)
----> 1 for name,param in model_transfer.parameters():
      2     if ("bn" not in name):
      3         param.requires_grad = False

ValueError: too many values to unpack (expected 2)

How to resolve it?

1 Like


If you want both the name and the parameter, you need to use model_transfer.named_parameters().


Python functions can return multiple variables . These variables can be stored in variables directly. This is a unique property of Python , other programming languages such as C++ or Java do not support this by default.

The valueerror: too many values to unpack occurs during a multiple-assignment where you either don’t have enough objects to assign to the variables or you have more objects to assign than variables. If for example myfunction() returned an iterable with three items instead of the expected two then you would have more objects than variables necessary to assign to.