335 Star 1.5K Fork 862

MindSpore / docs

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

Function Differences with torch.nn.CrossEntropyLoss

View Source On Gitee

torch.nn.CrossEntropyLoss

class torch.nn.CrossEntropyLoss(
    weight=None,
    size_average=None,
    ignore_index=-100,
    reduce=None,
    reduction='mean'
)(input, target) -> Tensor

For more information, see torch.nn.CrossEntropyLoss.

mindspore.nn.CrossEntropyLoss

class mindspore.nn.CrossEntropyLoss(
    weight=None,
    ignore_index=-100,
    reduction='mean',
    label_smoothing=0.0
)(logits, labels) -> Tensor

For more information, see mindspore.nn.CrossEntropyLoss.

Differences

PyTorch: Calculate the cross-entropy loss between the predicted and target values.

MindSpore: MindSpore implements the same function as PyTorch, and the target value supports two different data forms: scalar and probabilistic.

Categories Subcategories PyTorch MindSpore Difference
Parameters Parameter 1 weight weight -
Parameter 2 size_average - Deprecated, function taken over by reduction
Parameter 3 ignore_index ignore_index -
Parameter 4 reduce - Deprecated, function taken over by reduction
Parameter 5 reduction reduction -
Parameter 6 - label_smoothing Label smoothing value, used as a regularization means to prevent overfitting of the model when calculating Loss. The range of values is [0.0, 1.0]. Default value: 0.0.
Input Input 1 input logits Same function, different parameter names
Input 2 target labels Same function, different parameter names

Code Example 1

Both PyTorch and MindSpore support the case where the target value is a scalar.

# PyTorch
import torch
import numpy as np

input_torch = np.array([[1.624, -0.611, -0.528, -1.072, 0.865], [-2.301, 1.744, -0.761, 0.319, -0.249], [1.462, -2.060, -0.322, -0.384, 1.133]])
target_torch = np.array([1, 0, 4])
loss = torch.nn.CrossEntropyLoss()
input_torch = torch.tensor(input_torch, requires_grad=True)
target_torch = torch.tensor(target_torch, dtype=torch.long)
output = loss(input_torch, target_torch)
print(round(float(output.detach().numpy()), 3))
# 2.764

# MindSpore
import mindspore
import numpy as np

input_ms = np.array([[1.624, -0.611, -0.528, -1.072, 0.865], [-2.301, 1.744, -0.761, 0.319, -0.249], [1.462, -2.060, -0.322, -0.384, 1.133]])
target_ms = np.array([1, 0, 4])
input_ms = mindspore.Tensor(input_ms, mindspore.float32)
target_ms = mindspore.Tensor(target_ms, mindspore.int32)
loss = mindspore.nn.CrossEntropyLoss()
output = loss(input_ms, target_ms)
print(round(float(output), 3))
# 2.764
1
https://gitee.com/mindspore/docs.git
git@gitee.com:mindspore/docs.git
mindspore
docs
docs
r2.0

搜索帮助

53164aa7 5694891 3bd8fe86 5694891