title: Pytorch LSTM and cross entropy
body: <p>I am working on sentiment analysis, I want to classify the output into 4 classes. For <strong>loss</strong> I am using <strong>cross-entropy</strong>.</p>
  <p>The problem is PyTorch cross-entropy needs the input of <strong>(batch_size, output)</strong> which is am having trouble with.</p>
  <p>I am taking a <strong>batch size of 12</strong> and <strong>sequence size is 32</strong></p>
  <pre><code>import torch.nn as nn
  
  
  class RNN(nn.Module):
      def __init__(self, hidden_dim = 256, input_size = 32 , num_layers = 1, num_classes=4, vocab_size = len(vocab_to_int)+1, embedding_dim=100):
          super().__init__()
          self.input_size = input_size
          self.hidden_dim = hidden_dim
          self.num_layers = num_layers
          self.num_classes = num_classes
          self.embedding = nn.Embedding(vocab_size, embedding_dim)
          self.lstm = nn.LSTM(embedding_dim, hidden_dim, num_layers)
          self.fc1 = nn.Linear(hidden_dim, 50)
          self.fc2 = nn.Linear(50, 4)
  
      
      def forward(self, x, hidden):
          x = self.embedding(x)
          x = x.view(32, 12, 100)
          x, hidden = self.lstm(x, hidden)
          x = x.contiguous().view(-1, 256)
          x = self.fc1(x) # output shape ([384, 50])
          x = self.fc2(x) # output shape [384, 4]
          return x, hidden
      
      def init_hidden(self, batch_size=12):
          weight = next(self.parameters()).data
          hidden = (weight.new(self.num_layers, 12, self.hidden_dim).zero_().cuda(), weight.new(self.num_layers, 12, self.hidden_dim).zero_().cuda())
          return hidden
  </code></pre>