一、对于损失函数的理解
- 计算实际输出和目标之间的差距
- 为我们更新输出提供一定的依据
二、头文件
nn.L1Loss
大概含义:
代码:
import torch
from torch.nn import L1Loss
output = torch.tensor([1,2,3],dtype=float)
target = torch.tensor([1,2,5],dtype=float)
output = torch.reshape(output,(1,1,1,3))
target = torch.reshape(target,(1,1,1,3))
loss = L1Loss()
result = loss(output,target)
print(result)
输出:
tensor(0.6667, dtype=torch.float64)
nn.MSELoss
大概含义:
代码:
import torch
from torch.nn import L1Loss, MSELoss
output = torch.tensor([1,2,3],dtype=float)
target = torch.tensor([1,2,5],dtype=float)
output = torch.reshape(output,(1,1,1,3))
target = torch.reshape(target,(1,1,1,3))
loss = L1Loss()
result = loss(output,target)
loss2 = MSELoss()
result2 = loss2(output,target)
print(result2)
输出:
tensor(1.3333, dtype=torch.float64)
nn.CrossEntropyLoss
import torch
from torch.nn import L1Loss, MSELoss, CrossEntropyLoss
output = torch.tensor([1,2,3],dtype=float)
target = torch.tensor([1,2,5],dtype=float)
output = torch.reshape(output,(1,1,1,3))
target = torch.reshape(target,(1,1,1,3))
loss = L1Loss()
result = loss(output,target)
loss2 = MSELoss()
result2 = loss2(output,target)
x = torch.tensor([0.1,0.2,0.3])
y = torch.tensor(1) #正确的类别
loss3 = CrossEntropyLoss()
result3 = loss3(x,y)
print(result3)
输出:
tensor(1.1019)
包含 batch_size 的代码:
import torch
import torch.nn as nn
# 假设有 3 个样本,每个样本有 5 个类别
x = torch.tensor([[0.1, 0.2, 0.3, 0.4, 0.5], # 第一个样本的 logits
[0.3, 0.4, 0.2, 0.5, 0.1], # 第二个样本的 logits
[0.5, 0.1, 0.2, 0.4, 0.3]]) # 第三个样本的 logits
# 每个样本的真实类别标签
y = torch.tensor([1, 2, 0]) # 第一个样本属于类别 1,第二个样本属于类别 2,第三个样本属于类别 0
# 定义损失函数
loss_fn = nn.CrossEntropyLoss()
# 计算损失
loss = loss_fn(x, y)
print(f"Loss: {loss.item()}")
三、搭建模型与损失函数的结合
搭建模型
代码:
import torchvision
from torch import nn
from torch.nn import MaxPool2d, Conv2d, Flatten, Linear
from torch.utils.data import DataLoader
dataset = torchvision.datasets.CIFAR10("datasets",train=False,transform=torchvision.transforms.ToTensor(),download=True)
dataloader = DataLoader(dataset,batch_size=1)
class Mary(nn.Module):
def __init__(self):
super(Mary,self).__init__()
self.model1 = nn.Sequential(
Conv2d(3, 32, 5, 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
Yorelee = Mary()
for data in dataloader:
img,target = data
output = Yorelee(img)
print(output)
print(target)
loss = nn.CrossEntropyLoss()
result_loss = loss(output,target)
print(result_loss)
print("***********************")
result_loss.backward()
输出:
tensor([[ 0.0956, -0.0318, -0.0674, -0.0565, 0.1168, 0.0389, 0.0496, -0.0039,
-0.0221, 0.1028]], grad_fn=<AddmmBackward0>)
tensor([3])
tensor(2.3833, grad_fn=<NllLossBackward0>)
打个断点,单步执行前,注意此时还没有 grad:
单步执行后,此时就有了 grad,为后续优化器的选择做了铺垫: