维数错误:预期3,形状为2(119,80)

时间:2016-03-26 22:40:36

标签: python neural-network keras

我是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是一个numpy数组,有119个内部数组,每个数组有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)的3D张量,当你设置input_shape时,通常省略batch_size,你应该只提供(时间步长,维度)的元组。但是应该更改代码的哪一部分(如果可能,添加代码更改建议)?

额外信息

  

关于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

输入:class' pandas.core.series.Series'

形状:(119,)

dtype: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)

dtype: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']

为什么会发生这种情况?我该如何解决?

1 个答案:

答案 0 :(得分:6)

您可以通过

检查模型的外观
model.summary()

在这种情况下,您应该看起来像这样(实际值可能不同):

--------------------------------------------------------------------------------
Initial input shape: (None, None, 100)
--------------------------------------------------------------------------------
Layer (name)                  Output Shape                  Param #             
--------------------------------------------------------------------------------
SimpleRNN (simplernn)         (None, 1)                     102                 
  --------------------------------------------------------------------------------
Total params: 102
--------------------------------------------------------------------------------

如您所见,输入是3D张量,而不是2D张量。因此,您需要重塑阵列以适应keras的期望。特别是,输入X_train应该有维度(num_samples,1,input_dim)。这是一个包含一些随机生成的x / y数据的工作示例:

model.add(keras.layers.SimpleRNN(init='uniform',output_dim=1,input_dim=100))
model.compile(loss="mse", optimizer="sgd")
X_train = np.random.rand(300,1,100)
y_train = np.random.rand(300)
model.fit(X=X_train, y=y_train, batch_size=32,show_accuracy=True)