sponge.metrics.BinaryFocal

查看源文件
class sponge.metrics.BinaryFocal(alpha=0.25, gamma=2.0, feed_in=False, not_focal=False)[源代码]

计算预测值和真实值之间的二元分类焦点误差。

参考 Lin, Tsung-Yi, et al. 'Focal loss for dense object detection'

\[\mathrm{FL}\left(p_{\mathrm{t}}\right)=-\alpha_{\mathrm{t}}\left(1-p_{\mathrm{t}}\right)^{\gamma} \log \left(p_{\mathrm{t}}\right)\]
参数:
  • alpha (float,可选) - 交叉熵的权重,默认值为 0.25

  • gamma (float,可选) - 超参数,调节从难到易的损失,默认值为 2.0

  • feed_in (bool,可选) - 是否转换预测值,默认值为 False

  • not_focal (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 sponge.metrics import BinaryFocal
>>> net = BinaryFocal()
>>> 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,)