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

Rashmi_Koparde: <p>self.criterion = nn.CrossEntropyLoss(reduction=‘none’)</p>
<p>Dataset - Airbnb dataset to predict prices<br>
Model -MLP Regressor</p>
<p>Model</p>
<pre><code>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
</code></pre>
<p>training -</p>
<pre><code>def _train(self, cur_epoch, dataset_name):
    self.net.train()
    self.net.training = 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=&gt; Training Epoch #{cur_epoch}')
    for batch_idx, (inputs, labels) in enumerate(self.train_loader):

        inputs, labels = inputs.to(
            self.device), labels.to(self.device)

        # seed_features = self.net.extract_features(inputs, seq_lens)
        features = inputs.float()

        labels = labels.type(torch.LongTensor)
        outputs = self.net.forward(features)  # Forward Propagation
        inputs = inputs.to(self.device)
            labels = labels.to(self.device)

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

ptrblck: <p>I guess the error is raised in:</p>
<pre><code class="lang-python">labels=torch.argmax(labels, dim=1)
</code></pre>
<p>as <code>labels</code> might have a single dimension only.<br>
Check the shape of <code>labels</code> and make sure <code>torch.argmax</code> can be applied on <code>dim1</code>.</p>

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

ptrblck: <p>Most likely you wouldn’t use <code>torch.argmax</code> 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?</p>

Rashmi_Koparde: <p>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</p>

ptrblck: <p>In that case I’m not sure what the <code>argmax</code> should return for an input of <code>[89, 192, 56]</code>. <code>argmax</code> 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.</p>

Rashmi_Koparde: <p>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</p>

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