Skip to content Skip to sidebar Skip to footer

Keras Merging Class Prediction With Labels

When training my network, I have a multi label classification problem in which I convert the class labels into one hot encoding. After training the model, and generating prediction

Solution 1:

First of all, if you are doing multi-label classification, then you should use the binary_crossentropy loss:

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

Then it is important to say that keras' accuracy does not consider multi-label classification, so it will be a misleading metric. More appropriate metrics are precision/recall for each class.

To get class predictions, you have to threshold each class' predictions, and it is a threshold that you have to tune (it does not have to be the same for each class), so for example:

class_names = y.columns.tolist()
pred_classes = {}
preds = model.predict(pred_test_input)

thresh = 0.5
for i in range(num_classes):
    if preds[i] > thresh:
        pred_classes[class_name[i]] = preds[i]

This will output the pred_classes dictionary with the classes over the threshold, and include a confidence score.

Post a Comment for "Keras Merging Class Prediction With Labels"