IndexError: Dimension out of range Error


I’m getting an error that I’m having trouble solving. It’s a binary classification problem on tabular data. It’s something to do with the dimension(I think ) on the log_softmax. Below is the error.

Traceback (most recent call last):

  File "<ipython-input-4-626b771bc4cb>", line 39, in <module>
    outputs = model1(x)

  File "C:\Users\JORDAN.HOWELL.GITDIR\AppData\Local\Continuum\anaconda3\envs\torch_env\lib\site-packages\torch\nn\modules\", line 722, in _call_impl
    result = self.forward(*input, **kwargs)

  File "<ipython-input-4-626b771bc4cb>", line 14, in forward
    x = F.log_softmax(x,dim=2)

  File "C:\Users\JORDAN.HOWELL.GITDIR\AppData\Local\Continuum\anaconda3\envs\torch_env\lib\site-packages\torch\nn\", line 1591, in log_softmax
    ret = input.log_softmax(dim)

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

Here is the model:

class model(nn.Module):
    def __init__(self):
        self.fc1 = nn.Linear(D, 10)
        self.fc2 = nn.Linear(10, 5)
        self.fc3 = nn.Linear(5, 2)
    def forward(self, x_train):
        x = self.fc1(x_train)
        x = self.fc2(x)
        x = self.fc3(x)
        x = F.log_softmax(x, dim=2)
        return x

model1 = model()

# Loss and optimizer
criterion = nn.NLLLoss()
optimizer = torch.optim.Adam(model1.parameters())

Here is the training loop:

# Train the model
n_epochs = 1000

# Stuff to store
train_losses = np.zeros(n_epochs)
test_losses = np.zeros(n_epochs)

for it in range(n_epochs):
  # zero the parameter gradients

  # Forward pass
  outputs = model1(x)
  loss = criterion(outputs, y)
  # Backward and optimize

  # Get test loss
  outputs_test = model1(test_x)
  loss_test = criterion(outputs_test, test_y)

  # Save losses
  train_losses[it] = loss.item()
  test_losses[it] = loss_test.item()
  if (it + 1) % 50 == 0:
    print(f'Epoch {it+1}/{n_epochs}, Train Loss: {loss.item():.4f}, Test Loss: {loss_test.item():.4f}')

Thanks for any help.

X in the forward function of your network is a 1-D vector, right? (a vector of shape (1, 2)). So you want to call softmax along that dim, so pass dim=1 in your log_softmax. It’s the index of the dimension along which you want softmax to operate, not the number of values in it.

Thank you. I now get this error after changing the dim:

Traceback (most recent call last):

  File "C:\Users\JORDAN.HOWELL.GITDIR\Documents\GitHub\Inspection Model Contest\", line 155, in <module>
    loss = criterion(outputs, y)

  File "C:\Users\JORDAN.HOWELL.GITDIR\AppData\Local\Continuum\anaconda3\envs\torch_env\lib\site-packages\torch\nn\modules\", line 722, in _call_impl
    result = self.forward(*input, **kwargs)

  File "C:\Users\JORDAN.HOWELL.GITDIR\AppData\Local\Continuum\anaconda3\envs\torch_env\lib\site-packages\torch\nn\modules\", line 211, in forward
    return F.nll_loss(input, target, weight=self.weight, ignore_index=self.ignore_index, reduction=self.reduction)

  File "C:\Users\JORDAN.HOWELL.GITDIR\AppData\Local\Continuum\anaconda3\envs\torch_env\lib\site-packages\torch\nn\", line 2218, in nll_loss
    ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)

RuntimeError: 1D target tensor expected, multi-target not supported

My y is a tensor of 1’s or 0’s

Your target tensor might have an additional unnecessary dimension as [batch_size, 1], so remove dim1 via target = target.squeeze(1) if that’s the case.