IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)
As I've read through some previous questions, I'm getting this error that probably has something to do with a discrepancy between dimensions of tensors but since this is my very first attempt of running PyTorch so I'm coming here because I have far to little intuition about it. Wanted to run a non-standard dataset (which I'm pretty sure I'm loading fine) on a basic MNIST setup to mess with it and see what moves what.

Traceback (most recent call last):

File "C:/Users/Administrator/Desktop/pytong/proj/pytorch_cnnv2.py", line 110, in &lt;module&gt;
loss = loss_func(output, b_y)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\module.py", line 547, in __call__
result = self.forward(*input, **kwargs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\loss.py", line 916, in forward
ignore_index=self.ignore_index, reduction=self.reduction)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\functional.py", line 1995, in cross_entropy
return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\functional.py", line 1316, in log_softmax
ret = input.log_softmax(dim)

IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)


The line making the fuss is the loss function:

loss = loss_func(output, b_y)


The rest of the code, sans imports and load:

class CNNModel(nn.Module):
def __init__(self):
    super(CNNModel, self).__init__()

    # Convolution 1
    self.cnn1 = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=5, stride=1, padding=0)
    self.relu1 = nn.ReLU()

    # Max pool 1
    self.maxpool1 = nn.MaxPool2d(kernel_size=2)

    # Convolution 2
    self.cnn2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=5, stride=1, padding=0)
    self.relu2 = nn.ReLU()

    # Max pool 2
    self.maxpool2 = nn.MaxPool2d(kernel_size=2)

    # Fully connected
    self.fc1 = nn.Linear(338 * 4 * 4, 5)

def forward(self, x):
    # Convolution 1
    out = self.cnn1(x)
    print(out.shape)
    out = self.relu1(out)
    print(out.shape)

    # Max pool 1
    out = self.maxpool1(out)
    print(out.shape)

    # Convolution 2
    out = self.cnn2(out)
    print(out.shape)
    out = self.relu2(out)
    print(out.shape)

    # Max pool 2
    out = self.maxpool2(out)
    print('++++++++++++++ out')
    print(out.shape)
    # out = out.reshape(-1, 169 * 4 * 4)
    out = out.view(out.size(0), -1)
    print(out.shape)
    print('-----------------------')
    # Linear function (readout)
    out = self.fc1(out)
    print(out.shape)
    print('=======================')

    return out



if __name__ == '__main__':

print("Number of train samples: ", len(train_data))
print("Number of test samples: ", len(test_data))
print("Detected Classes are: ", train_data.class_to_idx) # classes are detected by folder structure

model = CNNModel()
optimizer = torch.optim.Adam(model.parameters(), lr=LEARNING_RATE)
loss_func = nn.CrossEntropyLoss()

# Training and Testing
for epoch in range(EPOCHS):
    print(enumerate(train_data_loader))
    for step, (x, y) in enumerate(train_data_loader):
        b_x = Variable(x)   # batch x (image)
        b_y = Variable(y)   # batch y (target)
        # print('============ b_x')
        # print(len(b_x))
        # print(b_x.data)
        # print('============ b_y')
        # print(len(b_y))
        # print(b_y.data)

        output = model(b_x)[0]
        loss = loss_func(output, b_y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        if step % 50 == 0:
            test_x = Variable(test_data_loader)
            test_output, last_layer = model(test_x)
            pred_y = torch.max(test_output, 1)[1].data.squeeze()
            accuracy = sum(pred_y == test_y) / float(test_y.size(0))
            print('Epoch: ', epoch, '| train loss: %.4f' % loss.data[0], '| test accuracy: %.2f' % accuracy)


Plus the output of my prints that I've tried to use for diagnostics:

torch.Size([100, 16, 60, 60])
torch.Size([100, 16, 60, 60])
torch.Size([100, 16, 30, 30])
torch.Size([100, 32, 26, 26])
torch.Size([100, 32, 26, 26])
++++++++++++++ out
torch.Size([100, 32, 13, 13])
torch.Size([100, 5408])
-----------------------
torch.Size([100, 5])
=======================


The problem is in this line:

output = model(b_x)[0]


The [0] changes the shape from [100, 5] to [5], and the loss expects the other way. Just remove it:

output = model(b_x)

