损失函数和反向传播

损失函数

(联邦学习笔记,资料来源于b站小土堆)

损失函数(loss function)或代价函数(cost function)是将随机事件或其有关随机变量的取值映射为非负实数以表示该随机事件的“风险”或“损失”的函数。在应用中,损失函数通常作为学习准则与优化问题相联系,即通过最小化损失函数求解和评估模型。例如在统计学和机器学习中被用于模型的参数估计(parametric estimation)  ,在宏观经济学中被用于风险管理(risk mangement)和决策   ,在控制理论中被应用于最优控制理论(optimal control theory) 。

相关介绍可参见:https://blog.csdn.net/perfect1t/article/details/88199179

关于nn模型中有关 L1Loss() 损失函数,均方差 MSELoss() 损失函数和交叉熵损失函数CrossEntropyLoss() 的计算和相关定义及使用代码如下:

定义:相关链接https://pytorch.org/docs/stable/nn.html#loss-functions

损失函数和反向传播_第1张图片

 损失函数和反向传播_第2张图片

 损失函数和反向传播_第3张图片

 代码:

import torch


#loss的使用
from torch import nn
from torch.nn import L1Loss, MSELoss

input = torch.tensor([1,2,3],dtype=torch.float32)
targets = torch.tensor([1,2,5],dtype=torch.float32)

input = torch.reshape(input,(1,1,1,3))
targets = torch.reshape(targets,(1,1,1,3))


#L1Loss() 直接(0+0+2)/3 = 0.6667
#L1Loss(reduction='sum') 是 0+0+2 = 2
loss = L1Loss(reduction='sum')
result = loss(input,targets)
print(result)

#均方差, mse = (0+0+2^2)/3 = 1.3333
loss_mse = MSELoss()
result_mse = loss_mse(input,targets)
print(result_mse)

#交叉熵  loss_cross = -x[class] + log(exp(x1)+exp(x2)+...+exp(xn))   , loss_cross = -0.2 + log(exp(0.1)+exp(0.2)+exp(0.3))
#x是输出,y是target
x = torch.tensor([0.1,0.2,0.3])
y = torch.tensor([1])
x = torch.reshape(x,(1,3))
loss_corss = nn.CrossEntropyLoss()
result_corss = loss_corss(x,y)
print(result_corss)

结果:

损失函数和反向传播_第4张图片

在模型中使用损失函数:

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


class Mymodule(nn.Module):
    def __init__(self):
        super(Mymodule, self).__init__()
        self.model = 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,input):
        output = self.model(input)
        return output


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

mymodule = Mymodule()

#交叉熵
loss = nn.CrossEntropyLoss()

for data in dataloader:
    imgs,targets = data
    output = mymodule(imgs)
    print(output)
    print(targets)
    result_loss = loss(output,targets)
    #反向传播,会得到一个梯度grad,可以利用它进行优化
    result_loss.backward()
    print(result_loss)

结果:

损失函数和反向传播_第5张图片

你可能感兴趣的:(pytorch,神经网络,联邦学习,神经网络,pytorch,损失函数)