Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong number of dimensions: expected 3, got 2 with shape (119, 80)

I'm new to Keras and having some trouble with shapes, specially when it comes to RNNs and LSTMs.

I'm running this code:

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)

The variable predictor_train is a numpy array with 119 inner arrays, each one having 80 different items.

I'm having this error:

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).')

So far what I found out is that an RNN receives 3D tensor with shape of (batch_size, timesteps, dimension) and when you set input_shape the batch_size is usually omitted, and you should just provide a tuple of (timesteps, dimension). But in what part of the code should that be changed (if possible, add your code changing suggestions)?


Extra info

About pred_frame

type: class 'pandas.core.frame.DataFrame'

shape: (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

type: class 'pandas.core.series.Series'

shape: (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

type: 'numpy.ndarray'

shape: (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]]

Edit

Thanks to @y300 apparently the 3d problem is surpassed. My shape now is (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

However, I'm still getting a shaping problem in the model.fit line, as you can see below:

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

Why is it happening and how could I fix it?

like image 501
aabujamra Avatar asked Mar 26 '16 22:03

aabujamra


1 Answers

You can check what your model looks like by doing

model.summary()

In this case, your should look something like this (actual values may differ):

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

As you can see, the input is a 3D tensor, not a 2D one. So you need to reshape your arrays to fit what keras is expecting. In particular, the input X_train should have dimensions (num_samples,1,input_dim). Here's a working example with some randomly generated x/y data:

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)
like image 166
yhenon Avatar answered Nov 16 '22 03:11

yhenon