I'm having X_train of shape (1400, 64, 35) and y_train of shape (1400,). I want to give X_train as input to LSTM layer and also want to find the Average (using GlobalAveragePooling Layer) of the Output of LSTM at each time step and give it as input to a Dense Layer. For this problem how to connect the layers and build a sequential model?

I'm using Tensorflow.Keras API's

  • $\begingroup$ It would help knowing more about your data, but you could look into ConvLSTM networks. $\endgroup$
    – tehem
    Commented Aug 24, 2020 at 15:35

3 Answers 3


LSTM takes as input 3 dimension tensors (batch_size,time_step,input). So before adding a LSTM() layer you need to either use Flatten() or TimeDistributed(Flatten()) layer.

this is a basic LSTM model


If you understand how a particular type of Layer works, you can simply add them as at the end all of these are Tensor operations.
But you must know what are you doing.

May do this way

from keras.models import Sequential
from keras import layers

model = Sequential()
model.add(layers.LSTM(30, return_sequences=True, input_shape=(30,3)))

model.compile(optimizer='adam', loss='mse', metrics=['mae'])

# For the case - x = (1800, 30, 3) and y=1800,1
history = model.fit(datagen(batch_size), steps_per_epoch=len(target)/batch_size, epochs=5)

You have mentioned X_train shape is (1400, 64, 35), So we can create a LSTM model whose input size will be (64,35) And you can take the number of units in LSTM as per your choice.

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, GlobalAveragePooling1D, Dense

# Define the model
model = Sequential()

# Add the LSTM layer
model.add(LSTM(units=64, input_shape=(64, 35), return_sequences=True))

# Add the GlobalAveragePooling layer

# Add the Dense layer
model.add(Dense(units=10, activation='softmax'))

# Compile the model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

# Print the model summary
model. Summary()

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.