Assert all(tensors[0].size(0) == tensor.size(0) for tensor in tensors) AssertionError

Hi, I am using pytorch to build CNN model. But I am facing some problem with data generation. Here is my data shape:
X_train = (-1, 1, 169999, 200)
X_test = (-1, 1, 3000, 200)
y_train = (169999, 200)
y_test = (3000, 200)
train = torch.utils.data.TensorDataset(X_train, y_train)
test = torch.utils.data.TensorDataset(X_test, y_test)

But after that I am getting this error:
assert all(tensors[0].size(0) == tensor.size(0) for tensor in tensors)
AssertionError

I guess your problem is that the tensors for y_* are missing the batch dimension? each training sample should have it’s corresponding label.

should I need to change the dimension of y_train and y_test?

It depends what your task is.
Pytorch always assumeses that the first dimension is the batch dimension.
If you do regression, then X_train and y_train should have exactly the same size.
If you are doing classification, y_train will be a 1D tensor of size X_train.size(0) (the batch size).

I am doing classification task. I want my X as four dimension and my y as 1 dimension but getting error while I am trying to stack it together.
train = torch.utils.data.TensorDataset(X_train, y_train), the error is coming from this line.

You need to check that X_train.size(0) is the same as y_train.size(0).

1 Like