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

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.

y_vals_Valence = np.array([0 if each==‘Neg’ else 1 if each ==‘Neu’ else 2 for each in data[‘all_data’][:,320]])
y_vals_Arousal = np.array([3 if each==‘Pas’ else 4 if each ==‘Neu’ else 5 for each in data[‘all_data’][:,321]])

DEAP_x_train = x_vals[:-256] #using 80% of whole data for training
DEAP_x_train_torch = DEAP_x_train.astype(float)
DEAP_x_train_torch = torch.from_numpy(DEAP_x_train_torch).double()

DEAP_x_test = x_vals[-256:] #using 20% of whole data for testing
DEAP_x_test_torch = DEAP_x_test.astype(float)
DEAP_x_test_torch = torch.from_numpy(DEAP_x_test_torch).double()

DEAP_y_train = y_vals_Valence[:-256]##Valence
DEAP_y_train_torch = DEAP_y_train.astype(float)
DEAP_y_train_torch = torch.from_numpy(DEAP_y_train_torch).double()

DEAP_y_test = y_vals_Valence[-256:]
DEAP_y_test_torch = DEAP_y_test.astype(float)
DEAP_y_test_torch = torch.from_numpy(DEAP_y_test_torch).double()

DEAP_y_train_A = y_vals_Arousal[:-256] ### Arousal
DEAP_y_train_torch_A = DEAP_y_train_A.astype(float)
DEAP_y_train_torch_A = torch.from_numpy(DEAP_y_train_torch_A).double()

DEAP_y_test_A = y_vals_Arousal[-256:]
DEAP_y_test_torch_A = DEAP_y_test_A.astype(float)
DEAP_y_test_torch_A = torch.from_numpy(DEAP_y_test_torch_A).double()

batch_size =50;

Data Loader (Input Pipeline)

train = data_utils.TensorDataset(DEAP_x_train_torch, [DEAP_y_train_torch]) ##()

train_loader = data_utils.DataLoader(train, batch_size=batch_size, shuffle=True)

test = data_utils.TensorDataset(DEAP_x_test_torch, [DEAP_y_test_torch])
test_loader = data_utils.DataLoader(test, batch_size=batch_size, shuffle=False)

Try to remove the bracket enclosing the target tensor.

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?

Sorry, I thought I’ve quoted the line of code.
Here it is:

train = data_utils.TensorDataset(DEAP_x_train_torch, [DEAP_y_train_torch]) 

While you are passing the data tensor directly, you enclose the target tensor with brackets.
Could you remove these brackets and try it again?

Sure, I see. the reason i used the brackets here is my labels are 2-D. the corresponding information is in this link. python - PyTorch: How to use DataLoaders for custom Datasets - Stack Overflow For now, I was just testing only one label. Anyway, I removed the brackets and tried again. the error is like this:

I guess your data is of type torch.DoubleTensor.
If you need double precision, you should cast your model using model = model.double().
The common case is to use float so you could try to cast your data using

train = data_utils.TensorDataset(DEAP_x_train_torch.float(), DEAP_y_train_torch)

yes, i couldn’t agree more. and, I wanna use double precision. this error occurred after adding model = model.double().

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

Now, I am confused. Could you tell me where is wrong?Thanks a lot!

nn.NLLLoss and nn.CrossEntropyLoss both require torch.LongTensors as the target input.
Cast your targets to .long() and try it again.

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?

Sorry for the confusion. I didn’t explained your last error properly.

Your training data should be of type float, while your target should be long.
Could you change your dataset to

 train = data_utils.TensorDataset(DEAP_x_train_torch.float(), DEAP_y_train_torch.long())

and run it again?

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:

No worries.
I forgot you wanted to use your model in double precision.
Try the following:

train = data_utils.TensorDataset(DEAP_x_train_torch.double(), DEAP_y_train_torch.long())
model = model.double()

No, the types of data and target depend on the loss function you are using.
In a classification use case you usually use floats for your data and long for the target.
You can find more information in the docs.

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%.

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)

Screenshot%20from%202018-08-02%2017-43-51

What kind of data do you have and what is your expected accuracy?

the feature is extracting from DEAP data set using HOC,and the label is also in this dataset. the expected accuracy is about 60%.

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