Keras simple RNN implementation

大兔子大兔子 提交于 2019-12-06 01:02:36

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/46256257/keras-simple-rnn-implementation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!