334 Star 1.5K Fork 864

MindSpore / docs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
SoftMarginLoss.md 2.39 KB
一键复制 编辑 原始数据 按行查看 历史
luojianing 提交于 2023-07-21 15:16 . replace target=blank

Function Differences with torch.nn.functional.soft_margin_loss

View Source On Gitee

torch.nn.functional.soft_margin_loss

torch.nn.functional.soft_margin_loss(input, target, size_average=None, reduce=None, reduction='mean')  -> Tensor/Scalar

For more information, see torch.nn.functional.soft_margin_loss.

mindspore.nn.SoftMarginLoss

class mindspore.nn.SoftMarginLoss(reduction='mean')(logits, labels)  -> Tensor

For more information, see mindspore.nn.SoftMarginLoss.

Differences

PyTorch: Loss function for the binary classification problem, used to calculate the binary loss value for the input Tensor x and the target value Tensor y (containing 1 or -1).

MindSpore: There are no functional differences except for two parameters that have been deprecated in PyTorch.

Categories Subcategories PyTorch MindSpore Difference
Parameters Parameter 1 input logits Same function, different parameter names
Parameter 2 target labels Same function, different parameter names
Parameter 3 size_average - Deprecated, replaced by reduction. MindSpore does not have this parameter
Parameter 4 reduce - Deprecated, replaced by reduction. MindSpore does not have this Parameter
Parameter 5 reduction reduction -

Code Example

The two APIs achieve the same function and have the same usage.

# PyTorch
import torch
from torch import tensor
import torch.nn as nn

logits = torch.FloatTensor([[0.3, 0.7], [0.5, 0.5]])
labels = torch.FloatTensor([[-1, 1], [1, -1]])
output = torch.nn.functional.soft_margin_loss(logits, labels)
print(output.numpy())
# 0.6764238

# MindSpore
import mindspore
import numpy as np
from mindspore import Tensor

loss = mindspore.nn.SoftMarginLoss()
logits = Tensor(np.array([[0.3, 0.7], [0.5, 0.5]]), mindspore.float32)
labels = Tensor(np.array([[-1, 1], [1, -1]]), mindspore.float32)
output = loss(logits, labels)
print(output)
# 0.6764238
1
https://gitee.com/mindspore/docs.git
git@gitee.com:mindspore/docs.git
mindspore
docs
docs
r2.0

搜索帮助