参考:
损失函数(loss function)
pytorch loss-functions 文档
作用:
1.计算实际输出和目标之间的差距
2.为我们更新输出提供一定的依据(反向传播)
from torch.nn import L1Loss
class torch.nn.L1Loss(size_average=None, reduce=None, reduction='mean')
参考:
torch.nn.L1Loss
作用:创建一个衡量输入x(模型预测输出)和目标y之间差的绝对值的平均值的标准。
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.]]]])
from torch.nn import MSELoss
class torch.nn.MSELoss(size_average=None, reduce=None, reduction='mean')
作用:创建一个衡量输入x(模型预测输出)和目标y之间均方误差标准。
# -*- 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.]]]])
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=)
tensor(2.2368, grad_fn=)
tensor(2.2519, grad_fn=)
...
...
参考:
深度学习 | 反向传播详解
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