Keras Input Error

I’m trying to build a very simple model of some numerical data.

My model is as follows:

model = Sequential()
model.add(Dense(32, activation=‘relu’, input_dim=1))
model.add(Dense(6, activation=‘sigmoid’))
model.compile(optimizer=‘rmsprop’, loss=‘binary_crossentropy’, metrics = [‘accuracy’]), y_train, epochs=10)

x_train is a numpy.ndarray with size 236. Each element of x_train is a list containing 218 elements, all of which are numpy.float64.
y_train is a one-hot encoded numpy.ndarray with size 236. Each element is a list with 6 elements.

I’ve tried with x_train as a list also, just in case that would help, but I got the same error. I’m kind of stuck, and haven’t found a good answer on google.

I get a long error:
Epoch 1/10
ValueError Traceback (most recent call last)
in ()
3 model.add(Dense(6, activation=‘sigmoid’))
4 model.compile(optimizer=‘rmsprop’, loss=‘binary_crossentropy’, metrics = [‘accuracy’])
----> 5, y_train, epochs=10)

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/ in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, **kwargs)
    851                               class_weight=class_weight,
    852                               sample_weight=sample_weight,
--> 853                               initial_epoch=initial_epoch)
    855     def evaluate(self, x, y, batch_size=32, verbose=1,

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/ in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, **kwargs)
   1484                               val_f=val_f, val_ins=val_ins, shuffle=shuffle,
   1485                               callback_metrics=callback_metrics,
-> 1486                               initial_epoch=initial_epoch)
   1488     def evaluate(self, x, y, batch_size=32, verbose=1, sample_weight=None):

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/ in _fit_loop(self, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch)
   1139                 batch_logs['size'] = len(batch_ids)
   1140                 callbacks.on_batch_begin(batch_index, batch_logs)
-> 1141                 outs = f(ins_batch)
   1142                 if not isinstance(outs, list):
   1143                     outs = [outs]

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/backend/ in __call__(self, inputs)
   1120     def __call__(self, inputs):
   1121         assert isinstance(inputs, (list, tuple))
-> 1122         return self.function(*inputs)

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/theano/compile/ in __call__(self, *args, **kwargs)
    793               [0] = s.type.filter(
    794                             arg, strict=s.strict,
--> 795                             allow_downcast=s.allow_downcast)
    797                     except Exception as e:

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/theano/tensor/ in filter(self, data, strict, allow_downcast)
    115             if allow_downcast:
    116                 # Convert to self.dtype, regardless of the type of data
--> 117                 data = theano._asarray(data, dtype=self.dtype)
    118                 # TODO: consider to pad shape with ones to make it consistent
    119                 # with self.broadcastable... like vector->row type thing

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/theano/misc/ in _asarray(a, dtype, order)
     32         dtype = theano.config.floatX
     33     dtype = np.dtype(dtype)  # Convert into dtype object.
---> 34     rval = np.asarray(a, dtype=dtype, order=order)
     35     # Note that dtype comparison must be done by comparing their `num`
     36     # attribute. One cannot assume that two identical data types are pointers

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/numpy/core/ in asarray(a, dtype, order)
    530     """
--> 531     return array(a, dtype, copy=False, order=order)

ValueError: Bad input argument to theano function with name "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/backend/" at index 0 (0-based).  
Backtrace when that variable is created:

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/IPython/core/", line 2717, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/IPython/core/", line 2821, in run_ast_nodes
    if self.run_code(code, result):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/IPython/core/", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-201-84fb2cbef217>", line 2, in <module>
    model.add(Dense(32, activation='relu', input_dim=1))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/", line 426, in add
    dtype=layer.dtype, + '_input')
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/", line 1392, in Input
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/", line 1303, in __init__
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/backend/", line 184, in placeholder
    x = T.TensorType(dtype, broadcast)(name)
setting an array element with a sequence.
1 Like

It looks like you’ve specified the wrong input shape to your first layer. You said that x_train is of shape (236, 218), where 236 is the number of samples and 218 is the number of input features per sample. So, try replacing your second line with this:

model.add(Dense(32, input_shape=(218,), activation='relu'))

By the way, if your y_train vector is one-hot encoded, then you probably want to change the activation on your output layer as well so that predictions approximate the one-hot encoding (i.e., one element with a high probability and all others close to zero). For that task, you would want to specify activation='softmax' on your last layer. (There’s a good discussion of softmax activation in the Lesson 3 notes.)


Ah, yes, thanks for the suggestion on softmax. I should have recalled that.

also, I had something like what you’ve listed (218, ) before I got to this answer. Changing the input shape gives me the error:

ValueError: Error when checking model input: expected dense_44_input to have 3 dimensions, but got array with shape (236, 1)

Other input shapes that didn’t work - (236,1), (236,) etc. Finally when I used input_dim = 1, I moved on from various versions of this error, to this new one.

EDIT: note that the ‘dense_44_input’ portion of the error often changes. For example, I tried again with (218, ) and it gave me dense_48_input

It sounds like your input isn’t in the form you think it is. What do you see when you run x_train.shape in your interpreter?

This is an auto-generated name for your input layer. If you don’t specify a name for your layers, Keras will create them for you. You’ll find that they increment as you instantiate new layers.

x_train.shape returns (removed a couple of vectors from the pre-train-test-split data since the above convo):


x is type numpy.ndarray

If I try with input_shape = (233, ) I get:
ValueError: Error when checking model input: expected dense_64_input to have shape (None, 233) but got array with shape (233, 1)

If I try with input_shape = (233, 1) I get:

ValueError: Error when checking model input: expected dense_62_input to have 3 dimensions, but got array with shape (233, 1)

So at least that’s a little different =)

There’s your problem! Your input data is just a 1d array with 233 numbers in it. Based on your earlier description, you should be seeing a shape of (236, 218). So I would recommend taking a look at your data preprocessing steps to see why it’s winding up in this form.

This won’t fix your problem, because the issue is with your data preprocessing. But I did want to point out one quirk of the Keras API that might be causing some confusion.

When you specify input_shape in Keras, you should omit the batch dimension (the number of samples). Whatever shape tuple you pass in, Keras will assume that that’s the shape of each individual sample. So, when you write input_shape = (233,), you’re telling Keras: “However many samples there may be in my data, each one will be input as a flat list of 233 values.” If you write input_shape=(233,1), you’re telling Keras that each sample will be input as an array of 233 1-element arrays (like [ [1.232], [3.222], [6.331], … ]).

Haha, thanks a lot Shawn. I’ll never forget this simple lesson. I was just so confused, because my array had 233 elements, and each of those elements had 218 elements, but definitely not the same.

In case anyone else ever finds this thread, I used the following to change from an array where each element was a list, to a proper array.
proper_array = np.array(arr_of_lists.tolist())