几种常见的损失函数

【常见的损失函数总结】 损失函数的定义 损失函数,又叫目标函数,是编译一个神经网络模型必须的两个要素之一。另一个必不可少的要素是优化器。 损失函数用来评价模型的预测值和真实值不一样的程度,损失函数越好,通常模型的性能越好。不同的模型用的损失函数一般也不一样。 损失函数分为经验风险损失函数和结构风险损失函数。经验风险损失函数指预测结果和实际结果的差别,结构风险损失函数是指经验风险损失函数加上正则项。 损失Loss必须是标量,因为向量无法比较大小(向量本身需要通过范数等标量来比较)。 损失函数一般分为4种,平方损失函数,对数损失函数 阅读详情

1. 损失函数、代价函数与目标函数

  损失函数(Loss Function):是定义在单个样本上的,是指一个样本的误差。
  代价函数(Cost Function):是定义在整个训练集上的,是所有样本误差的平均,也就是所有损失函数值的平均。
  目标函数(Object Function):是指最终需要优化的函数,一般来说是经验风险+结构风险,也就是(代价函数+正则化项)。

--

2. 常用的损失函数

  这一节转载自博客

(1)0-1损失函数(0-1 loss function)

 

L(y,f(x))={1,0,y≠f(x)y=f(x)L(y,f(x))={1,y≠f(x)0,y=f(x)


  也就是说,当预测错误时,损失函数为1,当预测正确时,损失函数值为0。该损失函数不考虑预测值和真实值的误差程度。只要错误,就是1。

(2)平方损失函数(quadratic loss function)

 

L(y,f(x))=(y−f(x))2L(y,f(x))=(y−f(x))2


  是指预测值与实际值差的平方。

(3)绝对值损失函数(absolute loss function)

 

L(y,f(x))=|y−f(x)|L(y,f(x))=|y−f(x)|


  该损失函数的意义和上面差不多,只不过是取了绝对值而不是求绝对值,差距不会被平方放大。

(4)对数损失函数(logarithmic loss function)

 

L(y,p(y|x))=−logp(y|x)L(y,p(y|x))=−log⁡p(y|x)


  这个损失函数就比较难理解了。事实上,该损失函数用到了极大似然估计的思想。P(Y|X)通俗的解释就是:在当前模型的基础上,对于样本X,其预测值为Y,也就是预测正确的概率。由于概率之间的同时满足需要使用乘法,为了将其转化为加法,我们将其取对数。最后由于是损失函数,所以预测正确的概率越高,其损失值应该是越小,因此再加个负号取个反。

(5)Hinge loss

  Hinge loss一般分类算法中的损失函数,尤其是SVM,其定义为:

 

L(w,b)=max{0,1−yf(x)}L(w,b)=max{0,1−yf(x)}


  其中 y=+1或y=−1y=+1或y=−1 ,f(x)=wx+bf(x)=wx+b ,当为SVM的线性核时。


3. 常用的代价函数

(1)均方误差(Mean Squared Error)

 

MSE=1N∑i=1N(y(i)−f(x(i)))MSE=1N∑i=1N(y(i)−f(x(i)))


  均方误差是指参数估计值与参数真值之差平方的期望值; MSE可以评价数据的变化程度,MSE的值越小,说明预测模型描述实验数据具有更好的精确度。( ii 表示第 ii 个样本,NN 表示样本总数)
  通常用来做回归问题的代价函数。

(2)均方根误差

 

RMSE=1N∑i=1N(y(i)−f(x(i)))−−−−−−−−−−−−−−−−−⎷RMSE=1N∑i=1N(y(i)−f(x(i)))


  均方根误差是均方误差的算术平方根,能够直观观测预测值与实际值的离散程度。
  通常用来作为回归算法的性能指标。

(3)平均绝对误差(Mean Absolute Error)

 

MAE=1N∑i=1N|y(i)−f(x(i))|MAE=1N∑i=1N|y(i)−f(x(i))|


  平均绝对误差是绝对误差的平均值 ,平均绝对误差能更好地反映预测值误差的实际情况。
  通常用来作为回归算法的性能指标。

(4)交叉熵代价函数(Cross Entry)

 

H(p,q)=−∑i=1Np(x(i))logq(x(−i))H(p,q)=−∑i=1Np(x(i))log⁡q(x(−i))


  交叉熵是用来评估当前训练得到的概率分布与真实分布的差异情况,减少交叉熵损失就是在提高模型的预测准确率。其中 p(x)p(x) 是指真实分布的概率, q(x) 是模型通过数据计算出来的概率估计。
  比如对于二分类模型的交叉熵代价函数(可参考逻辑回归一节):

L(w,b)=−1N∑i=1N(y(i)logf(x(i))+(1−y(i))log(1−f(x(i))))L(w,b)=−1N∑i=1N(y(i)log⁡f(x(i))+(1−y(i))log⁡(1−f(x(i))))


  其中 f(x)f(x) 可以是sigmoid函数。或深度学习中的其它激活函数。而 y(i)∈0,1y(i)∈0,1 。
  通常用做分类问题的代价函数。


引用及参考:
[1] https://blog.youkuaiyun.com/reallocing1/article/details/56292877
[2] https://blog.youkuaiyun.com/m_buddy/article/details/80224409
[3] https://blog.youkuaiyun.com/chaipp0607/article/details/76037351
[4] https://blog.youkuaiyun.com/shenxiaoming77/article/details/51614601

写在最后:本文参考以上资料进行整合与总结,文章中可能出现理解不当的地方,若有所见解或异议可在下方评论,谢谢!
若需转载请注明:https://www.cnblogs.com/lliuye/p/9549881.html

机器学习中10种损失函数大梳理!建议收藏,你一定用得到 今儿想和大家聊聊关于损失函数方面的问题。是在机器学习和深度学习中用来衡量模型预测值与真实标签之间差异的函数。不同的任务和模型可能需要不同的损失函数。今天就聊聊下面常见的损失函数,关于原理、使用场景,并且给出完整的代码:均方误差平均绝对误差交叉熵损失对数损失多类别交叉熵损失二分类交叉熵损失余弦相似度损失希尔伯特-施密特口袋Huber损失感知器损失ok,咱们一起来学习一下~ 阅读详情

相关推荐

【深度学习】损失函数详解

损失函数

LogosTR_的博客 1万+

损失函数(lossfunction)的全面介绍(简单易懂版)

此篇文章会带你快速了解lossfunction(损失函数),用简单的语言,方便理解!!!!!

小林学编程的博客 17万+

【Flow3D实战】从零构建水利冲淤模型:关键步骤与参数详解

本文详细介绍了使用Flow3D构建水利冲淤模型的关键步骤与参数设置技巧。从基础认知到前处理、计算设置和后处理,全面解析如何高效模拟水流与泥沙的相互作用,特别适用于桥墩冲刷、水库泄洪等工程场景。通过实战案例分享,帮助读者掌握Flow3D在水利工程中的精准应用,提升模型精度与计算效率。

weixin_42549723的博客 507

神经网络中的损失函数(Loss Function)

损失函数通常是一个非负实数函数,表示为L(Y, f(X)),其中Y是实际值(也称为标签或真实值),f(X)是模型的预测值(也称为输出值或估计值),X是输入数据。损失函数的值越小,表示模型的预测结果与实际值越接近,模型的性能也就越好。在这些任务中,模型的目标是尽可能准确地预测连续变量的值,而MAE则提供了一个衡量模型预测精度的有效指标。在这些任务中,模型的目标是准确预测样本所属的类别,而交叉熵损失则提供了一个衡量模型预测精度的有效指标。RMSE是通过计算预测值与实际值之间差的平方的平均值的平方根来衡量的。

搏博的专栏 1万+

数字IC后端设计实现 | PR工具中到底应该如何控制density和congestion?(ICC2&Innovus)

数字IC后端设计实现中到底应该如何控制cell density和congestion?(ICC2和Innovus两种解决方案)

weixin_37584728的博客 7113

DeepSeek-R1模型+Dify实战:5步打造能读PDF的智能客服(附BGE-M3嵌入模型配置)

本文详细介绍了如何利用DeepSeek-R1大模型与Dify平台,结合BGE-M3嵌入模型,构建企业级智能文档问答系统。通过5个步骤,从环境准备到知识库构建,再到智能问答应用开发,帮助企业快速从PDF等文档中提取关键信息,提升工作效率。特别适合处理产品手册、内部规程等专业内容。

convnet3designer的博客 720

几种常见的损失函数解析以及适用条件

在机器学习和深度学习领域,损失函数(Loss Function)是评估模型预测结果与真实值差异的重要工具。合理选择损失函数对于模型的训练和性能优化至关重要。本文将介绍几种常见的损失函数:L1Loss、NLLLoss、MSELoss、BCELoss 和 CrossEntropyLoss,以及它们的适用条件。选择合适的损失函数对于模型的训练和性能优化至关重要。本文介绍了五种常见的损失函数及其适用条件,希望对您在实际应用中有所帮助。在实际应用中,还需根据具体问题、数据分布和模型结构来合理选择损失函数。

qq_61600833的博客 947

给STM32 LWIP做一次‘性能体检’:手把手教你用Wireshark和iperf诊断网络瓶颈

本文详细介绍了如何使用Wireshark和iperf工具对STM32 LWIP网络性能进行全面诊断,帮助开发者精准定位网络瓶颈问题。通过搭建测试环境、集成iperf测试框架、深度报文分析和LWIP配置优化,实现从9KB/s到80MB/s的性能飞跃,特别适用于解决TCP发送速率和卡死问题。

weixin_29631169的博客 289

详细整理几种常见的损失函数|机器学习

机器学习|常见的损失函数 公众号:数据山谷 在学习过程中我们经常会接触到损失函数、代价函数、目标函数三个词语,本文让我们来总结一下机器学习中常见的损失函数和代价函数。 概念 首先让我们来了解一下三种损失函数的概念。 损失函数(Loss Function )是定义在单个样本上的,算的是一个样本的误差。 代价函数(Cost Function )是定义在整个训练集上的,是所有样本误差的平均,也就是损失函数的平均。 目标函数(Object Function)定义为:最终需要优化的函数。等于经验风险+结构风险(也就是

二哥为啥不像程序员? 1万+

双硬盘下Ubuntu 16.04与Win10双系统的安装

之前写过一个双系统安装过程,今天再次整理的原因是想重点强调双硬盘的区别。标红的一定要注意看,那就是我自己一步一步踏出来的坑啊...   基础步骤就不再重复,重点讲区别,需要看详细步骤的童鞋(或者你只有一个硬盘)请看我上一篇博客https://blog.youkuaiyun.com/weixin_40308540/article/details/80157444   区别:双硬盘和单硬盘的区别是什么呢?...

不吃包子 1125

安装glibc出现../sysdeps/nptl/pthread.h:744:47:error:argument 1 of type ‘struct __jmp_buf_tag *‘ 解决方案

本文主要介绍了安装glibc出现…/sysdeps/nptl/pthread.h:744:47:error:argument 1 of type ‘struct __jmp_buf_tag *’ 解决方案,希望能对使用Linux的同学们有所帮助。 文章目录 1. 问题描述 2. 解决方案

weixin_43178406的博客 12万+

机器学习中常见的几种损失函数

通常机器学习每一个算法中都会有一个目标函数,算法的求解过程是通过对这个目标函数优化的过程。在分类或者回归问题中,通常使用损失函数(代价函数)作为其目标函数。损失函数用来评价模型的预测值和真实值不一样的程度,损失函数越好,通常模型的性能越好。不同的算法使用的损失函数不一样。   损失函数分为经验风险损失函数和结构风险损失函数。经验风险损失函数指预测结果和实际结果的差别,结构风险损失函数是指经验风险...

chduan_10的博客 2559

【RK3399 Ubuntu】基于rk3399的lxde的lubuntu桌面移植适配记录之第二步:最基本Ubuntu根文件系统的相关常用工具完善

文章目录1.使能系统网络管理服务1.1 安装驱动modules管理工具1.2 使能网络服务2.终端自动补全命令功能3.安装ssh服务4.安装系统信息查看工具5.安装resize工具6.安装lrzsz工具7.使能rc.local服务7.1 修改服务Install块7.2 使能rc.local.service服务7.3 添加执行脚本且赋予执行权限 续接上文,本文主要完善前面已经制作好的rootfs 1.使能系统网络管理服务 1.1 安装驱动modules管理工具 apt-get install kmod 1.

wang 恒 的博客 1387

数据挖掘几种常见损失函数

对数损失函数

Bill_zhang5的博客 1315

学Simulink——基于Simulink的超前-滞后校正控制器设计与仿真建模示例

本文完成了基于 Simulink 的超前-滞后校正控制器设计与仿真,实现了:✅掌握超前与滞后校正的物理意义与设计流程✅分步设计复合控制器,兼顾动态与稳态✅在 Simulink 中搭建闭环系统并验证卓越性能✅达成高稳定裕度、高精度、快速响应三大目标。

xiaoheshang_123的博客 80

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

python训练模型损失值6000多_几种常见的损失函数

python金融风控评分卡模型和数据分析微专业课(博主亲自录制视频):http://dwz.date/b9vv1. 损失函数、代价函数与目标函数损失函数(Loss Function):是定义在单个样本上的,是指一个样本的误差。代价函数(Cost Function):是定义在整个训练集上的,是所有样本误差的平均,也就是所有损失函数值的平均。目标函数(Object Function):是指最终需要优化...

weixin_39868663的博客 1313

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

几种常见的计算机视觉中的损失函数原理理解&分析

机器学习任务中适用损失函数来计算描述预测结果和实际事实之间的差距,通过损失函数来量化这个差距从而判断预测的错误程度。因此选择适当的损失函数将有助于获得较好的结果。这里将尝试分析理解计算机视觉中一些基本的损失函数和图像分类、图像检测以及图像分割任务中常用的主流损失函数。

l13022736018的博客 6140

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

深度学习——(2)几种常见的损失函数

损失函数 L1-loss MSE-loss smoothL1 loss 交叉熵 KL散度

羊羊的博客 1527

如何画出几种常见二分类损失函数(附代码)

在二分类的监督学习中,支持向量机、逻辑斯谛回归与最大熵模型、提升方法各自使用合页损失函数、逻辑斯谛损失函数、指数损失函数,分别写为:这 3 种损失函数都是 0-1 损失函数的上界,具有相...

湾区人工智能 1566
上一篇: C++ 11会带来什么影响?
下一篇: 深入浅出--梯度下降法及其实现
Poo_Chai
博客等级 码龄9年 178粉丝 117原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值