0

I'm trying to compile a dataset with Sequential() with the Keras module, but I get back a value error:

ValueError: Error when checking model input: expected dense_input_1 to have shape (None, 33) but got array with shape (32, 36)

I went through my code many times, but couldn't find any possible error.

I have a dataset with 32 items, all which are converted into floats.

Here is the code for my neural network:

# Build neural network
# Sequential
model = Sequential()

# Neural network
model.add(Dense(36, input_dim=34, init='uniform', activation='sigmoid' ))
model.add(Dense(32, init='uniform', activation='sigmoid'))
model.add(Dense(32, init='uniform', activation='sigmoid'))
model.add(Dense(32, init='uniform', activation='sigmoid'))
model.add(Dense(33, init='uniform', activation='sigmoid'))

# Compile model
model.compile(loss='mean_squared_logarithmic_error', optimizer='SGD', metrics=['accuracy'])

# Fit model
history = model.fit(X, Y, nb_epoch=20, validation_split=0.2, batch_size=3)

Here is the complete error message I received:

Traceback (most recent call last):
  File "/Users/cliang/Desktop/Laurence/Python/Programs/Python/Collaborative_Projects/Cancer_screening/neural_network_alls_1.py", line 111, in <module>
    history = model.fit(X, Y, nb_epoch=20, validation_split=0.2, batch_size=3)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/models.py", line 672, in fit
    initial_epoch=initial_epoch)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py", line 1116, in fit
    batch_size=batch_size)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py", line 1029, in _standardize_user_data
    exception_prefix='model input')
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py", line 124, in standardize_input_data
    str(array.shape))
ValueError: Error when checking model input: expected dense_input_1 to have shape (None, 34) but got array with shape (32, 36)

1 Answer 1

1

As the error say, you have a miss match between your input data shape and the first layer. You clearly define that your input dimension is (number of features) is 34 input_dim=34, though you are passing data with 36 features.

I think you have confused between the number of neurons of the hidden layer 36 and your input data 34. Either you drop two column from your data or change input_dim=36.

Sign up to request clarification or add additional context in comments.

2 Comments

34 is the length of the input vector, 36 is the number of neurons in the first dense layer (hence the length of the output of this layer), not the number of hidden layers.
@ML_TN thank you! I eventually found two things that went wrong. The error was there because there wasn't an output layer with just one neuron (which made the program run without error). Secondly, as you said, there was a mismatch between the input data and the first layer (I was using data from the wrong file). Thank you for your help @ML_TN!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.