How to feed a 4D tensor into LSTM model?

Viewed 298

I want to use an LSTM model to predict the future sales.

The data is like the table below.

date       store family     sales
01/01/2013 1     AUTOMOTIVE 0    
01/01/2013 1     BABY CARE   0    
01/01/2013 1     BEAUTY     1    
..         .     ..         .    
01/01/2013 2     AUTOMOTIVE 0    
01/01/2013 2     BABY CARE   0    
..         .     ..         .    
01/01/2013 50     AUTOMOTIVE 0    
..         .     ..         .    
01/02/2013 1     AUTOMOTIVE 0    
01/02/2013 1     BABY CARE   50    
..         .     ..         .    
01/02/2013 2     AUTOMOTIVE 500  
01/02/2013 2     BABY CARE   0    
..         .     ..         .    
01/02/2013 50     AUTOMOTIVE 0    
..         .     ..         .    
..         .     ..         .    
12/31/2015 1     AUTOMOTIVE 0    
12/31/2015 1     BABY CARE   50    
..         .     ..         .    
12/31/2015 2     AUTOMOTIVE 500  
12/31/2015 2     BABY CARE   0    
..         .     ..         .    
12/31/2015 50     AUTOMOTIVE 0    
..         .     ..         .    

For each day, it has 50 stores.
For each store, it has different type of family (product). (They are all in perfect order, thank God).
Last, for each type of family, it has its sales.

Here is the problem.

The dimension of input of LSTM model is (Batch_Size, Sequence_Length, Input_Dimension). It is a 3D tensor.

However, in my case, my Input_Dimension is 2D, which is (rows x columns)
rows: number of rows in one day, which is 1782
columns: number of features, which is 2 (store and family)

Is there a good way to make my data into a shape which can be fed into a LSTM model?

Thanks a lot!

2 Answers

The concept is straightforward. It works differently during learning and prediction however, so I'll explain the two cases separately.

  • For training, you simply chop the part of your sequence that you chose as training data into smaller subsequences of time points. (You need to do this because the network is "unrolled" for the duration of those subsequences and it cannot be too big; if there is no obvious choice, 32 or 64 timesteps should be a good starting point.) To maximize the use of your data, you also want your subsequences to have some overlap. Both TF and PyTorch should have some helper functions that help you get this (such as tf.keras.utils.timeseries_dataset_from_array). You may find this article useful: https://mobiarch.wordpress.com/2020/11/13/preparing-time-series-data-for-rnn-in-tensorflow/

  • For prediction, you can just feed your sequence by reshaping such that it is a 3D tensor with the first dimension of size 1. Either reshape or newaxis in NumPy can get you this. Of course you don't need to feed your entire sequence, only a sufficient number of timesteps back as context to get the RNN's state to converge to something appropriate.

The solution I came up with is to make the whole data in each day to be a long long long sequence. So the dimension will be 1D, and can be fed into the LSTM model.

But I don't think this is the optimal solution.

Does anyone come up with better answer? Appreciate.

Related