TOP1 TOP5

top1----- is that the label you predicted takes the largest one in the final probability vector as the prediction result. If the classification with the largest probability in your prediction result is correct, the prediction is correct. Otherwise, the prediction error
top5----- is the top five with the largest probability vector at the end. As long as the correct probability appears, the prediction is correct. Otherwise, the prediction is wrong.

import numpy as np
import tensorflow.keras.backend as K

# 随机输出数字0~9的概率分布
output = K.random_uniform_variable(shape=(1, 10), low=0, high=1)
# 实际结果假设为数字1
actual_pos = K.variable(np.array([1]), dtype='int32')
print("数字0~9的预测概率分布为:", K.eval(output))
print("实际结果为数字:", K.eval(actual_pos))
print("实际结果是否in top 1: ", K.eval(K.in_top_k(output, actual_pos, 1)))
print("实际结果是否in top 5: ", K.eval(K.in_top_k(output, actual_pos, 5)))

The result is:

数字0~9的预测概率分布为: [[0.301023   0.8182187  0.71007144 0.80164504 0.7268218  0.58599055 0.19250274 0.9076816  0.8101771  0.49439466]]
实际结果为数字: [1]
实际结果是否in top 1:  [False]
实际结果是否in top 5:  [ True]

From the results, the highest ranked value in output is 0.9076816, its corresponding number is 7, and the actual number is 1, so it is not in Top1, and the value corresponding to number 1 is 0.8182187, ranking second, so it is in Top5

Guess you like

Origin blog.csdn.net/ALZFterry/article/details/111661753