TransWikia.com

How to give a 3D Tensor as input to LSTM

Data Science Asked by Ajay Ganti on April 2, 2021

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

2 Answers

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

Answered by Shiv on April 2, 2021

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.add(layers.GlobalAveragePooling1D())
model.add(layers.Dense(20))
model.add(layers.Dense(1))

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

# 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)

Answered by 10xAI on April 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP