具有 3 个隐藏层的 RNN 模型

数据挖掘 深度学习 喀拉斯 rnn
2022-02-20 04:34:31

在一篇论文中,它提到:ANN、RNN 和 LSTM NN 被优化为包含三个隐藏层,每层有 1000 个隐藏单元。

我想在 Keras 中建模 RNN 模型。但是我的代码出错了!

我的代码:

model=Sequential()
model.add(SimpleRNN(1000,input_shape=(320,15),activation='relu'))
model.add(SimpleRNN(1000))
model.add(SimpleRNN(1000))
model.add(Dense(1600))

错误:

    ValueError                                Traceback (most recent call last)
<ipython-input-49-ff01ce62eb30> in <module>()
      1 model=Sequential()
      2 model.add(SimpleRNN(1000,input_shape=(320,15),activation='relu'))
----> 3 model.add(SimpleRNN(1000))
      4 model.add(SimpleRNN(1000))
      5 model.add(Dense(1600))
.....
....
...
..
.


ValueError: Input 0 is incompatible with layer simple_rnn_2: expected ndim=3, found ndim=2

如何为 RNN 模型编码,该模型经过优化以包含三个隐藏层,每层有 1000 个隐藏单元?

非常感谢

1个回答

形状应该是 3d 数组 --> (samples, timesteps, features)

它需要在前两个 RNN 层中添加 return_sequences=True。

model=Sequential()
model.add(SimpleRNN(1000,input_shape=(1,320*15),activation='relu', return_sequences=True))
model.add(SimpleRNN(1000, return_sequences=True))
model.add(SimpleRNN(1000))