title: How to setup 1D-Convolution and LSTM in Keras
body: <p>I would like to use 1D-Conv layer following by LSTM layer to classify a 16-channel 400-timestep signal. </p>
  
  <p>The input shape is composed of:</p>
  
  <ul>
  <li><p><code>X = (n_samples, n_timesteps, n_features)</code>, where <code>n_samples=476</code>, <code>n_timesteps=400</code>, <code>n_features=16</code> are the number of samples, timesteps, and features (or channels) of the signal.</p></li>
  <li><p><code>y = (n_samples, n_timesteps, 1)</code>. Each timestep is labeled by either 0 or 1 (binary classification).</p></li>
  </ul>
  
  <p>I use the 1D-Conv to extract the temporal information, as shown in the figure below. <code>F=32</code> and <code>K=8</code> are the filters and kernel_size. 1D-MaxPooling is used after 1D-Conv. 32-unit LSTM is used for signal classification. The model should return a <code>y_pred = (n_samples, n_timesteps, 1)</code>.</p>
  
  <p><a href="https://i.stack.imgur.com/QVASQ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/QVASQ.png" alt="enter image description here"></a></p>
  
  <p>The code snippet is shown as follow:</p>
  
  <pre><code>input_layer = Input(shape=(dataset.n_timestep, dataset.n_feature))
  conv1 = Conv1D(filters=32,
                 kernel_size=8,
                 strides=1,
                 activation='relu')(input_layer)
  pool1 = MaxPooling1D(pool_size=4)(conv1)
  lstm1 = LSTM(32)(pool1)
  output_layer = Dense(1, activation='sigmoid')(lstm1)
  model = Model(inputs=input_layer, outputs=output_layer) 
  </code></pre>
  
  <p>The model summary is shown below:</p>
  
  <p><a href="https://i.stack.imgur.com/YxNBv.png" rel="noreferrer"><img src="https://i.stack.imgur.com/YxNBv.png" alt="enter image description here"></a></p>
  
  <p>However, I got the following error:</p>
  
  <pre><code>ValueError: Error when checking target: expected dense_15 to have 2 dimensions, but got array with shape (476, 400, 1).
  </code></pre>
  
  <p>I guess the problem was the incorrect shape. Please let me know how to fix it.</p>
  
  <p>Another question is the number of timesteps. Because the <code>input_shape</code> is assigned in the 1D-Conv, how can we let LSTM know the timestep must be 400?</p>
  
  <hr>
  
  <p>I would like to add the model graph based on the suggestion of @today. In this case, the timestep of LSTM will be 98. Do we need to use TimeDistributed in this case? I failed to apply the TimeDistributed in the Conv1D. </p>
  
  <p><a href="https://i.stack.imgur.com/demcq.png" rel="noreferrer"><img src="https://i.stack.imgur.com/demcq.png" alt="enter image description here"></a></p>
  
  <p>Is there anyway to perform the convolution among channels, instead of timesteps? For example, a filter (2, 1) traverses each timestep, as shown in figure below.
  <a href="https://i.stack.imgur.com/V2rYb.png" rel="noreferrer"><img src="https://i.stack.imgur.com/V2rYb.png" alt="enter image description here"></a></p>
  
  <p>Thanks.</p>