Assert all(tensors[0].size(0) == tensor.size(0) for tensor in tensors) AttributeError: 'list' object has no attribute 'size'

terminator9487: <p>Hello， I am new in using pytorch to build NN. Recently, I am trying to formalize my data using data_utils.TensorDataset. However, this following error was appeared. I do wrap the data with tensor in my code and put it into the Variable.  Unfortunately, it didn’t work at all. Are you familiar with this problem？ I really appreciate for yr comment. Here is the code and error information.<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/d/d267ca3cb2f318281dce719e96a434f3f8b56e6b.png" data-download-href="https://discuss.pytorch.org/uploads/default/d267ca3cb2f318281dce719e96a434f3f8b56e6b" title="Screenshot%20from%202018-07-27%2016-36-27"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/d/d267ca3cb2f318281dce719e96a434f3f8b56e6b_2_690x83.png" alt="Screenshot%20from%202018-07-27%2016-36-27" data-base62-sha1="u1kDxMfz7rrzCByUrcyIJWYTuAP" width="690" height="83" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/d/d267ca3cb2f318281dce719e96a434f3f8b56e6b_2_690x83.png, https://discuss.pytorch.org/uploads/default/original/2X/d/d267ca3cb2f318281dce719e96a434f3f8b56e6b.png 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/d/d267ca3cb2f318281dce719e96a434f3f8b56e6b.png 2x" data-dominant-color="F6F5F3"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot%20from%202018-07-27%2016-36-27</span><span class="informations">797×97 14.1 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>y_vals_Valence = np.array([0 if each==‘Neg’  else 1 if each ==‘Neu’ else 2 for each in data[‘all_data’][:,320]])<br>
y_vals_Arousal = np.array([3 if each==‘Pas’  else 4 if each ==‘Neu’ else 5 for each in data[‘all_data’][:,321]])</p>
<p>DEAP_x_train = x_vals[:-256] <span class="hashtag-raw">#using</span> 80% of whole data for training<br>
DEAP_x_train_torch = DEAP_x_train.astype(float)<br>
DEAP_x_train_torch = torch.from_numpy(DEAP_x_train_torch).double()</p>
<p>DEAP_x_test = x_vals[-256:]  <span class="hashtag-raw">#using</span> 20% of whole data for testing<br>
DEAP_x_test_torch = DEAP_x_test.astype(float)<br>
DEAP_x_test_torch = torch.from_numpy(DEAP_x_test_torch).double()</p>
<p>DEAP_y_train = y_vals_Valence[:-256]#<span class="hashtag-raw">#Valence</span><br>
DEAP_y_train_torch = DEAP_y_train.astype(float)<br>
DEAP_y_train_torch = torch.from_numpy(DEAP_y_train_torch).double()</p>
<p>DEAP_y_test = y_vals_Valence[-256:]<br>
DEAP_y_test_torch = DEAP_y_test.astype(float)<br>
DEAP_y_test_torch = torch.from_numpy(DEAP_y_test_torch).double()</p>
<p>DEAP_y_train_A = y_vals_Arousal[:-256] ### Arousal<br>
DEAP_y_train_torch_A = DEAP_y_train_A.astype(float)<br>
DEAP_y_train_torch_A = torch.from_numpy(DEAP_y_train_torch_A).double()</p>
<p>DEAP_y_test_A = y_vals_Arousal[-256:]<br>
DEAP_y_test_torch_A = DEAP_y_test_A.astype(float)<br>
DEAP_y_test_torch_A = torch.from_numpy(DEAP_y_test_torch_A).double()</p>
<p>batch_size =50;</p>
<h1><a name="data-loader-input-pipeline-1" class="anchor" href="#data-loader-input-pipeline-1"></a>Data Loader (Input Pipeline)</h1>
<p>train = data_utils.TensorDataset(DEAP_x_train_torch, [DEAP_y_train_torch])  ##()</p>
<p>train_loader = data_utils.DataLoader(train, batch_size=batch_size, shuffle=True)</p>
<p>test = data_utils.TensorDataset(DEAP_x_test_torch, [DEAP_y_test_torch])<br>
test_loader = data_utils.DataLoader(test, batch_size=batch_size, shuffle=False)</p>

ptrblck: <p>Try to remove the bracket enclosing the target tensor.</p>

terminator9487: <p>Thanks for yr comment. But, I couldn’t follow u because I am new in this field. what’s exactly the bracket you mentioned？the code that transfer the numpy to torch or？</p>

ptrblck: <p>Sorry, I thought I’ve quoted the line of code.<br>
Here it is:</p>
<pre><code class="lang-python">train = data_utils.TensorDataset(DEAP_x_train_torch, [DEAP_y_train_torch]) 
</code></pre>
<p>While you are passing the data tensor directly, you enclose the target tensor with brackets.<br>
Could you remove these brackets and try it again?</p>

