Solved – Multi-dimentional and multivariate Time-Series forecast (RNN/LSTM) Keras

forecastingkeraslstmpythonrecurrent neural network

I have been trying to understand how to represent and shape data to make a multidimentional and multivariate time series forecast using Keras (or TensorFlow) but I am still very unclear after reading many blog posts/tutorials/documentation about how to present the data in the correct shape (most examples being of slightly less

My Dataset:

  • several cities
  • for which I have info about say temperature, car traffic, humidity
  • for say the last 2 years (one record for each day)

What I want to do:
I'd like to forecast for each city the temperatures I can expect for the next year using a possibly lagged version of temperature, car traffic and humidity (of course there would be several more features but this is just an example for thought).

What I am confused about:
If I have 2 cities, for which I recorded 3 features for 365 days. How should I shape my input so that the model can output a forecast for 365 days for these two cities (i.e. 2 time series of temperatures for 365 days)?

Intuitively the tensor shape would be (?, 365, 3) for 365 days and 3 features. But I'm not sure what to stick into the first dimension and, most importantly, I would be surprised if it had to be for the number of cities. But at the same time, I have no idea how to specify into the model that it has to understand the dimensions properly.

Any pointers will be helpful. I'm pretty familiar with the rest of the problem (i.e. how you build a network in Keras etc since I have done this for other neural networks but more specifically how best to encode the sequence for the desired input.)

Oh and also, I guess I could train and predict for each city independently, but I'm sure everyone will agree there are probably things to be learned that are not particular to any city but that can only be seen if considering several of them, hence why I think it is important to encode it in the model.

Best Answer

The first term of the tensor shape is the batch size it helps for the trainning from keras page(https://keras.io/getting-started/sequential-model-guide/): "Same stacked LSTM model, rendered "stateful" A stateful recurrent model is one for which the internal states (memories) obtained after processing a batch of samples are reused as initial states for the samples of the next batch. This allows to process longer sequences while keeping computational complexity manageable."

Now for your kind of problem I would think of your features as a multi linear regression, so you get the correlations of the features. So for 2 (cities) x 3 features = 6 with Y_ the shifted data you want to predict if you want the whole year you will need lot more than 2 years. Hope this helps!

Related Question