"Error when checking model input: you are passing a list as input to your model, but the model expects a a list of 2 Numpy arrays instead."

6,667 views
Skip to first unread message

mose.w...@gmail.com

unread,
Jun 17, 2016, 2:36:51 PM6/17/16
to Keras-users
Hi everyone!

I recently got the above bewildering error message upon attempting to train my model. Here is the complete exception:

Exception: Error when checking model input: you are passing a list as input to your model, but the model expects a a list of 2 Numpy arrays instead. The list you passed was: [[array([  1.11022302e-16,  -3.72734042e-08,  -7.45453583e-08,
        -1.11815772e-07,  -1.11022302e-16,  -2.88746119e-08,
        -5.77451024e-08,  -8.66114245e-08]), array([  1.11022302e-16,  -3.72

As one can see, my input is a list of 2 Numpy arrays. Can someone give a clue as to what might be going on here and/or how to fix it? Thanks very much!

Mose

Bruno Guberfain do Amaral

unread,
Jun 17, 2016, 2:45:57 PM6/17/16
to Keras-users
It seems that you are passing an 'array of array' as input. Notice the double '[' at the begining: `[[array ...`.

Try to inspect the `len` of your input (it should return 2). 

Em sexta-feira, 17 de junho de 2016 15:36:51 UTC-3, Mose Win
tner escreveu:

Mose Wintner

unread,
Jun 17, 2016, 5:50:13 PM6/17/16
to Keras-users
Hi Bruno!

The length of each input is indeed 2 (there are two inputs to my network). The exception itself recognizes the input as a list and not an array, but I was able to verify it as a list manually as well. Any other ideas? Thanks!

Bruno Guberfain do Amaral

unread,
Jun 17, 2016, 8:44:02 PM6/17/16
to Mose Wintner, Keras-users
Can you show us your model.summary() (at least the lines regarding the input layers) and the shape of each np.array you are passing to the fit method?
--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/e12a648e-57ef-452a-8d2e-ccabf22455ce%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

François Chollet

unread,
Jun 17, 2016, 9:42:55 PM6/17/16
to Bruno Guberfain do Amaral, Mose Wintner, Keras-users
The model expects a list of 2 arrays. You are *not* passing a list of two arrays (looks at what gets printed). Hence the error. It seems to me that the error message if perfectly clear.


Mose Wintner

unread,
Jun 18, 2016, 2:20:04 AM6/18/16
to Keras-users, mose.w...@gmail.com
    from keras.models import Sequential
   
from keras.layers import Merge, Dense, Activation
   
from keras.optimizers import SGD
   
    nu
=4
    ny
=4
    nhidden
=4


    thetaHat
= Sequential([
       
Dense(nhidden, input_dim=nu+ny),
       
Activation('softmax'),
       
Dense(nhidden),
       
Activation('relu'),
       
Dense(nu)
       
])
       
    thetaHat
.summary()
   
    inputVector
=Sequential()
    inputVector
.add(Dense(nu, input_dim=nu))
   
    inputVector
.summary()
   
    dotted
=Merge([thetaHat, inputVector] , mode='dot', dot_axes=1)
   
   
out = Sequential()
   
out.add(dotted)
   
    sgd
= SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)

   
out.compile(loss='mse', optimizer=sgd, metrics=['accuracy'])    

Running the above yields:

___________________________________________________________________________________________________
Layer (type)                       Output Shape        Param #     Connected to                    
====================================================================================================
dense_1
(Dense)                    (None, 4)           36          dense_input_1[0][0]              
____________________________________________________________________________________________________
activation_1
(Activation)          (None, 4)           0           dense_1[0][0]                    
____________________________________________________________________________________________________
dense_2
(Dense)                    (None, 4)           20          activation_1[0][0]              
____________________________________________________________________________________________________
activation_2
(Activation)          (None, 4)           0           dense_2[0][0]                    
____________________________________________________________________________________________________
dense_3
(Dense)                    (None, 4)           20          activation_2[0][0]              
====================================================================================================
Total params: 76
____________________________________________________________________________________________________
____________________________________________________________________________________________________
Layer (type)                       Output Shape        Param #     Connected to                    
====================================================================================================
dense_4
(Dense)                    (None, 4)           20          dense_input_2[0][0]              
====================================================================================================
Total params: 20
____________________________________________________________________________________________________


Additionally,

    print type(i_train)
   
print type(i_train[0])
   
print type(i_train[0][0])
   
print type(i_train[0][1])
   
print len(i_train)
   
print len(i_train[0])
   
print len(i_train[0][0])
   
print len(i_train[0][1])
   
print len(i_train[1])
   
print i_train[0][0].shape
   
#    out.fit(i_train, o_train, batch_size='128', verbose=1)


gives me

<type 'list'>
<type 'list'>
<type 'numpy.ndarray'>
<type 'numpy.ndarray'>
2144970
2
8
4
2
(8L,)

Please let me know if I can clarify anything more. Thanks!

Carlos Bentes

unread,
Jun 18, 2016, 5:39:22 AM6/18/16
to Keras-users, mose.w...@gmail.com
Hi,

maybe this helps:

array_1 = np.array(range(100*(nu+ny))).reshape(100,nu+ny) # thetaHat
array_2 = np.array(range(100*nu)).reshape(100,nu) # inputVector

list_of_2_arrays = [array_1, array_2]
i_train = list_of_2_arrays


cheers,
cb

Bruno Guberfain do Amaral

unread,
Jun 18, 2016, 8:59:06 AM6/18/16
to Carlos Bentes, Keras-users, Mose Wintner
Hi Mose,

I think Carlos has the answer. But to make thinks clear, you should pass a list of two numpy arrays: the first one is the input of thetaHat and the second the input of inputVector.

These inputs must have dimensions: (None, nu+ny) and (None, nu). Here 'None' may be any dimension (the length of your input).

As a final thought, I'd suggest you to refactor your code using the Functional API: http://keras.io/getting-started/functional-api-guide/
Take a look at the "Multi-input and multi-output models" section for a clear example on how to use multiple inputs.

Regards,
Bruno

Mose Wintner

unread,
Jun 20, 2016, 7:24:02 PM6/20/16
to Bruno Guberfain do Amaral, Carlos Bentes, Keras-users
Thanks all!

I think I've worked out the issue. I am essentially working with one-dimensional time series; I suspect I may have intended for the length of the input to be nu+ny (resp. nu) and for the input dimension to be 1 (resp. 1).

If I do understand correctly, then my i_train should be a list of two numpy arrays. Should, say, i_train[0] then be an array, where

i_train[0][any_index].shape = (nu+ny, 1) ?

Was my assessment of the initial issue correct?

Thanks again for your patient guidance.

Bruno Guberfain do Amaral

unread,
Jun 20, 2016, 8:26:28 PM6/20/16
to Mose Wintner, Carlos Bentes, Keras-users
Actually:
i_train[0][any_index].shape = (nu+ny,)
Reply all
Reply to author
Forward
0 new messages