Andy
Andy

Reputation: 366

How to apply top_k_categorical_accuracy over batch dimension in Keras

I am making a custom metric in keras, based on top_k_categorical_accuracy. In my custom metric function I receive y_true and pred (two tensors) with 3 dimensions, having a shape of (batch_size, d2, d3), but apparently top_k_categorical_accuracy expects a 2-d tensor.

tf.keras.metrics.top_k_categorical_accuracy(y_true, y_pred, k=2)

My question is how can I apply this top_k function across different batches?

In the example below I would expect the output of the metric to be 1/2 (with k=2).

This would be done by taking the K.mean of top_k_categorical_accuracy(y_true[0], y_pred[0]) (1st batch gives 2/3) and top_k_categorical_accuracy(y_true[1], y_pred[1]) (2nd batch gives 1/3). So the mean would be 1/2

y_true = [
    [[0, 0, 1], [0, 1, 0], [1, 0, 0]],
    [[0, 0, 1], [0, 1, 0], [1, 0, 0]]
]
y_pred = [
    [[0.1, 0.7, 0.2], [0.05, 0.95, 0], [0.2,0.3,0.5]],
    [[0.7, 0.2, 0.1], [0.95, 0, 0.05], [0.3,0.2,0.5]]
]

Upvotes: 0

Views: 345

Answers (1)

zwang
zwang

Reputation: 357

Since only the last dimension is actual class predictions, you can reshape the first two dimensions into one using K.reshape:

y_true = K.reshape(y_true, shape=(-1,3))
y_pred = K.reshape(y_pred, shape=(-1,3))

Then the tensors will meet the API's shape requirements and produce an average score across batch*d1, which is also average across batch as you requested since each batch has the same number of d1.

Upvotes: 1

Related Questions