title: How to load 2D data into an LSTM in pytorch
body: <p>I have a series of sine waves that i have loaded in using a custom dataloader. The data is converted to a torch tensor using from_numpy. I then try to load the data using an enumerator over the train_loader. The iterator is shown below.</p>
  
  <pre><code>for epoch in range(epochs):
      for i, data in enumerate(train_loader):
          input = np.array(data)
          train(epoch)
  </code></pre>
  
  <p>The error i receive is:</p>
  
  <pre><code>RuntimeError: input must have 3 dimensions, got 2
  </code></pre>
  
  <p>I know i need to have my input data in <code>[sequence length, batch_size, input_size]</code> for an LSTM but i have no idea how to format my array data of 1000 sine waves of length 10000.</p>
  
  <p>Below is my training method.</p>
  
  <pre><code>def train(epoch):
      model.train()
      train_loss = 0
      def closure():
          optimizer.zero_grad()
          print(input.shape)
          output = model(Variable(input))
          loss = loss_function(output)
          print('epoch: ', epoch.item(),'loss:', loss.item())
          loss.backward()
          return loss
      optimizer.step(closure)
  </code></pre>
  
  <p>I thought i would try add (seq_length, batch_size, input_size) in a tuple but this cant be fed into the network. Further to this my assumption was that the dataloader fed batch size into the system. Any help would be appreciated.</p>
  
  <p><em>edit</em>:</p>
  
  <p>Here is my sample data:</p>
  
  <pre><code>T = 20
  L = 1000
  N = 100
  
  x = np.empty((N, L), 'int64')
  x[:] = np.array(range(L)) + np.random.randint(-4 * T, 4 * T, N).reshape(N, 1)
  data = np.sin(x / 1.0 / T).astype('float64')
  torch.save(data, open('traindata.pt', 'wb'))
  </code></pre>