- 查看(view)
Pytorch
的官方文档(official documents)Loss_funcations
L1_loss
L1_loss
数学理论支持(mathematical theory)
L1_Loss=|x-y|.
其中x是输入,y是我们的Ground Truth,L1_Loss是我们的L1损失函数的结果。
并且,这里有两种reduction方式,一种是求平均,另一种是求和。
where, x is input, y is target(Ground Truth), and L1_Loss is the result of L1_loss.
And there are two ways of reduction that we can use:
one is mean and the other is sum.
其参数(Parameters)有:size_average,reduce,reduction
输入(Input,Target)输出(Output)的shape
上代码
(code)
:
import torch
from torch import nn
# input:(1,2,3)
# output:(1,2,5)
# L1_loss = (|1-1|+|2-2|+|3-5|)/3 (mean)
# L1_loss = (|1-1|+|2-2|+|3-5|) (sum)
input = torch.tensor([1, 2, 3], dtype=torch.float32)
target = torch.tensor([1, 2, 5], dtype=torch.float32)
L1_loss_1 = nn.L1Loss()
output_mean = L1_loss_1(input, target)
print("output_mean: ", output_mean)
L1_loss_2 = nn.L1Loss(reduction='sum')
output_sum = L1_loss_2(input, target)
print("output_sum: ", output_sum)
结果(result)
:
MSE_Loss
MSE_loss
数学理论支持(mathematical theory)
MSE_Loss=(x-y)^2.
其中x是输入,y是我们的Ground Truth,MSE_Loss是我们的MSE损失函数的结果。
并且,这里有两种reduction方式,一种是求平均,另一种是求和。
where, x is input, y is target(Ground Truth), and MSE_Loss is the result of MSE_loss.
And there are two ways of reduction that we can use:
one is mean and the other is sum.
MSE_Loss与L1_Loss有相同的参数。
MSE_Loss has the same parameters as L1_Loss.
输入(Input,Target)输出(Output)的shape
上代码(code)
:
import torch
from torch import nn
# input:[1,2,3]
# target:[1,2,5]
# MSE_Loss = ((1-1)^2+(2-2)^2+(3-5)^2)/3 (mean)
# MSE_Loss = ((1-1)^2+(2-2)^2+(3-5)^2) (sum)
input = torch.tensor([1, 2, 3], dtype=torch.float32)
target = torch.tensor([1, 2, 5], dtype=torch.float32)
MSE_Loss_1 = nn.MSELoss()
output_mean = MSE_Loss_1(input, target)
print("output_mean: ", output_mean)
MSE_Loss_2 = nn.MSELoss(reduction="sum")
output_sum = MSE_Loss_2(input, target)
print("output_sum: ", output_sum)
结果(result)
:
CrossEntropy_Loss
CrossEntropy_loss
数学理论支持(mathematical theory)
cross_entorpy_loss = -x[class]+ln(E[j]exp(x[j]))
parameters of CrossEntropy_Loss
输入(Input,Target)输出(Output)的shape
上代码
(code)
:
from torch import nn
import torch
# there are three objects. the first one is a cat(0), and the second is a dog(1). the last one is car(2).
# The results of their neural network are (0.1,0.2,0.3)
# The target (ground truth) is second(1)(dog).
# cross_entropy_loss = -x[class]+ln(E[j]exp(x[j]))
# cross_entropy_loss = -0.2*(1)+ln(exp(0.1)+exp(0.2)+exp(0.3))
input = torch.tensor([0.1, 0.2, 0.3], dtype=torch.float32)
input = torch.reshape(input, (1, 3))
target = torch.tensor([1])
cross_loss = nn.CrossEntropyLoss()
result = cross_loss(input, target)
print(result)
结果(result)
:
上一章 13.初识Pytorch 复现VGG16及卷积神经网络图的可视化(Tensorboard)
下一章 15.初识Pytorch反向传播(Backward)与优化器(optimizer)SGD