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.

  • $\begingroup$ Train on all cities at the same time, of course (if you have enough computing power to train a model complex enough to learn the information contained in the bigger data set). Then predict for one city at a time: thus the input is $(1, 365, 3)$. $\endgroup$
    – DeltaIV
    Commented Apr 9, 2018 at 7:59
  • $\begingroup$ Do you have the solution to your problem? I have the same question with multi-sites (cities in your case) LTSM. I very much appreciate if you could share your solution. Thanks. $\endgroup$
    – Nguyen Anh
    Commented Nov 4, 2020 at 16:03
  • $\begingroup$ That was a pretty long time ago so I don't remember how that worked out. But I think German's answer below was the one I accepted and it helped I know. $\endgroup$
    – Bastien
    Commented Nov 5, 2020 at 11:30

2 Answers 2


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!

  • $\begingroup$ Thanks for getting back to this question. I follow your explanation but I'm not sure what exactly is the shape you suggest then. Would it look like (2, 3, 365)? And yes with regard to data size I'd have actually much more than that but it was just for example purposes. $\endgroup$
    – Bastien
    Commented Feb 23, 2018 at 9:53
  • $\begingroup$ So you doing two NN for trainning two cities right? batch_input_shape=(batch_size, l365, 3), for one city with 3 features. If you want to train the two cities together would be batch_input_shape=(batch_size, 365, 6). The batch size is related to the size of the dataset, when your computational cost is high you divide in batches. Hope this helps $\endgroup$ Commented Feb 28, 2018 at 5:14
  • $\begingroup$ Ok gotcha, and then would this allow you to output a prediction per city too? $\endgroup$
    – Bastien
    Commented Mar 7, 2018 at 17:40

I think you should consider each city separately rather than modeling several cities together. So the input would be all data about one city, and the output is just prediction of that city. The batch size and sample size are just arguments you should define based on the memory size you have for GPU.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.