Pytorch MSELoss


CLASS torch.nn.MSELoss(size_average=Nonereduce=Nonereduction='mean')

Creates a criterion that measures the mean squared error (squared L2 norm) between each element in the input x and target yy.

創建一個准則測量輸入x和目標y每個元的平方二范數。

x和y可以是任意形狀的張量,每個張量總的元素個數為n。

Note: size_average and reduce are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction. Default: 'mean'

size_average和reduce正在被棄用,因此我們只看reduction參數

reduction='none'

The unreduced (i.e. with reduction set to 'none') loss can be described as:

返回每個元素的平方二范數,shape與輸入相同。

reduction='sum'和reduction='mean'

所有對應元素的平方二范數求和或求平均(sum / n)。

 

 1 a = torch.tensor([[[1., 2.]], [[1, 2]]])  # [2, 1, 2]
 2 b = torch.tensor([[[2., 4.]], [[2, 4.]]]) # [2, 1, 2]
 3 
 4 C =  torch.nn.MSELoss(reduction='none')
 5 
 6 loss = C(a, b)
 7 
 8 print(a)
 9 print(b)
10 print(loss)
11 
12 >>>
13 
14 tensor([[[1., 2.]],
15         [[1., 2.]]])
16 
17 tensor([[[2., 4.]],
18         [[2., 4.]]])
19 
20 tensor([[[1., 4.]],
21         [[1., 4.]]])  # 分別計算對應元素的平方二范數
22 
23 C =  torch.nn.MSELoss(reduction='sum')
24 
25 loss = C(a, b)
26 print(loss)
27 
28 >>> tensor(10.) # 所有元素平方二范數之和
29 
30 C =  torch.nn.MSELoss(reduction='mean')
31 
32 loss = C(a, b)
33 print(loss)
34 >>> tensor(2.5000) # 所有元素平方二范數的均值

 

 

 



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM