How to implement a simpleRNN on a 1d toy dataset with the correct inputshape

i was trying to implement a simple RNN on a 1 dimensional toy dataset (just one x and one target variable y) to learn but I am having issue with the input shape.

X_train has input shape equal to (848,) y_train the same

my model is very simple

from keras.layers import Input, SimpleRNN, Dense
def create_rnn_model(train_size):
  inputs = Input(shape=( 1,))
  rnn = SimpleRNN(units=32, input_shape=( 1,))(inputs)
  dense1 = Dense(units=64, activation='relu')(rnn)
  dense2 = Dense(units=64, activation='relu')(dense1)
  modello = Model(inputs=inputs, outputs=dense2)
  return modello 

optimizer = tf.optimizers.SGD(learning_rate=0.0001,momentum=0.9)
rnn_model = create_rnn_model(train_size=train_size)
                  loss="mse" )

but whenever i try to fit it i get this input shape that doesn’t allow me to go further

ValueError: Input 0 of layer “simple_rnn_5” is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 1)

what is the correct way to pass the data to a simple rnn?
is there a way to pass a windows of size 10?

1 Like

I think the input has to be a 3D tensor and not a 2D one

you can see an example here:

and here: