Keras softmax output and accuracy

Viewed 1678

This is the last layer of a Keras model.

model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

I know that the output of the softmax layer is an array, with probability summing up to 1, such as [0.1, 0.4, 0.5].

I have one question about using accuracy as a metric.

e.g., when the true class is [0, 0, 1] and predicted probability is [0.1, 0.4, 0.5], even if 0.5 is the largest probability, the accuracy of this prediction should be 0, because 0.5 != 1. Is that correct?

More generally, when the output layer activation is softmax, we will normally get floating probability predictions, and in very very little chance will we get integer probability predictions like [0, 0, 1]. So we can't use accuracy as a metric when using softmax as activation. Is that correct?

1 Answers

e.g., when the true class is [0, 0, 1] and predicted probability is [0.1, 0.4, 0.5], even if 0.5 is the largest probability, the accuracy of this prediction should be 0, because 0.5 != 1. Is that correct?

No. You treat the index with the maximum value as the prediction of the model. So in your example, this sample prediction would count towards increasing the accuracy. This is normally called Top-1 accuracy. In image classification, the Top-5 accuracy is also often used (the top 5 maximum values in the softmax layer are treated as guesses of the NN and they are considered for the accuracy).

More generally, when the output layer activation is softmax, we will normally get floating probability predictions, and in very very little chance will we get integer probability predictions like [0, 0, 1]. So we can't use accuracy as a metric when using softmax as activation. Is that correct?

Technically speaking, you will never get integer values for the softmax layer output since the type is float. But yeah, there's a very teeny tiny chance of getting [0.0, 0.0, 1.0]. And this assumption of yours is incorrect since the premise does not hold. Nevertheless, accuracy is a valid metric when using Softmax as the classification layer of a neural network.

Related