Muhammad Zaim
Muhammad Zaim

Reputation: 1

A target array with shape (64, 4) was passed for an output of shape (None, 3) while using as loss `binary_crossentropy`

# Organize file names and class labels in X and Y variables
prepareNameWithLabels(classLabels[0])
prepareNameWithLabels(classLabels[1])
prepareNameWithLabels(classLabels[2])
prepareNameWithLabels(classLabels[3])          

X=np.asarray(X)
Y=np.asarray(Y)


# learning rate
batch_size = 64
epoch=50
activationFunction='relu'
def getModel():
    model = Sequential()
    model.add(Conv2D(64, (3, 3), padding='same', activation=activationFunction, input_shape=(img_rows, img_cols, 3)))
    model.add(Conv2D(64, (3, 3), activation=activationFunction))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))

    model.add(Conv2D(32, (3, 3), padding='same', activation=activationFunction))
    model.add(Conv2D(32, (3, 3), activation=activationFunction))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))

    model.add(Conv2D(16, (3, 3), padding='same', activation=activationFunction))
    model.add(Conv2D(16, (3, 3), activation=activationFunction))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Dense(64, activation=activationFunction)) # we can drop 
    model.add(Dropout(0.1))                  # this layers
    model.add(Dense(32, activation=activationFunction))
    model.add(Dropout(0.1))
    model.add(Dense(16, activation=activationFunction))
    model.add(Dropout(0.1))
    model.add(Dense(3, activation='softmax')) 

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

    return model

The Following Errors Pops Out

ValueError: A target array with shape (64, 4) was passed for an output of shape (None, 3) while using as loss binary_crossentropy. This loss expects targets to have the same shape as the output.

Upvotes: 0

Views: 1165

Answers (2)

Saurabh Chandra
Saurabh Chandra

Reputation: 21

You have 4 separate classes. So, your last layer (output layer) should have 4 neurons, not 3 neurons. Change output units to 4.

model.add(Dense(4, activation='softmax')) # Last layer

In model.compile() please change your loss function.

  • If your labels are is in binary form i.e. 0 or 1, use BinaryCrossentropy()
  • If your labels are multi-class classification and they are one-hot encoded, use CategoricalCrossentropy()
  • If your labels are multi-class classification and they are in integer form use SparseCategoricalCrossentropy()

Upvotes: 0

Kaveh
Kaveh

Reputation: 4960

As your code reflects, you have 4 separate classes. So, your last layer (output layer) should have 4 neurons, but you have specified 3. Change output units to 4.

Additionally, Your model output has more than one neuron, but your loss function is binary_crossentropy. Note that you can only use binary_crossentropy if you have only one output as with value 0 and 1, or you have multi output for multi-label problems (It is possible more than one class at the same time activated, not limited to only one class).

If you have multiple class classification, and your targets (y_train) are one hot encoded you may use categorical_crossentropy and if it is not one hot encoded you can use sparse_categorical_crossentropy as loss function.

Upvotes: 2

Related Questions