ValueError: Shapes (None, 3) and (None, 16) are incompatible

The last layer of a neural network with a sigmoid activation function in a multiclass classification problem has to reflect the number of classes that are being predicted. Here 3 classes are being used so just change it from 16 to 3.

def nn_model(max_len):
    model = Sequential()
    model.add(Dense(32, 
                    activation="elu",
                    input_shape=(max_len,)))
    model.add(Dense(1024, activation="elu"))
    model.add(Dense(512, activation="elu"))
    model.add(Dense(256, activation="elu"))
    model.add(Dense(128, activation="elu"))
    model.add(Dense(3))
    model.add(Activation("sigmoid"))
    
    model.summary() 
    
    model.compile(optimizer='adam', 
                  loss='categorical_crossentropy',
                  metrics = ['accuracy', precision, recall])

    return model

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top