title: loss.backward() - IndexError: select(): index 1 out of range for tensor of size [1, 32, 100] at dimension 0
body: <p>I have created the following NN using PyTorch API (for NLP Multi-class Classification)</p>
  <pre><code>class MultiClassClassifer(nn.Module):
    #define all the layers used in model
    def __init__(self, vocab_size, embedding_dim, hidden_dim, output_dim):
      
      #Constructor
      super(MultiClassClassifer, self).__init__()
  
      #embedding layer
      self.embedding = nn.Embedding(vocab_size, embedding_dim)
  
      #dense layer
      self.hiddenLayer = nn.Linear(embedding_dim, hidden_dim)
  
      #Batch normalization layer
      self.batchnorm = nn.BatchNorm1d(hidden_dim)
  
      #output layer
      self.output = nn.Linear(hidden_dim, output_dim)
  
      #activation layer
      self.act = nn.Softmax(dim=1) #2d-tensor
  
      #initialize weights of embedding layer
      self.init_weights()
  
    def init_weights(self):
  
      initrange = 1.0
      
      self.embedding.weight.data.uniform_(-initrange, initrange)
    
    def forward(self, text, text_lengths):
  
      embedded = self.embedding(text)
      embedded = torch.mean(embedded, dim=1, keepdim=True)
      print(embedded.shape)
  
      #packed sequence
      packed_embedded = nn.utils.rnn.pack_padded_sequence(embedded, text_lengths, batch_first=True)
  
      tensor, batch_size = packed_embedded[0], packed_embedded[1]
      print(tensor.shape)
  
      hidden_1 = self.batchnorm(self.hiddenLayer(tensor))
      print(hidden_1.shape)
  
      output = self.act(self.output(hidden_1))
      print(output.shape)
  
      return output
  </code></pre>
  <p>Instantiating the model</p>
  <pre><code>INPUT_DIM = len(TEXT.vocab)
  EMBEDDING_DIM = 100
  HIDDEN_DIM = 64
  OUTPUT_DIM = 3
  
  model = MultiClassClassifer(INPUT_DIM, EMBEDDING_DIM, HIDDEN_DIM, OUTPUT_DIM)
  </code></pre>
  <p>To train the model I have created the following <code>train()</code> method:</p>
  <pre><code>def train(model, iterator, optimizer, criterion):
      
      epoch_loss = 0
      
      model.train()
      
      for batch in iterator:
          
          optimizer.zero_grad()
  
          text, text_lengths = batch.text_normalized_tweet
                  
          predictions = model(text, text_lengths)
          
          loss = criterion(predictions, batch.label)
          print(loss)
          print(loss.item())
  
          loss.backward() #exception occurs here
        
          optimizer.step()
          
          epoch_loss += loss.item()
    
      return epoch_loss / len(iterator)
  </code></pre>
  <p>However, when I start the first epoch of the training, during the <code>loss.backward()</code> step I receive the following error</p>
  <blockquote>
  <p>IndexError: select(): index 1 out of range for tensor of size [1, 32, 100] at dimension 0</p>
  </blockquote>
  <p>Here is the output of the <code>print()</code> functions</p>
  <pre><code>torch.Size([32, 1, 100])
  torch.Size([32, 100])
  torch.Size([32, 64])
  torch.Size([32, 3])
  tensor(1.1309, grad_fn=&lt;NllLossBackward0&gt;)
  1.1309444904327393
  </code></pre>
  <p>Does the error have to do with the <code>nn.Embedding</code> layer of my network? And if so, how can I fix this exception.</p>
  <p>You may find <a href="https://colab.research.google.com/drive/1HtohSU4oz_FwllYll0WgVtbaj4cI-FR2?usp=sharing" rel="nofollow noreferrer">here</a> my Golab Notebook</p>