| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
| 4 个月前 | ||
| 4 个月前 | ||
| 4 个月前 | ||
| 4 个月前 | ||
| 4 个月前 | ||
| 4 个月前 | ||
| 3 个月前 |
SoftMarginLoss
产品支持情况
| 产品 | 是否支持 |
|---|---|
| Atlas A2 训练系列产品/Atlas 800I A2 推理产品 | √ |
功能说明
-
算子功能:计算二分类场景下的soft margin loss(逻辑回归损失),对输入预测值和目标值逐元素计算损失,并支持归约输出。
-
计算公式: 给定预测张量selfself和目标张量targettarget,逐元素计算损失:
Li=max(0,−ti⋅xi)+log(1+exp(−∣ti⋅xi∣)) L_i = \max(0, -t_i \cdot x_i) + \log(1 + \exp(-|t_i \cdot x_i|))
其中xi=selfix_i = self_i,ti=targetit_i = target_i。
归约模式:
- reduction=0reduction = 0(none):output=Loutput = L,输出与输入同shape
- reduction=1reduction = 1(mean):output=1N∑iLioutput = \frac{1}{N}\sum_{i}L_i,输出标量
- reduction=2reduction = 2(sum):output=∑iLioutput = \sum_{i}L_i,输出标量
-
示例: 假设输入张量self=[1,−1,2,−2]self = [1, -1, 2, -2],目标张量target=[1,1,−1,−1]target = [1, 1, -1, -1],reduction=0reduction = 0(none),那么输出张量output=[0.3133,1.3133,2.1269,0.1269]output = [0.3133, 1.3133, 2.1269, 0.1269],具体计算过程如下: $ \begin{aligned} L_0 &= \max(0, -(1)(1)) + \log(1 + \exp(-|1|)) = 0 + \log(1 + 0.3679) = 0.3133 \ L_1 &= \max(0, -(1)(-1)) + \log(1 + \exp(-|-1|)) = 1 + \log(1 + 0.3679) = 1.3133 \ L_2 &= \max(0, -(-1)(2)) + \log(1 + \exp(-|-2|)) = 2 + \log(1 + 0.1353) = 2.1269 \ L_3 &= \max(0, -(-1)(-2)) + \log(1 + \exp(-|-2|)) = 0 + \log(1 + 0.1353) = 0.1269 \end{aligned} $
参数说明
| 参数名 | 输入/输出/属性 | 描述 | 数据类型 | 数据格式 |
|---|---|---|---|---|
| self | 输入 | 预测值张量,公式中的x。 | FLOAT、FLOAT16 | ND |
| target | 输入 | 目标值张量,shape和dtype与self一致,公式中的t。 | FLOAT、FLOAT16 | ND |
| output | 输出 | 输出张量。reduction=none时与输入同shape;reduction=mean/sum时为标量(0维张量)。 | FLOAT、FLOAT16 | ND |
| reduction | 属性 |
|
Int | - |
约束说明
- self和target的shape和dtype必须一致。
- float16输入在Kernel内部提升至float32计算,仅最终输出转回float16。
调用说明
| 调用方式 | 调用样例 | 说明 |
|---|---|---|
| aclnn调用 | test_aclnn_soft_margin_loss.cpp | 通过aclnn接口方式调用SoftMarginLoss算子。 |