question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ValueError: Error when checking target: expected dense_14 to have shape (None, 2) but got array with shape (928, 1)

See original GitHub issue

I am working through the keras transfer learning tutorial here : https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html using Keras with a tensorflow backend. My data is made up training data (499 and 443 images of class 0 and 1) and validation data (101 and 103 image of class 0 and 1)

When I try and run the block of code below I receive the error

ValueError: Error when checking target: expected dense_14 to have shape (None, 2) but got array with shape (928, 1)

My understanding of the structure is that my input of (928,4,4,512) which sets the input shape to the flatten_9 layer at (none, 8192) but I am confused as why is causes and error at the dense_14 layer as the size of the hidden layers is already defined?

my model configuration is

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=======================================
flatten_9 (Flatten)          (None, 8192)              0         
_________________________________________________________________
dense_13 (Dense)             (None, 256)               2097408   
_________________________________________________________________
dropout_7 (Dropout)          (None, 256)               0         
_________________________________________________________________
dense_14 (Dense)             (None, 2)                 514       
========================================
def train_top_model():
    train_data = np.load(open('bottleneck_features_train.npy','rb'))
    train_labels = np.array(
        [0] * 499 + [1] * 443)

    validation_data = np.load(open('bottleneck_features_validation.npy','rb'))
    validation_labels = np.array(
        [0] * 101 + [1] * 103)
    
    model = Sequential()
    model.add(Flatten(input_shape=train_data.shape[1:]))
    model.add(Dense(256, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(2, activation='sigmoid'))

    model.compile(optimizer='rmsprop',
                  loss='binary_crossentropy', metrics=['accuracy'])

    model.fit(train_data, train_labels,
              epochs=epochs,
              batch_size=batch_size,
              validation_data=(validation_data, validation_labels))
    model.save_weights(top_model_weights_path)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:22

github_iconTop GitHub Comments

26reactions
shreyashk09commented, Mar 10, 2018

use “keras.utils.np_utils.to_categorical” to convert your train_labels to categorical one-hot vectors.

17reactions
ghostcommented, May 17, 2018

In my case, the loss parameter in the compiling the model was specified as “sparse_categorical_crossentropy”. When I changed it to “categorical_crossentropy”, the error was fixed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

expected dense_Dense2 to have shape x, but got array with ...
Unfortunately it does not work. Yes it fixes the error, but after happens new error "Error when checking target: expected dense_Dense2 to have...
Read more >
Error when checking target: expected dense_3 to have shape ...
I am working on training a VGG16-like model in Keras, on a 3 classes subset from Places205, and encountered the following error: ValueError: ......
Read more >
How to solve "ValueError: Error when checking target ...
Hi Everyone, I am new to Deep Learning and I tried some code with ... expected dense_14 to have shape (8,) but got...
Read more >
expected Output to have 2 dimensions, but got array with ...
I got help from someone. He mentions changing LSTM to return_sequences. # Input inputs = Input(shape=(max_length,), name="Input" ...
Read more >
expected dense_3 to have shape (3,) but got array ... - iTecNote
ValueError : Error when checking target: expected dense_3 to have shape (3,) but got array with shape (1,). I read multiple similar issues...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found