mindsponge.metrics.MultiClassFocal

查看源文件
class mindsponge.metrics.MultiClassFocal(num_class, beta=0.99, gamma=2.0, e=0.1, neighbors=2, not_focal=False, reducer_flag=False)[源代码]

计算多分类中预测值和真实值之间的焦点损失,详细实现过程参考: Lin, Tsung-Yi, et al. 'Focal loss for dense object detection'

参数:
  • num_class (int) - 分类类别数。

  • beta (float) - 滑动平均的系数。默认值: 0.99

  • gamma (float) - 超参数。默认值: 2.0

  • e (float) - 比例系数,focal误差占比。默认值: 0.1

  • neighbors (int) - 标签中需要mask的邻居数。默认值: 2

  • not_focal (bool) - 是否使用focal误差。默认值: False

  • reducer_flag (bool) - 是否对多卡的标签值做聚合。默认值: False

输入:
  • prediction (Tensor) - 模型预测值,shape为 \((batch\_size, ndim)\)

  • target (Tensor) - 标签值,shape为 \((batch\_size, ndim)\)

输出:

Tensor。shape为 \((batch\_size, )\)

支持平台:

Ascend GPU

样例:

>>> import numpy as np
>>> from mindspore import Tensor
>>> from mindsponge.metrics import MultiClassFocal
>>> net = MultiClassFocal(10)
>>> prediction = Tensor(np.random.randn(32, 10).astype(np.float32))
>>> target = Tensor(np.random.randn(32, 10).astype(np.float32))
>>> out = net(prediction, target)
>>> print(out.shape)
(32,)