问题描述
当尝试用一个循环层编译网络时,我发现了问题.似乎第一层的维数存在一些问题,因此我对Keras中RNN层的工作方式有所了解.
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.
我的代码示例是:
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))
错误是
ValueError: Input 0 is incompatible with layer simple_rnn_1: expected ndim=3, found ndim=2
无论input_dim
值如何,都会返回此错误.我想念什么?
This error is returned regardless of input_dim
value. What am I missing ?
推荐答案
该消息表示:进入rnn的输入具有2个维,但是rnn层需要3个维.
That message means: the input going into the rnn has 2 dimensions, but an rnn layer expects 3 dimensions.
对于RNN层,您需要输入形状类似于(BatchSize, TimeSteps, FeaturesPerStep)
的输入.这些是预期的3个维度.
For an RNN layer, you need inputs shaped like (BatchSize, TimeSteps, FeaturesPerStep)
. These are the 3 dimensions expected.
Dense
层(在keras 2中)可以使用2或3维.我们可以看到您正在使用2,因为您传递了input_dim
而不是传递了input_shape=(Steps,Features)
.
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.
解决方案1-您的训练数据是一个序列:
如果训练数据是序列,则可以像(NumberOfSamples, TimeSteps, Features)
那样对它进行整形并将其传递给模型.确保在第一层中使用input_shape=(TimeSteps,Features)
,而不是使用input_dim
.
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
.
解决方案2-重塑第一个致密层的输出,使其具有附加尺寸:
model.add(Reshape((TimeSteps,Features)))
确保乘积TimeSteps*Features
等于第一个密集层的输出8
.
Make sure that the product TimeSteps*Features
is equal to 8
, the output of your first dense layer.
这篇关于Keras简单的RNN实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!