代码拉取完成,页面将自动刷新
torch.nn.MSELoss(size_average=None, reduce=None, reduction='mean')(input, target) -> Tensor
更多内容详见torch.nn.MSELoss。
class mindspore.nn.MSELoss(reduction='mean')(logits, labels) -> Tensor
更多内容详见mindspore.nn.MSELoss。
PyTorch:用于计算输入input和target每一个元素的均方误差,reduction参数指定应用于loss的规约类型。
MindSpore:实现与PyTorch一致的功能。
分类 | 子类 | PyTorch | MindSpore | 差异 |
---|---|---|---|---|
参数 | 参数1 | size_average | - | 已弃用,被reduction替代 |
参数2 | reduce | - | 已弃用,被reduction替代 | |
参数3 | reduction | reduction | - | |
输入 | 输入1 | input | logits | 功能一致,参数名不同 |
输入2 | target | labels | 功能一致,参数名不同 |
计算
input
和target
的均方误差。默认情况下,reduction='mean'
。
# PyTorch
import torch
from torch import nn
from torch import tensor
import numpy as np
loss = nn.MSELoss()
input_ = np.array([1, 1, 1, 1]).reshape((2, 2))
inputs = tensor(input_, dtype=torch.float32)
target_ = np.array([1, 2, 2, 1]).reshape((2, 2))
target = tensor(target_, dtype=torch.float32)
output = loss(inputs, target)
print(output.numpy())
# 0.5
# MindSpore
import mindspore
from mindspore import Tensor
import mindspore.nn as nn
import numpy as np
loss = nn.MSELoss()
input_ = np.array([1, 1, 1, 1]).reshape((2, 2))
inputs = Tensor(input_, dtype=mindspore.float32)
target_ = np.array([1, 2, 2, 1]).reshape((2, 2))
target = Tensor(target_, dtype=mindspore.float32)
output = loss(inputs, target)
print(output)
# 0.5
计算
input
和target
的均方误差,以求和方式规约。
# PyTorch
import torch
from torch import nn
from torch import tensor
import numpy as np
loss = nn.MSELoss(reduction='sum')
input_ = np.array([1, 1, 1, 1]).reshape((2, 2))
inputs = tensor(input_, dtype=torch.float32)
target_ = np.array([1, 2, 2, 1]).reshape((2, 2))
target = tensor(target_, dtype=torch.float32)
output = loss(inputs, target)
print(output.numpy())
# 2.0
# MindSpore
import mindspore
from mindspore import Tensor
import mindspore.nn as nn
import numpy as np
loss = nn.MSELoss(reduction='sum')
input_ = np.array([1, 1, 1, 1]).reshape((2, 2))
inputs = Tensor(input_, dtype=mindspore.float32)
target_ = np.array([1, 2, 2, 1]).reshape((2, 2))
target = Tensor(target_, dtype=mindspore.float32)
output = loss(inputs, target)
print(output)
# 2.0
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。