14.初识Pytorch损失函数(Loss_funcations)

  • 查看(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

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值