Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras simple RNN implementation

I found problems when trying to compile a network with one recurrent layer. It seems there is some issue with the dimensionality of the first layer and thus my understanding of how RNN layers work in Keras.

My code sample is:

model.add(Dense(8,
                input_dim = 2,
                activation = "tanh",
                use_bias = False))
model.add(SimpleRNN(2,
                    activation = "tanh",
                    use_bias = False))
model.add(Dense(1,
                activation = "tanh",
                use_bias = False))

The error is

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

This error is returned regardless of input_dim value. What am I missing ?

like image 685
dev1223 Avatar asked Sep 16 '17 16:09

dev1223


People also ask

What is simple RNN model?

RNN model requires a step value that contains n number of elements as an input sequence. Here, we define it as a 'step'. This is an important part of RNN so let's see an example: x has the following sequence data. x = [1,2,3,4,5,6,7,8,9,10]


1 Answers

That message means: the input going into the rnn has 2 dimensions, but an rnn layer expects 3 dimensions.

For an RNN layer, you need inputs shaped like (BatchSize, TimeSteps, FeaturesPerStep). These are the 3 dimensions expected.

A Dense layer (in keras 2) can work with either 2 or 3 dimensions. We can see that you're working with 2 because you passed an input_dim instead of passing an input_shape=(Steps,Features).

There are many possible ways to solve this, but the most meaningful and logical would be a case where your input data is a sequence with time steps.

Solution 1 - Your training data is a sequence:

If your training data is a sequence, you shape it like (NumberOfSamples, TimeSteps, Features) and pass it to your model. Make sure you use input_shape=(TimeSteps,Features) in the first layer instead of using input_dim.

Solution 2 - You reshape the output of the first dense layer so it has the additional dimension:

model.add(Reshape((TimeSteps,Features)))

Make sure that the product TimeSteps*Features is equal to 8, the output of your first dense layer.

like image 147
Daniel Möller Avatar answered Oct 14 '22 12:10

Daniel Möller