title: Keras: How to shape inputs for CNN and LSTM layers?
body: <p>I am building a model to predict geospatial-temporal datasets.</p>
  
  <p>My data has original dimensions (features, lat, lon, time), i.e. for each feature and at each lat/lon point there is a time series.</p>
  
  <p>I have created a CNN-LSTM model using Keras like so (I assume the below needs to be modified, this is just a first attempt):</p>
  
  <pre><code>def define_model_cnn_lstm(features, lats, lons, times):
      """
      Create and return a model with CN and LSTM layers. Input and output data is 
      expected to have shape (lats, lons, times).
  
      :param lats: latitude dimension of input 3-D array 
      :param lons: longitude dimension of input 3-D array
      :param times: time dimension of input 3-D array
      :return: CNN-LSTM model appropriate to the expected input array
      """
      # define the CNN model layers, wrapping each CNN layer in a TimeDistributed layer
      model = Sequential()
      model.add(TimeDistributed(Conv2D(features, (3, 3), 
                                       activation='relu', 
                                       padding='same', 
                                       input_shape=(lats, lons, times))))
      model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
      model.add(TimeDistributed(Flatten()))
  
      # add the LSTM layer, and a final Dense layer
      model.add(LSTM(units=times, activation='relu', stateful=True))
      model.add(Dense(1))
  
      model.compile(optimizer='adam', loss='mse')
  
      return model
  </code></pre>
  
  <p>My assumption is that this model will take data with shape (features, lats, lons, times), so for example if my geospatial grid is 180 x 360 and there are 100 time steps at each point, and I have 4 features per observation/sample, then the shape will be (4, 180, 360, 100).</p>
  
  <p>I assume that I will want the model to take arrays with shape (features, lats, lons, times) as input and be able to predict labels arrays with shape (labels, lats, lons, times) as output. I am first using a single variable as my label, but it might be interesting later to be able to have multivariate output as well (i.e. labels > 1).</p>
  
  <p>How should I best shape my data for input, and/or how to structure the model layers in a way that's most appropriate for this application? </p>