Search code examples
pythonkerasargmax

Argmax in a Keras multiclassifier ANN


I am trying to code a 5 class classifier ANN, and this code return this error:

    classifier = Sequential()
    
    classifier.add(Dense(units=10, input_dim=14, kernel_initializer='uniform', activation='relu'))
    
    classifier.add(Dense(units=6, kernel_initializer='uniform', activation='relu'))
    
    classifier.add(Dense(units=5, kernel_initializer='uniform', activation='softmax'))
    
    classifier.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
    
    RD_Model = classifier.fit(X_train,y_train, batch_size=10 , epochs=10, verbose=1)


File "c:\Program Files\Python310\lib\site-packages\keras\backend.py", line 5119, in categorical_crossentropy
        target.shape.assert_is_compatible_with(output.shape)
    ValueError: Shapes (None, 1) and (None, 5) are incompatible

I figured this is caused because I have a probability matrix instead of an actual output, so I have been trying to apply an argmax, but haven't figured a way

Can someone help me out?


Solution

  • As Dr. Snoopy mentioned, it was indeed a problem of one-hot encoding... I missed to do that, resulting in my model not working.

    So I just one hot encoded it:

    encoder = LabelEncoder()
    encoder.fit(y_train)
    encoded_Y = encoder.transform(y_train)
    # convert integers to dummy variables (i.e. one hot encoded)
    dummy_y = np_utils.to_categorical(encoded_Y)
    

    And it worked after using dummy_y. Thank you for your help.