Search code examples
pythonpython-2.7machine-learningkerastraining-data

Keras shape of features for training


I'm trying to train nn with keras train_on_batch function. I have 39 features and want a batch to contain 32 samples. So I have a list of 32 numpy arrays for every training iteration.

So here is my code(here every batch_x is a list of 32 numpy array each containing 39 features):

input_shape = (39,)

model = Sequential()
model.add(Dense(39, input_shape=input_shape)) # show you is only first layer
... 

for batch_x, batch_y in train_gen:
    model.train_on_batch(batch_x, batch_y)

But suddenly I got an error:

Exception: Error when checking model input: the list of Numpy arrays
that you are passing to your model is not the size the model expected.
Expected to see 1 arrays but instead got the following list of 32 arrays:

I'm not really sure what's wrong.

P.S. I also tried different input_shape such as (32, 39), (39, 32) etc.


Solution

  • You don't want 32 arrays of size 39, you want one array of size (32, 39).

    So you must change input_shape to (None, 39), the None allowing you to dynamically change your batch_size, and change batch_x to be a numpy array of shape (32, 39).