QBoard » Artificial Intelligence & ML » AI and ML - Tensorflow » Error when checking target: expected dense_3 to have shape (3,) but got array with shape (1,)

Error when checking target: expected dense_3 to have shape (3,) but got array with shape (1,)

  • I am working on training a VGG16-like model in Keras, on a 3 classes subset from Places205, and encountered the following error:

    ValueError: Error when checking target: expected dense_3 to have shape (3,) but got array with shape (1,) 

    I read multiple similar issues but none helped me so far. The error is on the last layer, where I've put 3 because this is the number of classes I'm trying right now.

    The code is the following:

    import keras from keras.datasets
    import cifar10 from keras.preprocessing.image 
    import ImageDataGenerator from keras.models 
    import Sequential 
    from keras.layers import Dense, Dropout, Activation, Flatten from keras.layers import Conv2D, MaxPooling2D 
    from keras import backend as K import os
    
    
    # Constants used  
    img_width, img_height = 224, 224  
    train_data_dir='places\\train'  
    validation_data_dir='places\\validation'  
    save_filename = 'vgg_trained_model.h5'  
    training_samples = 15  
    validation_samples = 5  
    batch_size = 5  
    epochs = 5
    
    
    if K.image_data_format() == 'channels_first':
        input_shape = (3, img_width, img_height) else:
        input_shape = (img_width, img_height, 3)
    
    model = Sequential([
        # Block 1
        Conv2D(64, (3, 3), activation='relu', input_shape=input_shape, padding='same'),
        Conv2D(64, (3, 3), activation='relu', padding='same'),
        MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
        # Block 2
        Conv2D(128, (3, 3), activation='relu', padding='same'),
        Conv2D(128, (3, 3), activation='relu', padding='same'),
        MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
        # Block 3
        Conv2D(256, (3, 3), activation='relu', padding='same'),
        Conv2D(256, (3, 3), activation='relu', padding='same'),
        Conv2D(256, (3, 3), activation='relu', padding='same'),
        MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
        # Block 4
        Conv2D(512, (3, 3), activation='relu', padding='same'),
        Conv2D(512, (3, 3), activation='relu', padding='same'),
        Conv2D(512, (3, 3), activation='relu', padding='same'),
        MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
        # Block 5
        Conv2D(512, (3, 3), activation='relu', padding='same',),
        Conv2D(512, (3, 3), activation='relu', padding='same',),
        Conv2D(512, (3, 3), activation='relu', padding='same',),
        MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
        # Top
        Flatten(),
        Dense(4096, activation='relu'),
        Dense(4096, activation='relu'),
        Dense(3, activation='softmax') ])
    
    model.summary()
    
    model.compile(loss='binary_crossentropy',
                  optimizer='rmsprop',
                  metrics=['accuracy'])
    
    # no augmentation config train_datagen = ImageDataGenerator() validation_datagen = ImageDataGenerator()
         train_generator = train_datagen.flow_from_directory(
        train_data_dir,
        target_size=(img_width, img_height),
        batch_size=batch_size,
        class_mode='binary')
    
    validation_generator = validation_datagen.flow_from_directory(
        validation_data_dir,
        target_size=(img_width, img_height),
        batch_size=batch_size,
        class_mode='binary')
    
    model.fit_generator(
        train_generator,
        steps_per_epoch=training_samples // batch_size,
        epochs=epochs,
        validation_data=validation_generator,
        validation_steps=validation_samples // batch_size)
    
    model.save_weights(save_filename)​
    This post was edited by Pranav B at September 3, 2020 3:58 PM IST
      September 3, 2020 3:56 PM IST
    0
  • The problem is with your label-data shape. In a multiclass problem you are predicting the probabibility of every possible class, so must provide label data in (N, m) shape, where N is the number of training examples, and m is the number of possible classes (3 in your case).

    Keras expects y-data in (N, 3) shape, not (N,) as you've problably provided, that's why it raises an error.

    Use e.g. OneHotEncoder to convert your label data to one-hot encoded form.

      September 3, 2020 4:06 PM IST
    0
  • Had the same issue. To solve the problem you can simply change in validation_generator and train_generator the class mode from 'binary' to 'categorical' - that's because you have 3 classes-which is not binary.
      September 3, 2020 4:07 PM IST
    0
  • Problem : expected dense_3 to have shape (3,) but got array with shape (1,)

    If you are using it for classification then the number of variables should be correct in the parameter for adding a dense layer.

    variables_for_classification=5 #change it as per your number of categories
    model.add(Dense(variables_for_classification, activation='softmax'))
    
    model.fit(X_train, Y_train, epochs=epochs, batch_size=batch_size,validation_split=0.1,callbacks=[EarlyStopping(monitor='val_loss', patience=3, min_delta=0.0001)])

    To make it more clear. As I was using the LSTM to predict the category of the news and the categories were 5- business, tech, politics, sports, entertainment

    In that dense function when I put 5 it worked correctly.

      September 3, 2020 4:09 PM IST
    0

  • I also got the same error and solved it by setting class_mode as categorical instead of binary This post was edited by Jainew Nanda at September 3, 2020 4:20 PM IST
      September 3, 2020 4:18 PM IST
    0
  • The problem is with the shape of the labels of the data "Y".
    The shape you have for the labels are (m,) and this will not work with the:

    loss = "binary_crossentropy"

    I believe if you don't want to play with the shape of the labels, then use:

    loss = "sparse_categorical_crossentropy"
      September 3, 2020 4:21 PM IST
    0