ValueError: need more than 1 value to unpack

username: fupanbo
cooked: <p>so sorry for my  pool english and thank u for watching my question</p>
<p>i set batchsize=2<br>
so after nn.Linear i  get a 2x24 vector to use for my Classification task1,<br>
now i want to get a 2X8 vector from the 2X24 vector  for my Classification task2,<br>
i used split cat sum and so on to achieve the purpose<br>
but when i run the code ,It can run for a while and then report an error<br>
{{{<br>
Traceback (most recent call last):<br>
File “FeedbackNet_train.py”, line 65, in <br>
trainer.train()<br>
File “/home/fp/feedback/pytorch_feedback-network-master/utils/Trainer.py”, line 113, in train<br>
self._train_one_epoch()<br>
File “/home/fp/feedback/pytorch_feedback-network-master/utils/Trainer.py”, line 184, in _train_one_epoch<br>
outputs1,outputs2 = self.model(inputs)      #<span class="hashtag">#4x2x24</span><br>
File “/home/fp/.conda/envs/toold/lib/python2.7/site-packages/torch/nn/modules/module.py”, line 357, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)<br>
File “/home/fp/.conda/envs/toold/lib/python2.7/site-packages/torch/nn/parallel/data_parallel.py”, line 71, in forward<br>
return self.module(*inputs[0], **kwargs[0])<br>
File “/home/fp/.conda/envs/toold/lib/python2.7/site-packages/torch/nn/modules/module.py”, line 357, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)<br>
File “/home/fp/feedback/pytorch_feedback-network-master/network/feedbacknet.py”, line 75, in forward<br>
b1,b2=x_i.split(1,0)<br>
ValueError: need more than 1 value to unpack<br>
}}}</p>
<p>here is my code in network forward<br>
x_finished = []<br>
mx_finished = []</p>
<pre><code>    for x_i in x_all:
        x_i = F.relu(x_i) 
        x_i =self.avg_pool(x_i)          
        x_i=x_i.view(x.size()[0],-1)
        x_i=self.output(x_i)           
        
        x_finished.append(x_i)
         
        x_i=self.soft(x_i)     ''now x_i is a 2x24 vector ''

        b1,b2=x_i.split(1,0)
        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 -&gt; 8""
        b1=b1.view(1,-1)                      ''b1 : 8 -&gt; 1x8''
        b2=torch.sum(b2,1)
        b2=b2.view(1,-1)
        x_i=torch.cat((b1,b2),0)                   ""get x_i  2x8 size''
        mx_finished.append(x_i)

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

username: ptrblck
cooked: <p>I guess the last batch could be smaller than the other ones, thus <code>x_i</code> might be e.g. <code>[1, 24]</code> instead of the expected <code>[2, 24]</code>.<br>
Could you add a print statement right before the <code>.split</code> call and check the shapes during training, and which shape <code>x_i</code> has before throwing this error?</p>
<p>A workaround could be to drop the last smaller batch via <code>drop_last=True</code> in your <code>DataLoader</code>.</p>

username: fupanbo
cooked: <p>so thanks to u<br>
u are right , the error  caused by  the last batch could be smaller than the other ones<br>
on the other hand i found some new questions want to ask u,<br>
in my code  the b1,b2 is the  ‘’ for loop " s  local variable,<br>
should i set them  as the Variable tensor in cuda before my network is working?<br>
e.g.  :  b1=torch.randn(2,24)<br>
b1=Variable(b1)<br>
b1=b1.cuda()</p>
<p>another question  is:<br>
i use split and cat  to achieve “2x24 --&gt;2X8”<br>
i mean the split and cat seems like not a basic mathematical operations<br>
so when the back propagation is running , it looks like the grad update will be effected?</p>
<p>last one is:<br>
i want to use the 2x24 vector get my loss1  and use the 2X8 vector get my loss2,<br>
so the code for the part of  "2X24 turn to 2X8 " should put in my  network 's  def forward<br>
or in my every train epoch<br>
(i mean the network just return the 2X24 and get 2X8 ,loss1,loss2 in the training<br>
or the network return the 2X24 ,2X8 and get loss1,loss2 in the training  )</p>
<p>Thank you very much for helping me</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="fupanbo" data-post="3" data-topic="64697">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/f/ecae2f/48.png" class="avatar"> fupanbo:</div>
<blockquote>
<p>should i set them as the Variable</p>
</blockquote>
</aside>
<p>No, <code>Variables</code> are deprecated since PyTorch <code>0.4.0</code>, so you can just use tensors in newer versions.</p>
<aside class="quote no-group" data-username="fupanbo" data-post="3" data-topic="64697">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/f/ecae2f/48.png" class="avatar"> fupanbo:</div>
<blockquote>
<p>when the back propagation is running , it looks like the grad update will be effected?</p>
</blockquote>
</aside>
<p><code>torch.split</code> and <code>torch.cat</code> won’t detach the tensors from the computation graph and the backward call will still work.</p>
<aside class="quote no-group" data-username="fupanbo" data-post="3" data-topic="64697">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/f/ecae2f/48.png" class="avatar"> fupanbo:</div>
<blockquote>
<p>so the code for the part of "2X24 turn to 2X8 " should put in my network 's def forward<br>
or in my every train epoch</p>
</blockquote>
</aside>
<p>It doesn’t matter where these operations are called. As long as these ops are called somewhere, the results will be the same.<br>
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. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>

username: fupanbo
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="4" data-topic="64697">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>No, <code>Variables</code> are deprecated since PyTorch <code>0.4.0</code> , so you can just use tensors in newer versions.</p>
</blockquote>
</aside>
<p>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:<br>
i want to know  outside "the for loop "   should the  b1,b2 need be  defined as tensor such as:b1=torch.randn(1,24)</p>
<p>or  just as a local value  in “the for loop”</p>
<p>other question i already know what u mean   so appreciated:kissing_heart:</p>

username: ptrblck
cooked: <p>You don’t need to define <code>b1</code> and <code>b2</code> before the for loop, if you create them via the <code>split</code> inside the loop.</p>

username: fupanbo
cooked: <p>thank u<br>
i recently read some literature their code is based on pytorch 0.4.0 or 0.4.1<br>
do u have some suggestions about the chioce between pytorch 0.4 and 1.0 or newer versions?</p>

username: ptrblck
cooked: <p>I would strongly recommend to use the latest stable release, which is <code>1.3.1</code> at the moment.<br>
Otherwise you might run into legacy issues, which were already solved. Also, you’ll get all new features.</p>

username: fupanbo
cooked: <p>ok   as a college student now i get ready for  my new trip to e study pytorch<br>
thank u for your help and patience all the time~</p>

username: ptrblck
cooked: <p>Sure! Feel free to post your questions in this board (or search for related questions), in case you get stuck. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>