terminator9487: <p>Sure, I see. the reason i used the brackets here is  my labels are 2-D. the corresponding information is in this link.   <a href="https://stackoverflow.com/questions/41924453/pytorch-how-to-use-dataloaders-for-custom-datasets" class="inline-onebox" rel="noopener nofollow ugc">python - PyTorch: How to use DataLoaders for custom Datasets - Stack Overflow</a>                For now, I was just testing only one label. Anyway, I removed the brackets and tried again. the error is like this:<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/3/353cf1a30cedd06dd2900ad8db71e5b199259e25.png" data-download-href="https://discuss.pytorch.org/uploads/default/353cf1a30cedd06dd2900ad8db71e5b199259e25" title="Screenshot%20from%202018-07-28%2017-01-22"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/3/353cf1a30cedd06dd2900ad8db71e5b199259e25_2_690x105.png" alt="Screenshot%20from%202018-07-28%2017-01-22" data-base62-sha1="7AXSpAAZMzfiWCnYvTFuLYvKqB7" width="690" height="105" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/3/353cf1a30cedd06dd2900ad8db71e5b199259e25_2_690x105.png, https://discuss.pytorch.org/uploads/default/original/2X/3/353cf1a30cedd06dd2900ad8db71e5b199259e25.png 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/3/353cf1a30cedd06dd2900ad8db71e5b199259e25.png 2x" data-dominant-color="F6F5F4"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot%20from%202018-07-28%2017-01-22</span><span class="informations">800×122 16.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>I guess your data is of type <code>torch.DoubleTensor</code>.<br>
If you need double precision, you should cast your model using <code>model = model.double()</code>.<br>
The common case is to use <code>float</code> so you could try to cast your data using</p>
<pre><code>train = data_utils.TensorDataset(DEAP_x_train_torch.float(), DEAP_y_train_torch)</code></pre>

