Keras LSTM dense layer multidimensional input

。_饼干妹妹 提交于 2019-12-06 13:29:15

In case of keras < 2.0: you need to use TimeDistributed wrapper in order to apply it element-wise to a sequence.

In case of keras >= 2.0: Dense layer is applied element-wise by default.

Since you updated your keras version and your error messages changed, here is what works on my machine (Keras 2.0.x)

This works:

model = Sequential()

model.add(LSTM(10,input_shape=(15, 10), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM( 100, return_sequences=True))
model.add(Dropout(0.2))
model.add(Dense(1, activation='linear'))

This also works:

model = Sequential()

model.add(LSTM(10,input_shape=(15, 10), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM( 100, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(1,return_sequences=True, activation='linear'))

Testing with:

x = np.ones((3000,15,10))
y = np.ones((3000,15,1))

Compiling and training with:

model.compile(optimizer='adam',loss='mse')
model.fit(x,y,epochs=4,verbose=2)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!