IndexError: too many indices for tensor

IndexError: too many indices for tensor of dimension 0

I am trying to write the code to train the model and I get this error. The code is given below:

model = Classifier()
criterion = nn.NLLLoss()
optimizer = optim.Adam(model.parameters(), lr=0.003)

epochs = 30
steps = 0

train_losses, test_losses = [], []
for e in range(epochs):
    running_loss = 0
    for images, labels in trainloader:        
        log_ps = model(images)
        loss = criterion(log_ps, labels)
        running_loss += loss.item()

Make sure you pass the mean and std values as 1-dimensional tensors, e.g.:

mean = torch.tensor([0.5, 0.5, 0.5])

Based on the error, it looks like you are trying to pass them as 0-dim tensors:

mean = torch.tensor(0.5)

Thanks @ptrblck for the hint. I changed my transform params from

transform = transforms.Compose([
    transforms.Normalize((0.5), (0.5))


transform = transforms.Compose([
    transforms.Normalize((0.5,), (0.5,))

and it worked!

1 Like