学习笔记Pytorch十二损失函数与反向传播
Posted Mr庞.
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学习笔记Pytorch十二损失函数与反向传播相关的知识,希望对你有一定的参考价值。
【学习笔记】【Pytorch】十二、损失函数与反向传播
一、损失函数的介绍
参考:
损失函数(loss function)
pytorch loss-functions 文档
作用:
1.计算实际输出和目标之间的差距
2.为我们更新输出提供一定的依据(反向传播)
1.L1Loss类的使用
from torch.nn import L1Loss
class torch.nn.L1Loss(size_average=None, reduce=None, reduction='mean')
参考:
torch.nn.L1Loss
作用:创建一个衡量输入x(模型预测输出)和目标y之间差的绝对值的平均值的标准。
reduction = ‘mean’:
代码实现
import torch
from torch.nn import L1Loss
input = torch.tensor([1, 2, 3], dtype=torch.float32) # 创建一个一阶张量(3个元素)
targets = torch.tensor([1, 2, 5], dtype=torch.float32) # 创建一个一阶张量(3个元素)
# 转化为四阶张量(不转化也可以)
inputs = torch.reshape(input, (1, 1, 1, 3))
targets = torch.reshape(targets, (1, 1, 1, 3))
loss1 = L1Loss() # 创建一个实例
loss2 = L1Loss(reduction="sum") # 创建一个实例
loss3 = L1Loss(reduction="none") # 创建一个实例
result1 = loss1(inputs, targets)
result2 = loss2(inputs, targets)
result3 = loss3(inputs, targets)
print(result1)
print(result2)
print(result3)
输出:
tensor(0.6667)
tensor(2.)
tensor([[[[0., 0., 2.]]]])
2.MSELoss类的使用
from torch.nn import MSELoss
class torch.nn.MSELoss(size_average=None, reduce=None, reduction='mean')
参考:
torch.nn.MSELoss
作用:创建一个衡量输入x(模型预测输出)和目标y之间均方误差标准。
reduction = ‘mean’:
# -*- coding: UTF-8 -*-
# 开发团队 :桂林电子科技大学 - 人工智能学院 - chen
# 开发人员 :Chen
# 开发时间 :2023/1/15 21:46
# 开发名称 :18.nn.loss.py
# 开发工具 :PyCharm
import torch
from torch.nn import MSELoss
input = torch.tensor([1, 2, 3], dtype=torch.float32) # 创建一个一阶张量(3个元素)
targets = torch.tensor([1, 2, 5], dtype=torch.float32) # 创建一个一阶张量(3个元素)
# 转化为四阶张量(不转化也可以)
inputs = torch.reshape(input, (1, 1, 1, 3))
targets = torch.reshape(targets, (1, 1, 1, 3))
loss1 = MSELoss() # 创建一个实例
loss2 = MSELoss(reduction="sum") # 创建一个实例
loss3 = MSELoss(reduction="none") # 创建一个实例
result1 = loss1(inputs, targets)
result2 = loss2(inputs, targets)
result3 = loss3(inputs, targets)
print(result1)
print(result2)
print(result3)
输出:
tensor(1.3333)
tensor(4.)
tensor([[[[0., 0., 4.]]]])
3.损失函数在模型中的实现
import torch
import torchvision
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
class Model(nn.Module):
def __init__(self) -> None:
super().__init__() # 初始化父类属性
self.model1 = Sequential(
Conv2d(3, 32, 5, stride=1, padding=2),
MaxPool2d(2),
Conv2d(32, 32, 5, padding=2),
MaxPool2d(2),
Conv2d(32, 64, 5, padding=2),
MaxPool2d(2),
Flatten(),
Linear(1024, 64),
Linear(64, 10)
)
def forward(self, x):
x = self.model1(x)
return x
dataset = torchvision.datasets.CIFAR10(root="./dataset", train=False,
transform=torchvision.transforms.ToTensor(),
download=True)
dataloader = DataLoader(dataset, batch_size=1)
loss = nn.CrossEntropyLoss() # 交叉熵
model = Model() # 创建一个实例
for data in dataloader:
imgs, targets = data
output = model(imgs)
result_loss = loss(output, targets)
print(result_loss) # 输出误差
输出:
Files already downloaded and verified
tensor(2.4058, grad_fn=<NllLossBackward0>)
tensor(2.2368, grad_fn=<NllLossBackward0>)
tensor(2.2519, grad_fn=<NllLossBackward0>)
...
...
二、反向传播
参考:
深度学习 | 反向传播详解
import torch
import torchvision
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
class Model(nn.Module):
def __init__(self) -> None:
super().__init__() # 初始化父类属性
self.model1 = Sequential(
Conv2d(3, 32, 5, stride=1, padding=2),
MaxPool2d(2),
Conv2d(32, 32, 5, padding=2),
MaxPool2d(2),
Conv2d(32, 64, 5, padding=2),
MaxPool2d(2),
Flatten(),
Linear(1024, 64),
Linear(64, 10)
)
def forward(self, x):
x = self.model1(x)
return x
dataset = torchvision.datasets.CIFAR10(root="./dataset", train=False,
transform=torchvision.transforms.ToTensor(),
download=True)
dataloader = DataLoader(dataset, batch_size=1)
loss = nn.CrossEntropyLoss() # 交叉熵
model = Model() # 创建一个实例
for data in dataloader:
imgs, targets = data
output = model(imgs)
result_loss = loss(output, targets)
result_loss.backward() # 反向传播
print(result_loss) # 输出误差
Bebug模型:查看卷积核的梯度
model -> Protected Attributes -> _models -> 0 -> weight -> grad
以上是关于学习笔记Pytorch十二损失函数与反向传播的主要内容,如果未能解决你的问题,请参考以下文章