损失函数与反向传播

损失函数与反向传播,第1张

损失函数

Loss Functions:来计算搭建模型预测的输出值和真实值之间的误差
1、实际计算输出和目标之间的差距
2、为我们更新输出提供一定的依据(反向传播)
3、损失函数越小越好

import torch
from torch.nn import L1Loss
from torch import nn

#[1,2,3]是实际的数据
inputs = torch.tensor([1, 2, 3], dtype=torch.float32)
targets = torch.tensor([1, 2, 5], dtype=torch.float32)

#1,1,1,3:1个样本,通道为1,高宽:1×3
inputs = torch.reshape(inputs, (1, 1, 1, 3))
targets = torch.reshape(targets, (1, 1, 1, 3))

loss = L1Loss()
result = loss(inputs, targets)

print(result)

结果:

tensor(0.6667)

如果计算方式为sum,则将各个数对应的差值相加,(1-1)+(2-2)+(5-3)=2:

loss = L1Loss(reduction='sum')
result = loss(inputs, targets)

结果:

tensor(2.)

如果计算方式为mean,则计算各个数对应的均方差,((1-1)+(2-2)+(5-3))/3=3/2:

loss_mse = nn.MSELoss()
result_mse = loss_mse(inputs, targets)

结果:

tensor(1.3333)

交叉熵损失函数:在深度学习中,可以看作通过概率分布 q ( x ) q(x) q(x)表示概率分布 p ( x ) p(x) p(x)的困难程度。交叉熵值越小(相对熵的值越小),两个概率分布越接近。

公式为:

outputtarget
[0.1, 0.2, 0.3]1
xclass

计算过程:-0.2+ln(exp(0.1)+exp(0.2)+exp(0.3))=1.10194284823

import torch
from torch import nn

x = torch.tensor([0.1, 0.2, 0.3])
y = torch.tensor([1])

x = torch.reshape(x, (1, 3))
loss_cross = nn.CrossEntropyLoss()

result_cross = loss_cross(x, y)
print(result_cross)

结果:

tensor(1.1019)

计算神经网络的输出和真实输出的误差

import torchvision
from torch import nn
from torch.nn import Sequential, Conv2d, MaxPool2d, Linear
from torch.nn.modules import Flatten
from torch.utils.data import DataLoader

dataset = torchvision.datasets.CIFAR10("../data", train=False, transform=torchvision.transforms.ToTensor(), download=True)
dataloader = DataLoader(dataset, batch_size=64)

class Test(nn.Module):
    def __init__(self):
        super(Test, self).__init__()
        self.model1 = 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

loss = nn.CrossEntropyLoss()
test1 = Test()
for data in dataloader:
    imgs, targets = data
    outputs = test1(imgs)
    result_loss = loss(outputs, targets)
    print(result_loss)

结果:

tensor(2.3283, grad_fn=<NllLossBackward>)
tensor(2.2921, grad_fn=<NllLossBackward>)
tensor(2.3282, grad_fn=<NllLossBackward>)
tensor(2.3227, grad_fn=<NllLossBackward>)
tensor(2.2987, grad_fn=<NllLossBackward>)
tensor(2.3106, grad_fn=<NllLossBackward>)
tensor(2.2942, grad_fn=<NllLossBackward>)
tensor(2.3251, grad_fn=<NllLossBackward>)
tensor(2.3139, grad_fn=<NllLossBackward>)
tensor(2.3239, grad_fn=<NllLossBackward>)
tensor(2.3042, grad_fn=<NllLossBackward>)
tensor(2.3065, grad_fn=<NllLossBackward>)
tensor(2.2911, grad_fn=<NllLossBackward>)
tensor(2.2752, grad_fn=<NllLossBackward>)
tensor(2.3072, grad_fn=<NllLossBackward>)
反向传播

对于神经网络的卷积核就是我们需要调节的,给每一个卷积核设置了一个参数为grad(梯度), 当采用反向传播的时候,每一个需要更新的参数都会求出来一个对应的梯度,然后再优化过程中就可以根据这个梯度对其中的参数进行优化,降低loss

loss = nn.CrossEntropyLoss()
test1 = Test()
for data in dataloader:
    imgs, targets = data
    outputs = tudui(imgs)
    result_loss = loss(outputs, targets)
    # 反向传播要使用loss
    result_loss.backward()

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/916261.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存