terminator9487: <p>yes, i couldn’t agree more. and, I wanna use double precision. this error occurred after adding model = model.double(）.<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/7/7726c4856bfb686c10050be41f196b86d3356fad.png" data-download-href="https://discuss.pytorch.org/uploads/default/7726c4856bfb686c10050be41f196b86d3356fad" title="Screenshot%20from%202018-07-30%2010-35-49"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/7/7726c4856bfb686c10050be41f196b86d3356fad_2_690x109.png" alt="Screenshot%20from%202018-07-30%2010-35-49" data-base62-sha1="h03SseL8PcscxvNXeqehxAKKEqh" width="690" height="109" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/7/7726c4856bfb686c10050be41f196b86d3356fad_2_690x109.png, https://discuss.pytorch.org/uploads/default/original/2X/7/7726c4856bfb686c10050be41f196b86d3356fad.png 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/7/7726c4856bfb686c10050be41f196b86d3356fad.png 2x" data-dominant-color="F4F3F2"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot%20from%202018-07-30%2010-35-49</span><span class="informations">803×128 18.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

terminator9487: <p>So I  guess the torch should be changed to long type. another error occurred after adding     train = data_utils.TensorDataset(DEAP_x_train_torch.long(), DEAP_y_train_torch.long（）)  <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/2/242cbad193424c6fe2aa838e1abf213dba35d3bf.png" data-download-href="https://discuss.pytorch.org/uploads/default/242cbad193424c6fe2aa838e1abf213dba35d3bf" title="Screenshot%20from%202018-07-30%2010-41-06"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/2/242cbad193424c6fe2aa838e1abf213dba35d3bf_2_690x89.png" alt="Screenshot%20from%202018-07-30%2010-41-06" data-base62-sha1="5a11mAADnEHgZK0sIh6wB7Kr0yr" width="690" height="89" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/2/242cbad193424c6fe2aa838e1abf213dba35d3bf_2_690x89.png, https://discuss.pytorch.org/uploads/default/original/2X/2/242cbad193424c6fe2aa838e1abf213dba35d3bf.png 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/2/242cbad193424c6fe2aa838e1abf213dba35d3bf.png 2x" data-dominant-color="F3F3F2"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot%20from%202018-07-30%2010-41-06</span><span class="informations">807×105 17.2 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div>  Now, I am confused. Could you tell me where is wrong？Thanks a lot!</p>

ptrblck: <p><code>nn.NLLLoss</code> and <code>nn.CrossEntropyLoss</code> both require <code>torch.LongTensors</code> as the target input.<br>
Cast your targets to <code>.long()</code> and try it again.</p>

terminator9487: <p>sorry, I couldn’t understand it. I have already cast my input feature and label by train = data_utils.TensorDataset(DEAP_x_train_torch.long(), DEAP_y_train_torch.long（）)     but it’s still  wrong. Did I get correct understanding what you mentioned before?</p>

ptrblck: <p>Sorry for the confusion. I didn’t explained your last error properly.</p>
<p>Your training data should be of type <code>float</code>, while your target should be <code>long</code>.<br>
Could you change your dataset to</p>
<pre><code class="lang-python"> train = data_utils.TensorDataset(DEAP_x_train_torch.float(), DEAP_y_train_torch.long())
</code></pre>
<p>and run it again?</p>

terminator9487: <p>Thanks for yr patience. I thought this feature data should have the same type with the label, right？ Anyway, I just tried it again. However, it still didn’t work. the error is as follow：<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/c/c0a2377b73ca6124240d7a234611e619a391b676.png" data-download-href="https://discuss.pytorch.org/uploads/default/c0a2377b73ca6124240d7a234611e619a391b676" title="Screenshot%20from%202018-08-01%2011-55-40"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/c/c0a2377b73ca6124240d7a234611e619a391b676_2_690x84.png" alt="Screenshot%20from%202018-08-01%2011-55-40" data-base62-sha1="ru7dLBAnM1xI0oC8vsapS0jiiHA" width="690" height="84" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/c/c0a2377b73ca6124240d7a234611e619a391b676_2_690x84.png, https://discuss.pytorch.org/uploads/default/original/2X/c/c0a2377b73ca6124240d7a234611e619a391b676.png 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/c/c0a2377b73ca6124240d7a234611e619a391b676.png 2x" data-dominant-color="F5F4F4"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot%20from%202018-08-01%2011-55-40</span><span class="informations">806×99 14.3 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>No worries.<br>
I forgot you wanted to use your model in double precision.<br>
Try the following:</p>
<pre><code class="lang-python">train = data_utils.TensorDataset(DEAP_x_train_torch.double(), DEAP_y_train_torch.long())
model = model.double()
</code></pre>
<p>No, the types of data and target depend on the loss function you are using.<br>
In a classification use case you usually use floats for your data and long for the target.<br>
You can find more information in the <a href="https://pytorch.org/docs/stable/nn.html#loss-functions">docs</a>.</p>

terminator9487: <p>Thanks for yr replying. It finally works. But, it still existed the problem. How come the accuracy is so low when using Multi-layer perceptron？ here is the code and result. Do you have any idea the reason of that? BTW， I was also using the SVM for classification model. The result is about 45%.</p>
<pre><code>class Net(nn.Module):
    def __init__(self):
    super(Net, self).__init__()
    self.l1 = nn.Linear(320, 512)
    self.l2 = nn.Linear(512, 1024)
    self.l3 = nn.Linear(1024, 512)
    self.l4 = nn.Linear(512, 512)
    self.l5 = nn.Linear(512, 3)

def forward(self, x):
    x = F.relu(self.l1(x))
    x = F.relu(self.l2(x))
    x = F.relu(self.l3(x))
    x = F.relu(self.l4(x))
    return F.log_softmax(self.l5(x), dim=1)
    #return self.l5(x)

    model = Net()
    model = model.double()

   optimizer = optim.SGD(model.parameters(), lr=0.0001, momentum=0.5)
</code></pre>
<p><img src="https://discuss.pytorch.org/uploads/default/original/2X/a/a394e0e057c1ebdee8d1c87af9f75046231ed882.png" alt="Screenshot%20from%202018-08-02%2017-43-51" data-base62-sha1="nl6NuptbVAE6JRAn7yl7cDDmAiS" width="459" height="333"></p>

ptrblck: <p>What kind of data do you have and what is your expected accuracy?</p>

terminator9487: <p>the feature is extracting from DEAP data set using HOC，and the label is also in this dataset. the expected accuracy is about 60%.</p>

Ahmad_Altameemi: <p>Hello ,can someone helps me ,when I run my code I got this error<br>
AttributeError: ‘list’ object has no attribute ‘size’<br>
<span class="hashtag">#rpm</span><br>
for epoch in range(300):<br>
for x,y in enumerate(train_loader):<br>
y = torch.zeros(y.size(0),4).scatter_(1,y.view(-1,1),1.)<br>
x,y = x.to(device),y.to(device)<br>
output,_ = rpm_nn(x,y) # cnn output<br>
loss = loss_func(output) # cross entropy loss# clear gradients for this training step<br>
optimizer_rpm.zero_grad()<br>
loss.backward()<br>
optimizer_rpm.step()<br>
test_out,_=rpm_nn(test_x,test_y)<br>
pred_z=torch.max(test_out,1)[1].cpu().data.numpy()<br>
accuracy = float((pred_z == test_z.cpu().numpy()).astype(int).sum()) / float(test_y.size(0))<br>
print('Epoch: ', epoch, ‘| train loss: %.4f’ % loss.cpu().data.numpy(), ‘| test accuracy: %.2f’ % accuracy)</p>