Search code examples
pythonneural-networkpytorchconv-neural-networkmnist

Pytorch CNN: Expected input to have 1 channel but got 60000 channels instead


While implementing a NN for Fashion MNIST dataset, I'm getting the following error:

RuntimeError: Given groups=1, weight of size [6, 1, 5, 5], expected input[1, 60000, 28, 28] to have 1 channels, but got 60000 channels instead

I'm inferring that 60000 is the length of my entire dataset, but not sure why is the algorithm giving this error. Can someone help me fix this please?

My dataset:

(X_train, y_train), (X_test, y_test) = fashion_mnist.load_data()
train_data = []
test_data = []
train_data.append([X_train, y_train])
test_data.append([X_test, y_test])

trainloader = torch.utils.data.DataLoader(train_data, shuffle=True, batch_size=100)
testloader = torch.utils.data.DataLoader(test_data, shuffle=True, batch_size=100)

I'm getting the error in following order (as per stack trace):

      8     #making predictions
----> 9     y_pred = model(images)

     32     #first hidden layer
---> 33     x = self.conv1(x)

Update 1

Added the line:

images = images.transpose(0, 1)

to transpose the image as pointed out by Ivan but now getting the error:

RuntimeError: expected scalar type Byte but found Float

Solution

  • You input is shaped (1, 60000, 28, 28), while it should be shaped (60000, 1, 28, 28). You can fix this by transposing the first two axes:

    >>> x.transpose(0, 1)