IndexError: Dimension out of range Error

username: Jordan_Howell
cooked: <p>Hello,</p>
<p>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 <code>log_softmax</code>.  Below is the error.</p>
<pre><code class="lang-auto">Traceback (most recent call last):

  File "&lt;ipython-input-4-626b771bc4cb&gt;", line 39, in &lt;module&gt;
    outputs = model1(x)

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

  File "&lt;ipython-input-4-626b771bc4cb&gt;", 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\functional.py", 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)
</code></pre>
<p>Here is the model:</p>
<pre><code class="lang-auto">class model(nn.Module):
    def __init__(self):
        
        super().__init__()
        
        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())
</code></pre>
<p>Here is the training loop:</p>
<pre><code class="lang-auto"># 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
  optimizer.zero_grad()

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

  # 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}')
    
</code></pre>
<p>Thanks for any help.</p>

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

username: Jordan_Howell
cooked: <p>Thank you.  I now get this error after changing the dim:</p>
<pre><code class="lang-auto">Traceback (most recent call last):

  File "C:\Users\JORDAN.HOWELL.GITDIR\Documents\GitHub\Inspection Model Contest\q_r_train_torch.py", line 155, in &lt;module&gt;
    loss = criterion(outputs, y)

  File "C:\Users\JORDAN.HOWELL.GITDIR\AppData\Local\Continuum\anaconda3\envs\torch_env\lib\site-packages\torch\nn\modules\module.py", 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\loss.py", 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\functional.py", 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
</code></pre>
<p>My y is a tensor of 1’s or 0’s</p>

username: ptrblck
cooked: <p>Your target tensor might have an additional unnecessary dimension as <code>[batch_size, 1]</code>, so remove <code>dim1</code> via <code>target = target.squeeze(1)</code> if that’s the case.</p>