从零开始,一步一步学习caffe的使用,期间贯穿深度学习和调参的相关知识!html
softmax layer
softmax layer: 输出似然值
layers {
bottom: "cls3_fc"
top: "prob"
name: "prob"
type: "softmax"
}
公式以下所示:c++
softmax-loss layer:输出loss值
layer {
name: "loss"
type: "SoftmaxWithLoss"
bottom: "ip1"
bottom: "label"
top: "loss"
loss_param{
ignore_label:0
normalize: 1
normalization: FULL
}
}
公式以下所示:web
loss_param 说明:微信
ignore_label
int型变量,默认为空。
若是指定值,则label等于ignore_label的样本将不参与Loss计算,而且反向传播时梯度直接置0.idenormalize
bool型变量,即Loss会除以参与计算的样本总数;不然Loss等于直接求和函数normalization
enum型变量,默认为VALID,具体表明状况以下面的代码。学习
enum NormalizationMode {
// Divide by the number of examples in the batch times spatial dimensions.
// Outputs that receive the ignore label will NOT be ignored in computing the normalization factor.
FULL = 0;
// Divide by the total number of output locations that do not take the
// ignore_label. If ignore_label is not set, this behaves like FULL.
VALID = 1;
// Divide by the batch size.
BATCH_SIZE = 2;
//
NONE = 3;
}
(1) 未设置normalization,可是设置了normalize:normalize==1
: 归一化方式为VALID
normalize==0
: 归一化方式为BATCH_SIZE
(2)一旦设置normalization
,归一化方式则由normalization
决定,再也不考虑normalize
。this
其余说明
softmax的上溢与下溢
对于softmax
的计算公式来讲,对于比较小的输入数据来讲是没有什么问题的,可是针对指数函数的特色,对于较大或者较小的数据进行softmax
计算会出现数据上溢与下溢的问题。计算机中浮点数的最大表示位数为2^64,若是超过此数会产生上溢inf
,一样数据小于2^(-64)计算机在计算过程当中会产生下溢-inf
。举个例子:spa
对于[3,1,-3],直接计算是可行的,咱们能够获得(0.88,0.12,0)。.net
对于[1000,1000,1000],咱们会获得inf(上溢);
对于[-1000,-999,-1000],咱们会获得-inf(下溢)。
softmax解决上溢与下溢的办法
对任意a都成立,这意味着咱们能够自由地调节指数函数的指数部分,一个典型的作法是取输入向量中的最大值:a=max{x1,x2…..xn}
这能够保证指数最大不会超过0,因而避免了上溢。即使剩余的部分下溢出了,加了a以后,也能获得一个合理的值。
而且softmax
不受输入的常数偏移影响,即softmax(x)=softmax(x+c)证实以下:
参考
softmax函数计算时候为何要减去一个最大值?
caffe层解读系列-softmax_loss(http://blog.csdn.net/shuzfan/article/details/51460895)
-长按关注-
本文分享自微信公众号 - AI异构(gh_ed66a0ffe20a)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。