TOP1 TOP5

top1----- 就是你预测的label取最后概率向量里面最大的那一个作为预测结果,如过你的预测结果中概率最大的那个分类正确,则预测正确。否则预测错误
top5----- 就是最后概率向量最大的前五名中,只要出现了正确概率即为预测正确。否则预测错误。

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)))

结果为:

数字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]

从结果上看,output中排名最高的值为0.9076816,其对应的数字为7,而实际数字为1,故不在Top1,而数字1对应的值为0.8182187,排名第二,故在Top5内

猜你喜欢

转载自blog.csdn.net/ALZFterry/article/details/111661753