CLASS torch.nn.MSELoss(size_average=None, reduce=None, reduction=mean)

torch.nn.functional.mse_loss(input, target, size_average=None, reduce=None, reduction=mean) → Tensor


参数

  • size_average: 默认为True, 计算一个batch中所有loss的均值;reduce为 False时,忽略这个参数;

  • reduce: 默认为True, 计算一个batch中所有loss的均值或者和;

    • reduce = False,size_average 参数失效,返回的 loss是向量,维度为 (batch_size, ) ;

    • reduce = True,size_average 参数失效,返回的 loss是标量;

      • size_average = True,返回 loss.mean();
      • size_average = False,返回 loss.sum();
  • reduction :‘none’ | ‘mean’ | ‘sum’,默认均值;指定size_average 和reduce参数就不使用reduction ,与之相反

输入

"mse_cpu" not implemented for 'Int'
  • Input: (N,∗) where *∗ means, any number of additional dimensions;input.float()
  • Target: (N,∗) , same shape as the input;Target.float()
loss = nn.MSELoss(reduce=False, size_average=False)
input = torch.randn(3, 5)
target = torch.randn(3, 5)
output = loss(input.float(), target.float())
print(output)#output:
#tensor([[1.2459e+01, 5.8741e-02, 1.8397e-01, 4.9688e-01, 7.3362e-02],
#[8.0921e-01, 1.8580e+00, 4.5180e+00, 7.5342e-01, 4.1929e-01],
#[2.6371e-02, 1.5204e+00, 1.5778e+00, 1.1634e+00, 9.5338e-03]])
loss = nn.MSELoss(reduce = True, size_average=True)
input = torch.randn(3, 5)
target = torch.randn(3, 5)
output = loss(input, target)
print(output)#output:
#tensor(1.2368)

这个地方有一个巨坑,就是一定要小心input和target的位置,说的更具体一些,target一定需要是一个不能被训练更新的、requires_grad=False的值,否则会报错!!!

loss=nn.MSELoss()
input=torch.randn(3,5,requires_grad=True)
target=torch.randn(3,5)
error=loss(input,target)
error.backward()

另外,关于MSELoss的设定

若设定loss=torch.nn.MSELoss(reduction=‘mean’),最终输出值是(target-input)每个元素数字平方和除以width x height,也就是在batch和特征维度上都做了平均。如果只想在batch上做平均,则可以写成这个样子:

#需要注意的是,这里的input和target是mini-batch的形式
loss=torch.nn.MSELoss(reduction='sum')
loss=loss(input,target)/target.size(0)

torch.nn.MSELoss的用法相关推荐

  1. pytorch torch.nn.MSELoss(size_average=True)(均方误差【损失函数】)Mean Squared Error(MSE)、SSE(和方差)

    class torch.nn.MSELoss(size_average=True)[source] 创建一个衡量输入x(模型预测输出)和目标y之间均方误差标准. x 和 y 可以是任意形状,每个包含n ...

  2. pytorch torch.nn.MSELoss

    应用 # 1.计算绝对差总和:|0-1|^2+|1-1|^2+|2-1|^2+|3-1|^2=6 # 2.求平均: 6/4 =1.5 import torch import torch.nn as n ...

  3. torch.nn.RNN基本用法

    #torch.nn.RNN CLASS torch.nn.RNN(*args, **kwargs) **实现的功能:**实现一个用tanh或者ReLU作为非线性成分的Elman RNN(两种RNN中的 ...

  4. torch.nn.functional.unfold 用法解读

    torch.nn.Unfold(kernel_size, dilation=1, padding=0, stride=1) 参数 kernel_size,滑动窗口的大小 dilation,和空洞卷积中 ...

  5. nn.Module、nn.Sequential和torch.nn.parameter学习笔记

    nn.Module.nn.Sequential和torch.nn.parameter是利用pytorch构建神经网络最重要的三个函数.搞清他们的具体用法是学习pytorch的必经之路. 目录 nn.M ...

  6. pytorch使用torch.nn.Sequential构建网络

    以一个线性回归的例子为例: 全部代码 import torch import numpy as npdef get_x_y():x = np.random.randint(0, 50, 300)y_v ...

  7. pytorch nn.MSELoss

    1.应用 import torch import torch.nn as nnloss = nn.MSELoss() input = torch.tensor([1.0,1.0], requires_ ...

  8. torch.nn、(二)

    参考 torch.nn.(二) - 云+社区 - 腾讯云 目录 Recurrent layers RNN LSTM GRU RNNCell LSTMCell GRUCell Transformer l ...

  9. [Pytorch系列-28]:神经网络基础 - torch.nn模块功能列表

    作者主页(文火冰糖的硅基工坊):文火冰糖(王文兵)的博客_文火冰糖的硅基工坊_CSDN博客 本文网址:https://blog.csdn.net/HiWangWenBing/article/detai ...

  10. 【pytorch】2.9 均方差损失函数 nn.MSELoss()

    均方差损失函数一般作为 回归问题的损失函数,公式如下: MSE=∑i=1n(yi−yi′)2nMSE = \frac{\sum^n_{i=1}(y_i-y'_i)^2}{n} MSE=n∑i=1n​( ...

最新文章

  1. 二值网络--Structured Binary Neural Networks for Accurate Image Classification and Semantic Segmentation
  2. BTC上轨受阻继续调整,主流币分化BCH强势上行
  3. 慎用stl中的erase的返回值
  4. Mysql优化之执行计划查看
  5. 相关函数: atof, atoi, atol, strtod, strtoul
  6. javaweb实训第一天上午——HTML和CSS
  7. 用typedef给结构体一个别名
  8. ubuntu下git使用
  9. 闭合导线平差计算(表面)
  10. Android隐藏应用桌面图标/动态修改桌面图标
  11. 关于读书学习与思考力。 文/江湖一剑客
  12. npm ------ npm 安装删除命令(rimraf)删除本地依赖包
  13. PyPI--python软件仓储库
  14. DUTOJ-1205: 对圣杯宝具的威力值
  15. 江苏在职读研计算机专业,计算机技术专业江苏科技大学在职研究生的相关信息介绍...
  16. python在线编程平台-在线python编程
  17. 关闭Windows系统中的UAC用户帐户控制
  18. trigger的用法总结
  19. windows 文件与文件夹加密
  20. MS12-020漏洞攻击复现

热门文章

  1. 板内板间通信协议及接口(六)RS232 RS485 RS422
  2. iOS 视频边下边播
  3. nuxt如何添加背景图片
  4. 分布式文件系统 FastDFS 5.0.5 Linux CentOS 7 安装配置
  5. Fsm hdlc_HDLbits
  6. 其实带宽≠速度 ADSL加速法完全功略
  7. 【软件需求工程】北理的恶龙们01——需求获取阶段准备工作
  8. 分光器(光分路器)基础知识【快速入门】01
  9. CF gym Coins(01背包计数)
  10. JS 进阶 (六) 浏览器事件模型DOM操作