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

self.criterion = nn.CrossEntropyLoss(reduction=‘none’)

Dataset - Airbnb dataset to predict prices
Model -MLP Regressor


class MLP_Regressor(nn.Module):
     def __init__(self,config):
      super(MLP_Regressor, self).__init__()
      self.fc1 = nn.Linear(config['n_inputs'], config['n_hidden'])
      self.act1 = ReLU()
      self.fc2 = nn.Linear(config['n_hidden'], config['n_hidden'])
      self.act2 = ReLU()
      self.fc3 = nn.Linear(config['n_hidden'],1)#1

     def forward(self, X):
         X = self.fc1(X)
         X = self.act1(X)
         X = self.fc2(X)
         X = self.act2(X)
         X = self.fc3(X)

     return X

training -

def _train(self, cur_epoch, dataset_name): = True
    train_losses = 0.0
    clf_losses = 0.0
    metric_losses = 0.0
    d_losses = 0.0
    g_losses = 0.0
    correct = 0
    total = 0
    n_batch = len(self.train_loader)
    print(f'\n=> Training Epoch #{cur_epoch}')
    for batch_idx, (inputs, labels) in enumerate(self.train_loader):

        inputs, labels =

        # seed_features =, seq_lens)
        features = inputs.float()

        labels = labels.type(torch.LongTensor)
        outputs =  # Forward Propagation
        inputs =
            labels =

            features = inputs
            outputs = encoder(inputs.float())
            labels=torch.argmax(labels, dim=1)
            #labels = labels.reshape((labels.shape[0], 1))
            loss = self.criterion(outputs, labels)

I guess the error is raised in:

labels=torch.argmax(labels, dim=1)

as labels might have a single dimension only.
Check the shape of labels and make sure torch.argmax can be applied on dim1.

Yes, the labels have a shape as torch.size([200]). how to apply torch.argmax in this case?

Most likely you wouldn’t use torch.argmax in this case. Could you explain why you want to apply argmax on this tensor, what the current values are and what the expected result would be?

Current values in the target are the prices such as 89,192,56 etc. I want to pass it through MLP regressor to predict future prices

In that case I’m not sure what the argmax should return for an input of [89, 192, 56]. argmax will return the index of the tensor in the specified dimension containing the max value. In your case your tensor doesn’t have a second dimension and is containing regression targets for each sample.

The initial error for me was when I was passing the target tensor to criterion(loss). The error is targetError : 89 out of bound. To overcome that I used torch.argmax

argmax won’t solve this issue, which points to a mismatch between the valid target indices and the model output shape.
Assuming you are using nn.CrossEntropyLoss the output should have the shape [batch_size, nb_classes] while the target should have the shape [batch_size] and contain class indices in the range [0, nb_classes-1]. Since your target contains values as large as 156 you would be dealing with at least 157 classes (valid indices are [0, 156]) and the model output should thus have the shape [batch_size, 157].
While this doesn’t fit your description of working on a regression task, the error would fit into an indexing issue in nn.CrossEntropyLoss.