title: How to feed input into a pytorch lstm layer
body: <p>I am new to PyTorch, and don't understand how to fit a network using this framework.</p>
  
  <p>I have a simple model in Keras:</p>
  
  <pre><code>model = Sequential()
  model.add(Embedding(vocab_size, embedding_size, input_length=55, weights=[pretrained_weights]))
  model.add(Bidirectional(LSTM(units=len(X_train))))
  model.add(Dense(n_classes, activation='softmax'))
  model.compile(loss='categorical_crossentropy',
            optimizer = RMSprop(lr=0.0005),
            metrics=['accuracy'])
  
  model.fit(np.array(X_train), np.array(y_train), epochs=100, validation_data=(np.array(X_val), np.array(y_val)))
  </code></pre>
  
  <p>In Keras I can just feed the network with my X_train (a 2D array containing indices) and my y_train(a 2D array containing a single index for each input).</p>
  
  <p>Now, in order to feed my PyTorch model I transformed my matrix into a tensor like this:</p>
  
  <pre><code>M = torch.tensor(X_train)
  </code></pre>
  
  <p>And defined my network:</p>
  
  <pre><code># Bidirectional recurrent neural network (many-to-one)
  class BiRNN(nn.Module):
    def __init__(self, input_size, hidden_size, num_layers, num_classes):
      super(BiRNN, self).__init__()
      self.hidden_size = hidden_size
      self.num_layers = num_layers
      self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True, bidirectional=True)
      self.fc = nn.Linear(hidden_size*2, num_classes)  # 2 for bidirection
  
    def forward(self, x):
      # Set initial states
      h0 = torch.zeros(self.num_layers*2, x.size(0), self.hidden_size).to(device) # 2 for bidirection 
      c0 = torch.zeros(self.num_layers*2, x.size(0), self.hidden_size).to(device)
  
      # Forward propagate LSTM
      out, _ = self.lstm(x, (h0, c0))  # out: tensor of shape (batch_size, seq_length, hidden_size*2)
  
      # Decode the hidden state of the last time step
      out = self.fc(out[:, -1, :])
      return out
  
  model = BiRNN(input_size, hidden_size, num_layers, num_classes).to(device)
  
  # Loss and optimizer
  criterion = nn.CrossEntropyLoss()
  optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
  </code></pre>
  
  <p>But what I don't understand is how to call the function and use my own data for the predictions.</p>