0

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?

2 Answers2

1

Have you tried applying:

tf.keras.backend.argmax()

You can define a lambda layer using the following:

from keras.layer import Lambda
from keras import backend as K

def argmax_layer(input):
  return K.argmax(input, axis=-1)

Keras provides two paradigms for defining a model topology. The code you are using uses the Sequential API. You might have to revert to the Functional API.

input_layer = Input(shape=(14,))
layer_1 = Dense(10, activation="relu")(input_layer)
layer_2 = Dense(6, activation="relu")(layer_1)
layer_3 = argmax_layer()(layer_2 )
output_layer= Dense(5, activation="linear")(layer_3 )

model = Model(inputs=input_layer, outputs=output_layer)

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

Another option would be to instantiate an inherited class of a Keras Layer. https://www.tutorialspoint.com/keras/keras_customized_layer.htm

YScharf
  • 1,638
  • 15
  • 20
  • I have tried to put it as you showed, but I couldn't figure what to put in as "argmax_layer()" input here, as it require one. I tried to put "name='x'" in the first step and then use x as input argument, but wouldn't work. I also tried to put layer_2 as input, without real belief and yeah, not working either. Would you know what to use as input argument? I am really new to this so I probably miss a point. I will try to implement the link you sent as a custom function but as I just said, really new to this and this seems a bit harder than what I can do right now. Will try anyway! Thank you! – Timothée D Jul 25 '22 at 22:42
1

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.