Search code examples
pythontensorflowkeraslstmrecurrent-neural-network

Quick question about LSTM's 3D input shape


I'm very confused about LSTM's 3D input shape.

According to internet, it says that the input shape is [batchsize, timestep, feature].

So if I have a 2D data of 1000 timesteps * 10 features and I would like to predict its future.

Also, if I would like LSTM layer to read 10 timesteps and then predict the next timestep, that is, read t= 1-10, predict t=11.

Does that make my input shape [990,10,10] or [10,990,10]?

Thanks in advance.


Solution

  • The first one is correct. Here's an example:

    import tensorflow as tf
    import numpy as np
    
    x = np.random.rand(1000, 10)
    y = np.random.rand(1000)
    
    
    def multivariate_data(dataset, target, 
                          start_index, end_index, 
                          history_size, target_size, 
                          step, single_step=False):
      data = []
      labels = []
    
      start_index = start_index + history_size
      if end_index is None:
        end_index = len(dataset) - target_size
    
      for i in range(start_index, end_index):
        indices = range(i-history_size, i, step)
        data.append(dataset[indices])
    
        if single_step:
          labels.append(target[i+target_size])
        else:
          labels.append(target[i:i+target_size])
    
      return np.array(data), np.array(labels)
    
    x, y = multivariate_data(dataset=x,
                             target=y,
                             start_index=0,
                             end_index=len(x),
                             history_size=10,
                             target_size=1,
                             step=1, single_step=False)
    
    print(x.shape)
    
    (990, 10, 10)