首页 > 解决方案 > Stack two LSTM layers in Keras dimension mismatch

问题描述

I want to make an LSTM neural net using Keras which gets as input some length of four features and predicts 10 following values. And I can't manage to set proper input dimensions. X_train is an array of shape (34,5,4) (repeated observations, the sequence of observations, features) y_train is an array of shape(34,10). I can't manage to satisfy the required dimensions.

Any ideas what am I doing wrong?

X_train = X_train.reshape((X_train.shape[0], X_train.shape[1], 4))
model.add(LSTM(30, dropout=0.2, batch_size=window_size))
model.add(LSTM(10, activation=None))
model.compile(optimizer='adam',loss='mse')
model.fit(X_train,y_train,epochs= epochs,validation_split=0.2,shuffle=True)

标签: pythontensorflowkeraslstmrecurrent-neural-network

解决方案


如果要堆叠两层lstm,则需要return_sequence用于第一层,它返回每个时间步的输出,将馈送到第二lstm层。

这是解释示例,您可以通过它解决您的问题。


推荐阅读