此页面由 AI 自动翻译。查看英文原版
最后更新

本页目录

  • 类
  • SmoothBCEWithLogitsLoss
Claude CodeCodexCursor
Luxonis 智能体工具包为你常用的编程智能体加载 Luxonis 上下文与技能,更快地基于 OAK 开发应用。
安装工具包

smooth_bce_with_logits

最后更新
Python API: luxonis_train.attached_modules.losses.smooth_bce_with_logits带标签平滑的二元交叉熵。

类

SmoothBCEWithLogitsLoss

带标签平滑的 logits 上的二元交叉熵。损失将每个目标推向相反的类别。然后,它计算 BCEWithLogitsLoss 在新目标上。
  • 输入::
    • predictions (Tensor): [B, C, ...] logits
    • target (Tensor): 相同形状,浮点值在 [0, 1] 之间
  • 输出::
    • Tensor: 标量,或当 reduction 为 "none" 时为 [B, C, ...]
  • 公式:: 对于平滑因子 s,目标 y 变为 y’ = (1 − s) y + s (1 − y) 因此,目标 1 变为 1 − s,目标 0 变为 s。损失是 logits 和 y’ 的 BCEWithLogitsLoss,其中 bce_pow 作为其 pos_weight。

方法

init
Python
1def __init__(label_smoothing: float = 0.0, bce_pow: float = 1.0, weight: list[float] | None = None, reduction: Literal['mean', 'sum', 'none'] = 'mean', **kwargs):
初始化损失和包装的 BCEWithLogitsLoss。参数
  • label_smoothing (float): 平滑因子 s。目标 1 变为 1 - label_smoothing,目标 0 变为 label_smoothing。0.0 保持目标不变。
  • bce_pow (float): 损失正项的因子,所有类别相同。它成为包装损失的 pos_weight。
  • weight (list[float] | None): 元素损失的因子。包装损失将列表转换为张量,该张量与损失广播,并在最后一个维度对齐。None 使每个元素的因子为 1。
  • reduction (Literal['mean', 'sum', 'none']): 如何减少元素的损失:
    • "none": 返回每个元素的损失。
    • "mean": 返回所有元素的平均值。
    • "sum": 返回所有元素的总和。
  • **kwargs: 转发给 BaseLoss 的关键字参数,例如 final_loss_weight 和 node。
forward
Python
1def forward(predictions: Tensor, target: Tensor) -> Tensor:
平滑目标并计算二元交叉熵。参数
  • predictions (Tensor): 形状为 [B, C, ...] 的 logits,是节点的主要输出。
  • target (Tensor): 形状与 predictions 相同的 [0, 1] 范围内的浮点目标。
返回
  • Tensor: 对于 "mean" 和 "sum" 归约,返回一个标量。对于 "none",返回每个元素的损失,形状为 [B, C, ...]。
引发
  • RuntimeError: 当 predictions 和 target 形状不同时。

属性

criterion
supported_tasks