维数错误:预期为 3,得到 2,形状为 (119, 80)
Wrong number of dimensions: expected 3, got 2 with shape (119, 80)
我是 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) 的 3D 张量,当您设置 input_shape 时,通常会省略 batch_size ,你应该只提供一个元组(时间步长,维度)。但是应该更改代码的哪一部分(如果可能,请添加您的代码更改建议)?
额外信息
About 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]
About 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
About 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']
为什么会这样,我该如何解决?
您可以通过
检查您的模型的外观
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)
我是 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) 的 3D 张量,当您设置 input_shape 时,通常会省略 batch_size ,你应该只提供一个元组(时间步长,维度)。但是应该更改代码的哪一部分(如果可能,请添加您的代码更改建议)?
额外信息
About 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]
About 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
About 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']
为什么会这样,我该如何解决?
您可以通过
检查您的模型的外观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)