我是Keras的新手,在处理RNN和LSTM时遇到了一些形状上的问题。
我正在运行这段代码:
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)
变量predictor_train是一个包含119个内部数组的numpy数组,每个内部数组都有80个不同的项。
我遇到了这个错误:
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 (119, 80).')
目前我发现RNN接收形状为(batch_size, timesteps, dimension)的三维张量。当您设置input_shape时,通常会省略batch_size,您只需要提供一个元组(timesteps, dimension)。但是,应该在代码的哪个部分更改它(如果可能,请添加更改建议的代码)?
额外信息
关于pred_frame
类型:class 'pandas.core.frame.DataFrame'
形状:(206,80)
Pred Pred Pred ...
Date
1999-01-01 NaN NaN NaN
1999-02-01 NaN NaN NaN
1999-03-01 NaN NaN NaN
1999-04-01 NaN NaN NaN
...
2015-11-01 288.333333 -0.044705 589.866667
2015-12-01 276.333333 -0.032157 1175.466667
2016-01-01 282.166667 0.043900 1458.966667
2016-02-01 248.833333 -0.082199 5018.966667
[206 rows x 80 columns]
关于target_train
类型:类 'pandas.core.series.Series'
形状:(119,)
数据类型:float64
Date
2004-10-01 0.003701
2005-05-01 0.001715
2005-06-01 0.002031
2005-07-01 0.002818
...
2015-05-01 -0.007597
2015-06-01 -0.007597
2015-07-01 -0.007597
2015-08-01 -0.007597
关于 predictor_train
类型:'numpy.ndarray'
形状:(119,80)
数据类型:float64
[[ 0.00000000e+00 -1.00000000e+00 1.03550000e-02 ..., 8.42105263e-01
6.50000000e+01 -3.98148148e-01]
[ -1.13600000e-02 -1.07482052e+00 -9.25333333e-03 ..., 4.45783133e-01
8.30000000e+01 -1.94915254e-01]
[ 4.71300000e-02 -5.14876761e+00 1.63166667e-03 ..., 4.45783133e-01
8.50000000e+01 -1.94915254e-01]
...,
[ 4.73500000e-02 -1.81092653e+00 -8.54000000e-03 ..., 1.39772727e+00
2.77000000e+02 -3.43601896e-01]
[ -6.46000000e-03 -1.13643083e+00 1.06100000e-02 ..., 2.22551929e-01
2.77000000e+02 -3.43601896e-01]
[ 3.14200000e-02 -5.86377709e+00 1.50850000e-02 ..., 2.22551929e-01
2.82000000e+02 -2.76699029e-01]]
编辑
感谢@y300,显然3D问题已经被解决。我的形状现在是(119,1,80)。
model.summary() returns the following:
--------------------------------------------------------------------------------
Initial input shape: (None, None, 119)
--------------------------------------------------------------------------------
Layer (name) Output Shape Param #
--------------------------------------------------------------------------------
SimpleRNN (Unnamed) (None, 1) 121
Total params: 121
然而,我在model.fit这一行仍然遇到了一个形状问题,如下所示:
File "/Library/Python/2.7/site-packages/theano/tensor/blas.py", line 1612, in perform
z[0] = numpy.asarray(numpy.dot(x, y))
ValueError: ('shapes (119,80) and (119,1) not aligned: 80 (dim 1) != 119 (dim 0)', (119, 80), (119, 1))
Apply node that caused the error: Dot22(Alloc.0, <TensorType(float32, matrix)>)
Inputs types: [TensorType(float32, matrix), TensorType(float32, matrix)]
Inputs shapes: [(119, 80), (119, 1)]
Inputs strides: [(320, 4), (4, 4)]
Inputs values: ['not shown', 'not shown']
为什么会发生这种情况,我该如何修复?