【Pytorch基础】torch.nn.MSELoss损失函数

MSE: Mean Squared Error(均方误差

含义:均方误差,是预测值与真实值之差的平方和的平均值,即:

$\mathrm{MSE}=\frac1{\mathrm{N}}\sum_{\mathrm{i}=1}^{\mathrm{n}}(\mathrm{x_i}-\mathrm{y_i})^2$

但是,在具体的应用中跟定义稍有不同。主要差别是参数的设置,在torch.nn.MSELoss中有一个reduction参数。reduction是维度要不要缩减以及如何缩减主要有三个选项:

loss_fn1 = torch.nn.MSELoss(reduction='none')

‘none’: no reduction will be applied.
‘mean’:  the sum of the output will be divided by the number of elements in the output.
‘sum’:  the output will be summed.

‘ none ’:不进行减值。

‘ mean ’:输出的总和将除以输出中的元素数量。

‘ sum ’:输出将被求和。

如果不设置reduction参数,默认是’mean’。

下面看个例子:

import torch
import torch.nn as nn

a = torch.tensor([[1, 2], 
[3, 4]], dtype=torch.float)

b = torch.tensor([[3, 5], 
[8, 6]], dtype=torch.float)

loss_fn1 = torch.nn.MSELoss(reduction='none')
loss1 = loss_fn1(a.float(), b.float())
print(loss1) # 输出结果:tensor([[ 4., 9.],
# [25., 4.]])

loss_fn2 = torch.nn.MSELoss(reduction='sum')
loss2 = loss_fn2(a.float(), b.float())
print(loss2) # 输出结果:tensor(42.)


loss_fn3 = torch.nn.MSELoss(reduction='mean')
loss3 = loss_fn3(a.float(), b.float())
print(loss3) # 输出结果:tensor(10.5000)

参考链接:【Pytorch基础】torch.nn.MSELoss损失函数-CSDN博客

感谢你的阅读


欢迎评论交流


忽如一夜春风来,千树万树梨花开。

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