10

我正在尝试运行这个 SimpleRNN:

model.add(SimpleRNN(init='uniform',output_dim=1,input_dim=len(pred_frame.columns)))
model.compile(loss="mse", optimizer="sgd")
model.fit(X=predictor_train, y=target_train, batch_size=len(pred_frame.index),show_accuracy=True)

错误出现在 model.fit 上,如下所示:

File "/Users/file.py", line 1496, in Pred
model.fit(X=predictor_train, y=target_train, batch_size=len(pred_frame.index),show_accuracy=True)
File "/Library/Python/2.7/site-packages/keras/models.py", line 581, in fit
shuffle=shuffle, metrics=metrics)
File "/Library/Python/2.7/site-packages/keras/models.py", line 239, in _fit
outs = f(ins_batch)
File "/Library/Python/2.7/site-packages/keras/backend/theano_backend.py", line 365, in __call__
return self.function(*inputs)
File "/Library/Python/2.7/site-packages/theano/compile/function_module.py", line 513, in __call__
allow_downcast=s.allow_downcast)
File "/Library/Python/2.7/site-packages/theano/tensor/type.py", line 169, in filter
data.shape))
TypeError: ('Bad input argument to theano function with name "/Library/Python/2.7/site-packages/keras/backend/theano_backend.py:362"  at index 0(0-based)', 'Wrong number of dimensions: expected 3, got 2 with shape (88, 88).')

错误告诉我它的维度数错误,它应该是 3,它只有 2。它指的是什么维度?

4

2 回答 2

10

您正在尝试运行 RNN。这意味着您希望在计算中包含以前的时间步长。为此,您必须在将数据提供给 SimpleRNN 层之前对其进行预处理。

为简单起见,我们假设不是每个具有 88 个特征的 88 个样本,而是每个具有 4 个特征的 8 个样本。现在,当使用 RNN 时,您必须决定反向传播的最大值(即计算中包含的先前时间步数)。在这种情况下,您可以选择最多包含 2 个先前的时间步。因此,为了计算 RNN 的权重,您必须在每个时间步提供当前时间步的输入(具有 4 个特征)和前 2 个时间步的输入(每个时间步具有 4 个特征)。就像在这个可视化中一样:

sequence    sample0  sample1  sample2  sample3  sample4  sample5  sample6 sample7       
   0        |-----------------------|
   1                 |-----------------------|
   2                          |-----------------------|
   3                                   |-----------------------|
   4                                             |----------------------|
   5                                                      |----------------------|

因此,不要将 (nb_samples, nb_features) 矩阵作为 SimpleRNN 的输入,您必须为其提供 (nb_sequences, nb_timesteps, nb_features) 形状的输入。在这个例子中,这意味着你给它一个 (5x3x4) 输入而不是给它一个 (8x4) 输入。

keras嵌入层可能会完成这项工作,但在这种情况下,您也可以为它编写一个短代码:

input = np.random.rand(8,4)
nb_timesteps = 3    # 2 (previous) + 1 (current)
nb_sequences = input.shape[0] - nb_timesteps    #8-3=5

input_3D = np.array([input[i:i+nb_timesteps] for i in range(nb_sequences)])
于 2016-03-30T10:07:14.230 回答
4

该错误可能是因为您的输入尺寸不是以下格式:

(nb_samples, timesteps, input_dim)

它需要 3 个维度,而您只提供 2 个维度(88,88)

于 2016-03-22T16:08:43.360 回答