花园宝宝战队 ----- 一阶段代码注释成果 #10
|
|
@ -13,11 +13,24 @@
|
|||
# limitations under the License.
|
||||
# ============================================================================
|
||||
"""Accuracy."""
|
||||
# 名为Accuracy的Python模块,其中包含了与准确率相关的功能
|
||||
# 首先,导入了numpy库,用于处理数值计算
|
||||
import numpy as np
|
||||
# 然后,从metric模块中导入了一个名为EvaluationBase的类,以及一个名为rearrange_inputs的函数,以及一个名为_check_onehot_data的函数。这些类和函数用于计算和处理准确率相关的内容
|
||||
# EvaluationBase,是metric模块中的一个基类,用于实现评估指标的基本功能
|
||||
|
||||
# rearrange_inputs,该函数用于重新排列输入数据,以便在计算评估指标时可以正确地处理数据。具体来说,它会检查输入数据是否是一个多维数组,
|
||||
# 如果是,它会将其转换为一个二维数组,其中每一行表示一个样本,每一列表示一个特征
|
||||
|
||||
# _check_onehot_data,该函数用于检查输入数据是否符合独热编码规则。具体来说,它会检查输入数据是否是一个二维数组,其中每一行表示一个样本,
|
||||
# 每一列表示一个类别。此外,它还检查输入数据中的每个类别是否是唯一的,如果不是,则会抛出一个异常
|
||||
from .metric import EvaluationBase, rearrange_inputs, _check_onehot_data
|
||||
|
||||
|
||||
class Accuracy(EvaluationBase):
|
||||
# Accuracy类,它是EvaluationBase类的子类,用于计算分类和多标签数据的精度。
|
||||
# 在计算精度时,它会创建两个本地变量,即正确数量的计数器和总数量的计数器。然后,它使用这些变量计算准确率,
|
||||
# 具体来说,它是正确数量的除数,加上正确数量的除数和假正例数量的除数和假负例数量的除数
|
||||
r"""
|
||||
Calculates the accuracy for classification and multilabel data.
|
||||
|
||||
|
|
@ -51,17 +64,27 @@ class Accuracy(EvaluationBase):
|
|||
0.6666666666666666
|
||||
"""
|
||||
def __init__(self, eval_type='classification'):
|
||||
'''
|
||||
初始化准确率
|
||||
:param eval_type: 评估类型,默认为分类
|
||||
'''
|
||||
super(Accuracy, self).__init__(eval_type)
|
||||
# 调用子类的clear方法,用于清除评估结果
|
||||
self.clear()
|
||||
|
||||
def clear(self):
|
||||
# 清空内部评估结果
|
||||
"""Clears the internal evaluation result."""
|
||||
# 正确项计数器
|
||||
self._correct_num = 0
|
||||
# 总数
|
||||
self._total_num = 0
|
||||
# 类数量
|
||||
self._class_num = 0
|
||||
|
||||
@rearrange_inputs
|
||||
def update(self, *inputs):
|
||||
# 更新本地变量。对于分类任务,如果预测值的索引与标签相匹配,则认为预测结果正确。对于多标签任务,如果预测值与标签相匹配,则认为预测结果正确
|
||||
"""
|
||||
Updates the local variables. For 'classification', if the index of the maximum of the predict value
|
||||
matches the label, the predict result is correct. For 'multilabel', the predict value match the label,
|
||||
|
|
@ -82,36 +105,53 @@ class Accuracy(EvaluationBase):
|
|||
ValueError: class numbers of last input predicted data and current predicted data not match.
|
||||
|
||||
"""
|
||||
# 检查输入参数的数量是否为2
|
||||
if len(inputs) != 2:
|
||||
# 如果不是,则抛出一个ValueError异常,表示需要2个输入参数,但实际提供了{}个
|
||||
raise ValueError("For 'Accuracy.update', it needs 2 inputs (predicted value, true value), "
|
||||
"but got {}".format(len(inputs)))
|
||||
# 将data型的输入值inputs[0]转换为numpy数组
|
||||
y_pred = self._convert_data(inputs[0])
|
||||
# 将data型的输入值inputs[1]转换为numpy数组
|
||||
y = self._convert_data(inputs[1])
|
||||
# 根据self._type的值判断是分类任务还是多标签任务
|
||||
if self._type == 'classification' and y_pred.ndim == y.ndim and _check_onehot_data(y):
|
||||
# 对于分类任务,如果y_pred和y的维度相同,并且y是onehot编码的,则将y的维度降低到一个维度
|
||||
y = y.argmax(axis=1)
|
||||
# 最后,检查y_pred和y的形状和值是否符合要求
|
||||
self._check_shape(y_pred, y)
|
||||
self._check_value(y_pred, y)
|
||||
|
||||
# 对输入的类型做出判断,并根据不同的结果做出不同的修改
|
||||
if self._class_num == 0:
|
||||
# 若输入的类别数为0,则将其赋值为预测的类别数
|
||||
self._class_num = y_pred.shape[1]
|
||||
elif y_pred.shape[1] != self._class_num:
|
||||
elif y_pred.shape[1]!= self._class_num:
|
||||
# 若预测的类别数不等于输入的类别数,则抛出异常ValueError需检查预测值出错的原因
|
||||
raise ValueError("For 'Accuracy.update', class number not match, last input predicted data contain {} "
|
||||
"classes, but current predicted data contain {} classes, please check your predicted "
|
||||
"value(inputs[0]).".format(self._class_num, y_pred.shape[1]))
|
||||
|
||||
# 根据self._type的值判断是分类任务还是多标签任务
|
||||
if self._type == 'classification':
|
||||
# 对于分类任务,将y_pred的维度降低到一个维度
|
||||
indices = y_pred.argmax(axis=1)
|
||||
# 比较y_pred和y是否相等
|
||||
result = (np.equal(indices, y) * 1).reshape(-1)
|
||||
elif self._type == 'multilabel':
|
||||
elif self._type =='multilabel':
|
||||
# 对于多标签任务,获取预测结果的维度并减一
|
||||
dimension_index = y_pred.ndim - 1
|
||||
# 将预测结果的维度改为-1
|
||||
y_pred = y_pred.swapaxes(1, dimension_index).reshape(-1, self._class_num)
|
||||
# 将标签的维度改为-1,将y_pred和y的维度降低到一个维度
|
||||
y = y.swapaxes(1, dimension_index).reshape(-1, self._class_num)
|
||||
# 将预测结果和标签做比较,并将结果改为1
|
||||
result = np.equal(y_pred, y).all(axis=1) * 1
|
||||
|
||||
# 计算result中所有元素的和,并将其加到self._correct_num和self._total_num中
|
||||
self._correct_num += result.sum()
|
||||
self._total_num += result.shape[0]
|
||||
|
||||
def eval(self):
|
||||
# 计算模型的准确性
|
||||
"""
|
||||
Computes the accuracy.
|
||||
|
||||
|
|
@ -121,8 +161,12 @@ class Accuracy(EvaluationBase):
|
|||
Raises:
|
||||
RuntimeError: If the sample size is 0.
|
||||
"""
|
||||
# 首先,检查self._total_num是否为0
|
||||
if self._total_num == 0:
|
||||
# 如果是,则抛出一个RuntimeError异常,表示无法计算准确性,因为样本数量为0
|
||||
raise RuntimeError("The 'Accuracy' can not be calculated, because the number of samples is 0, "
|
||||
"please check whether your inputs(predicted value, true value) are empty, "
|
||||
"or has called update method before calling eval method.")
|
||||
# 如果self._total_num不为0,则计算self._correct_num除以self._total_num的结果准确率,并返回该结果。
|
||||
# 注:在计算准确性时,需要确保已经调用了update方法,以便能够更新self._correct_num和self._total_num的值
|
||||
return self._correct_num / self._total_num
|
||||
|
|
|
|||
Loading…
Reference in New Issue