ValueError: need more than 1 value to unpack

so sorry for my pool english and thank u for watching my question

i set batchsize=2
so after nn.Linear i get a 2x24 vector to use for my Classification task1,
now i want to get a 2X8 vector from the 2X24 vector for my Classification task2,
i used split cat sum and so on to achieve the purpose
but when i run the code ,It can run for a while and then report an error
Traceback (most recent call last):
File “”, line 65, in
File “/home/fp/feedback/pytorch_feedback-network-master/utils/”, line 113, in train
File “/home/fp/feedback/pytorch_feedback-network-master/utils/”, line 184, in _train_one_epoch
outputs1,outputs2 = self.model(inputs) ##4x2x24
File “/home/fp/.conda/envs/toold/lib/python2.7/site-packages/torch/nn/modules/”, line 357, in call
result = self.forward(*input, **kwargs)
File “/home/fp/.conda/envs/toold/lib/python2.7/site-packages/torch/nn/parallel/”, line 71, in forward
return self.module(*inputs[0], **kwargs[0])
File “/home/fp/.conda/envs/toold/lib/python2.7/site-packages/torch/nn/modules/”, line 357, in call
result = self.forward(*input, **kwargs)
File “/home/fp/feedback/pytorch_feedback-network-master/network/”, line 75, in forward
ValueError: need more than 1 value to unpack

here is my code in network forward
x_finished = []
mx_finished = []

    for x_i in x_all:
        x_i = F.relu(x_i) 
        x_i =self.avg_pool(x_i)          
        x_i=self.soft(x_i)     ''now x_i is a 2x24 vector ''

        b1=b1*w               "w is  a constant tensor size 8X24 to help me transform the 1X24 to 1x8 "
        b2=b2*w               ’‘after b1=b1*w   b1 from 1x24 to 8X24""
        b1=torch.sum(b1,1)                  ""b1: 8x24 -> 8""
        b1=b1.view(1,-1)                      ''b1 : 8 -> 1x8''
        b2=b2.view(1,-1),b2),0)                   ""get x_i  2x8 size''

    return x_finished,mx_finished

because my network is based on CONVLSTM so x_finished have four 2x24
mx_finished have four 2x8
I think my code logic is smooth, but I don’t know why I get an error

I guess the last batch could be smaller than the other ones, thus x_i might be e.g. [1, 24] instead of the expected [2, 24].
Could you add a print statement right before the .split call and check the shapes during training, and which shape x_i has before throwing this error?

A workaround could be to drop the last smaller batch via drop_last=True in your DataLoader.

so thanks to u
u are right , the error caused by the last batch could be smaller than the other ones
on the other hand i found some new questions want to ask u,
in my code the b1,b2 is the ‘’ for loop " s local variable,
should i set them as the Variable tensor in cuda before my network is working?
e.g. : b1=torch.randn(2,24)

another question is:
i use split and cat to achieve “2x24 -->2X8”
i mean the split and cat seems like not a basic mathematical operations
so when the back propagation is running , it looks like the grad update will be effected?

last one is:
i want to use the 2x24 vector get my loss1 and use the 2X8 vector get my loss2,
so the code for the part of "2X24 turn to 2X8 " should put in my network 's def forward
or in my every train epoch
(i mean the network just return the 2X24 and get 2X8 ,loss1,loss2 in the training
or the network return the 2X24 ,2X8 and get loss1,loss2 in the training )

Thank you very much for helping me

No, Variables are deprecated since PyTorch 0.4.0, so you can just use tensors in newer versions.

torch.split and won’t detach the tensors from the computation graph and the backward call will still work.

It doesn’t matter where these operations are called. As long as these ops are called somewhere, the results will be the same.
I would personally put these operations into the model and just return the output, which can be passed to a criterion, but your coding style might differ. :wink:

yeah i know and my study code is based on 0.3.0 now i have not much time to change the version:pleading_face:
i want to know outside "the for loop " should the b1,b2 need be defined as tensor such as:b1=torch.randn(1,24)

or just as a local value in “the for loop”

other question i already know what u mean so appreciated:kissing_heart:

You don’t need to define b1 and b2 before the for loop, if you create them via the split inside the loop.

thank u
i recently read some literature their code is based on pytorch 0.4.0 or 0.4.1
do u have some suggestions about the chioce between pytorch 0.4 and 1.0 or newer versions?

I would strongly recommend to use the latest stable release, which is 1.3.1 at the moment.
Otherwise you might run into legacy issues, which were already solved. Also, you’ll get all new features.

ok as a college student now i get ready for my new trip to e study pytorch
thank u for your help and patience all the time~

Sure! Feel free to post your questions in this board (or search for related questions), in case you get stuck. :wink: