日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

pytorch中的MSELoss函数

發布時間:2024/3/12 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pytorch中的MSELoss函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

基本概念

均方誤差(mean square error, MSE),是反應估計量與被估計量之間差異程度的一種度量,設ttt是根據子樣確定的總體參數θ\thetaθ的一個估計量,(θ?t)2(\theta-t)^{2}(θ?t)2的數學期望,稱為估計量ttt的均方誤差。

pytorch中MSELoss函數介紹

torch.nn.MSELoss(size_average=True,reduce=True,reduction=′mean′)torch.nn.MSELoss(size\_average=True, reduce=True, reduction='mean')torch.nn.MSELoss(size_average=True,reduce=True,reduction=mean)
創建一個度量:輸入xxx(模型預測輸出)和目標yyy之間的均方誤差標準。
loss(x,y)=1n∑(xi?yi)2loss(x,y)=\frac{1}{n}\sum(x_i-y_i)^2loss(x,y)=n1?(xi??yi?)2

其實按照我們習慣的寫法,預測值是y^\hat{y}y^?,真值是yyy,那么公式為:
loss(y^,y)=1n∑(y^i?yi)2loss(\hat{y},y)=\frac{1}{n}\sum(\hat{y}_i-y_i)^2loss(y^?,y)=n1?(y^?i??yi?)2

  • y^\hat{y}y^?yyy可以是任意形狀,每個包含nnn個元素。
  • nnn個元素對應差值的平方求和,得到的結果再除以nnn
  • 如果在創建MSELossMSELossMSELoss實例的時候,在構造函數中傳入size_average=Falsesize\_average=Falsesize_average=False,那么求出來的平方和將不會除以nnn
  • 如果reduce=Falsereduce=Falsereduce=False,那么size_averagesize\_averagesize_average參數失效,直接返回向量形式losslossloss

實驗(使用jupyter notebook進行實驗)

預測值和真值都是形狀為(2,5)(2, 5)(2,5)的張量,使用randnrandnrandn函數隨機產生。

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值,公式為loss=1n∑(yi^?yi)2loss=\frac{1}{n}\sum(\hat{y_i}-y_i)^2loss=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 * subtractharm += squaresum_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、reduce=False,size_average=Falsereduce=False, size\_average=Falsereduce=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、reduce=False,size_average=Truereduce=False, size\_average=Truereduce=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>)

總結

以上是生活随笔為你收集整理的pytorch中的MSELoss函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。