How to create a one-to-one relationship between an Input and SimpleRNN layer in Keras?

35 views
Skip to first unread message

Andrew Erskine

unread,
Feb 26, 2021, 12:21:09 PM2/26/21
to Keras-users

I am trying to create a 2-layer simple RNN model with Keras in which I can directly feed data into the first layer. As I understand it, cells in an RNN are fully connected with their input with the standard Keras layer. For example, if I define an input with 4 features and 1 timestep, connected to a SimpleRNN with 4 cells as follows:

from tensorflow import keras
from tensorflow.keras import layers
model = keras.models.Sequential() model.add(layers.Input(shape=(1, 4))) model.add(layers.SimpleRNN(4, use_bias=False, return_sequences=True))
model.summary()

I will get 32 trainable params which makes sense. There are 4x4 recurrent connections in the SimpleRNN, and 4x4 connections between the input and the 4 RNN cells, so we get 32 trainable params (ignoring bias terms).

However, I would like to structure my network so that each input feature is connected to only one cell in the RNN, as in the diagram below:

rnn_ex.PNG

Is there a neat way to implement this in Keras? Any way to remove specific connections between layers?

Thanks!
Andrew

Reply all
Reply to author
Forward
0 new messages