mse pytorch_pytorch scatter

mse pytorch_pytorch scatter基本概念均方误差(meansquareerror,MSE),是反应估计量与被估计量之间差异程度的一种度量,设ttt是根据子样确定的总体参数θ\thetaθ的一个估计量,(θ−t)2(\theta-t)^{2}(θ−t)2的数学期望,称为估计量ttt的均方误差。pytorch中MSELoss函数介绍torch.nn.MSELoss(size_average=True,reduce=Tru…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

基本概念

均方误差(mean square error, MSE),是反应估计量与被估计量之间差异程度的一种度量,设 t t t是根据子样确定的总体参数 θ \theta θ的一个估计量, ( θ − t ) 2 (\theta-t)^{2} (θt)2的数学期望,称为估计量 t t t的均方误差。

pytorch中MSELoss函数介绍

t o r c h . n n . M S E L o s s ( s i z e _ a v e r a g e = T r u e , r e d u c e = T r u e , r e d u c t i o n = ′ m e a n ′ ) torch.nn.MSELoss(size\_average=True, reduce=True, reduction=’mean’) torch.nn.MSELoss(size_average=True,reduce=True,reduction=mean)
创建一个度量:输入 x x x(模型预测输出)和目标 y y y之间的均方误差标准。
l o s s ( x , y ) = 1 n ∑ ( x i − y i ) 2 loss(x,y)=\frac{1}{n}\sum(x_i-y_i)^2 loss(x,y)=n1(xiyi)2

其实按照我们习惯的写法,预测值是 y ^ \hat{y} y^,真值是 y y y,那么公式为:
l o s s ( y ^ , y ) = 1 n ∑ ( y ^ i − y i ) 2 loss(\hat{y},y)=\frac{1}{n}\sum(\hat{y}_i-y_i)^2 loss(y^,y)=n1(y^iyi)2

  • y ^ \hat{y} y^ y y y可以是任意形状,每个包含 n n n个元素。
  • n n n个元素对应差值的平方求和,得到的结果再除以 n n n
  • 如果在创建 M S E L o s s MSELoss MSELoss实例的时候,在构造函数中传入 s i z e _ a v e r a g e = F a l s e size\_average=False size_average=False,那么求出来的平方和将不会除以 n n n
  • 如果 r e d u c e = F a l s e reduce=False reduce=False,那么 s i z e _ a v e r a g e size\_average size_average参数失效,直接返回向量形式 l o s s loss loss

实验(使用jupyter notebook进行实验)

预测值和真值都是形状为 ( 2 , 5 ) (2, 5) (2,5)的张量,使用 r a n d n randn randn函数随机产生。

import torch
import torch.nn as nn
import math
import numpy as np
#默认求的是平均值、

input = torch.randn(2, 5, requires_grad=True)#预测值
target = torch.randn(2, 5)#真值
print('input is', input)
print('target is', target)

输出:

input is tensor([[-0.8038, -1.0976, -0.2270, -0.6983, -0.2839],
        [-0.3291, -0.6583, -1.1446, -0.0108, -0.4827]], requires_grad=True)
target is tensor([[-1.4185,  0.5586,  0.3662,  0.9048,  1.5899],
        [ 1.5777,  0.7461,  2.4658, -0.3369,  0.7868]])

1、实例化MSELoss,使用默认设置(输出的loss为标量形式,并且是平均值):

loss = nn.MSELoss()
output = loss(input, target)
print('output is', output)

输出:

output is tensor(2.9916, grad_fn=<MseLossBackward>)

验证(自己使用公式计算loss值,公式为 l o s s = 1 n ∑ ( y i ^ − y i ) 2 loss=\frac{1}{n}\sum(\hat{y_i}-y_i)^2 loss=n1(yi^yi)2):

sum_1 = []
harm = 0
for i in range(2):
    for j in range(5):
       subtract = input[i][j] - target[i][j]
       square = subtract * subtract
       harm += square

    sum_1.append(harm)
    harm = 0
# print(sum_1)
rows ,cols = input.size()
he = 0
for i in range(2):
    he += sum_1[i]
# print('')
L = he / (rows * cols)
print('自己计算loss:', L)

输出:

自己计算loss: tensor(2.9916, grad_fn=<DivBackward0>)

2、实例化MSELoss,不使用平均值(size_average=False)

loss = nn.MSELoss(size_average = False)
output = loss(input, target)
print('output is', output)

输出:

output is tensor(29.9156, grad_fn=<MseLossBackward>)

3、实例化MSELoss, 输出为向量形式(reduce=False)

loss = nn.MSELoss(reduce = False)
output = loss(input, target)
print('output is', output)

输出:

output is tensor([[ 0.3779,  2.7431,  0.3519,  2.5697,  3.5111],
        [ 3.6359,  1.9726, 13.0353,  0.1064,  1.6118]],
       grad_fn=<MseLossBackward>)

4、验证使用reduce=False时,size_average失效。

  • 1、 r e d u c e = F a l s e , s i z e _ a v e r a g e = F a l s e reduce=False, size\_average=False reduce=False,size_average=False
loss = nn.MSELoss(reduce = False, size_average = False)
output = loss(input, target)
print('output is', output)

输出:

output is tensor([[ 0.3779,  2.7431,  0.3519,  2.5697,  3.5111],
        [ 3.6359,  1.9726, 13.0353,  0.1064,  1.6118]],
       grad_fn=<MseLossBackward>)
  • 2、 r e d u c e = F a l s e , s i z e _ a v e r a g e = T r u e reduce=False, size\_average=True reduce=False,size_average=True
loss = nn.MSELoss(reduce = False, size_average = True)
output = loss(input, target)
print('output is', output)

输出:

output is tensor([[ 0.3779,  2.7431,  0.3519,  2.5697,  3.5111],
        [ 3.6359,  1.9726, 13.0353,  0.1064,  1.6118]],
       grad_fn=<MseLossBackward>)
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/192066.html原文链接:https://javaforall.net

(0)
上一篇 2026年1月19日 下午10:01
下一篇 2026年1月19日 下午10:43


相关推荐

  • Linux那些事儿之我是Hub(26)支持计划生育–看代码的理由

    Linux那些事儿之我是Hub(26)支持计划生育–看代码的理由北大校长马寅初先生曾斩钉截铁地跟讲:”中国人口太多是因为农村晚上没有电.”因此,为了支持计划生育这项基本国策,每一个男人都有义务认真看一下电源管理的代码.另一方面,虽然现在已经不住在农村了,但我一直坚定不移的认为,这个世界,最慢的是我家的网速,最快的是我家电表的转速.所以,为了了解如何让电表转速更慢,让我们一起来看看usb子系统里是如何支持电源管理的吧.上节说了应该从usb_sus…

    2022年5月30日
    34
  • JMESPath_英语语法整理

    JMESPath_英语语法整理前言JMESPath是JSON的查询语言。您可以从JSON文档中提取和转换元素官方文档:https://jmespath.org/tutorial.html基本表达式JMESPath用的最多的

    2022年7月28日
    12
  • 分类分类分类_常见名词有哪些

    分类分类分类_常见名词有哪些[{“Name”:”北京房产”,”SubMenu”:[{“Name”:”北京房产”,”SubMenu”:null,”CategoriesMenu”:null},{“Name”:”租房”,”SubMenu”:null,”CategoriesMenu”:null},{“Name”:”整租”,”SubMenu”:null,”CategoriesMenu”:null},{“Name”:”合租”,”SubMenu”:null,”CategoriesMenu”:null},{“Name”:”民宿短租”,”SubMenu

    2022年10月4日
    4
  • 【windows屏幕扩展】把你多余屏幕利用起来,spacedesk屏幕扩展超低延迟解决方案[通俗易懂]

    【windows屏幕扩展】把你多余屏幕利用起来,spacedesk屏幕扩展超低延迟解决方案[通俗易懂]目录扫盲扫盲spacedesk是一款基于TCP/IP协议的屏幕扩展工具,通过这款工具你可以把自己身边的闲置的平板手机或者笔记本利用起来,扩展你的屏幕。只要你的两台设备处于同一个网络环境下(只要互相能够ping通),你就可以实现屏幕扩展(卡不卡我就不知道了)。用过win10中的wifi扩展屏幕的同学都知道,扩展的屏幕显示质量和网络环境成正比。而win10的屏幕扩展很玄学,…

    2022年8月13日
    10
  • 逻辑运算符Python_逻辑运算符的优先级

    逻辑运算符Python_逻辑运算符的优先级#练习1:定义一个整数变量age,编写代码判断年龄是否正确age=180#要求人的年龄在0~120之间#if0<=age<=120:#新语法ifage>=0andage<=120:print(“年龄正确”)else:print(“年龄不正确”)…

    2025年6月7日
    3
  • Nginx代理转发_nginx代理和转发的区别

    Nginx代理转发_nginx代理和转发的区别nginx之proxy_pass第一种:location/proxy/{proxy_passhttp://127.0.0.1/;}代理到URL:http://127.0.0.1/test.html第二种(相对于第一种,最后少一个/location/proxy/{proxy_passhttp://127.0.0.1;}代理到URL:http://127.0.0.1/proxy/test.html第三种location/proxy/{p

    2025年7月4日
    8

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号