How to change batch size of an intermediate layer in Keras?
<p>My problem is to take all hidden outputs from an LSTM and use them as training examples for a single dense layer. Flattening the output of the hidden layers and feeding them to a dense layer is not what I am looking to do. I have tried the following things:</p>

<ol>
<li><p>I have considered Timedistributed wrapper for the dense layer (<a href="https://keras.io/layers/wrappers/" rel="nofollow noreferrer">https://keras.io/layers/wrappers/</a>). But, this seems to apply the same layer to every time slice, which is not what I want. In other words, the Timedistributed wrapper has input_shape of a 3D tensor (number of samples, number of timesteps, number of features) and produces another 3D tensor of the same type: (number of samples, number of timesteps, number of features). Instead what I want is a 2D tensor as output, which looks like (number of samples*number of timesteps, number of features)</p></li>
<li><p>There was a pull request for an AdvancedReshapeLayer: <a href="https://github.com/fchollet/keras/pull/36" rel="nofollow noreferrer">https://github.com/fchollet/keras/pull/36</a> on GitHub. This seems to be exactly what I am looking for. Unfortunately, it appears like that pull request was closed with no conclusive outcome. </p></li>
<li><p>I tried to build my own lambda layer to accomplish what I want as follows:
<code>
A). model.add(LSTM(NUM_LSTM_UNITS, return_sequences=True, activation='tanh')) # 
B). model.add(Lambda(lambda x: x, output_shape=lambda x: (x[0]*x[1], x[2])))
C). model.add(Dense(NUM_CLASSES, input_dim=NUM_LSTM_UNITS))
</code></p></li>
</ol>

<p>mode.output_shape after (A) prints: (BATCH_SIZE, NUM_TIME_STEPS, NUM_LSTM_UNITS) and model.output_shape after (B) prints: (BATCH_SIZE*NUM_OF_TIMESTEPS, NUM_LSTM_UNITS)</p>

<p>Which is exactly what I am trying to achieve.</p>

<p>Unfortunately, when I try to run step (C). I get the following error: </p>

<blockquote>
  <p>Input 0 is incompatible with layer dense_1: expected ndim=2, found
  ndim=3</p>
</blockquote>

<p>This is baffling since when I print model.output_shape after (B), I do indeed see (BATCH_SIZE*NUM_OF_TIMESTEPS, NUM_LSTM_UNITS), which is of ndim=2. </p>

<p>Really appreciate any help with this. </p>

<p>EDIT: When I try to use the functional API instead of a sequential model, I still get the same error on step (C)</p>