caffe详解之softmax层

 


从零开始,一步一步学习caffe的使用,期间贯穿深度学习和调参的相关知识!

 

softmax layer

softmax layer: 输出似然值

layers {
  bottom: "cls3_fc"
  top: "prob"
  name: "prob"
  type: "softmax"
}

公式如下所示:

 

softmax-loss layer:输出loss值

layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "ip1"
  bottom: "label"
  top: "loss"
  loss_param{
    ignore_label:0
    normalize: 1
    normalization: FULL
  }
}

公式如下所示:

 

loss_param 说明:

  • ignore_label
    int型变量,默认为空。
    如果指定值,则label等于ignore_label的样本将不参与Loss计算,并且反向传播时梯度直接置0.
  • normalize
    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。

其他说明

softmax的上溢与下溢

对于softmax的计算公式来说,对于比较小的输入数据来说是没有什么问题的,但是针对指数函数的特点,对于较大或者较小的数据进行softmax计算会出现数据上溢与下溢的问题。计算机中浮点数的最大表示位数为,如果超过此数会产生上溢inf,同样数据小于计算机在计算过程中会产生下溢`-inf。举个例子:

  • 对于[3,1,-3],直接计算是可行的,我们可以得到(0.88,0.12,0)。
  • 对于[1000,1000,1000],我们会得到inf(上溢);
  • 对于[-1000,-999,-1000],我们会得到-inf(下溢)。

softmax解决上溢与下溢的办法

 


对任意a都成立,这意味着我们可以自由地调节指数函数的指数部分,一个典型的做法是取输入向量中的最大值:a=max{x1,x2…..xn}
这可以保证指数最大不会超过0,于是避免了上溢。即便剩余的部分下溢出了,加了a之后,也能得到一个合理的值。
并且softmax不受输入的常数偏移影响,即softmax(x)=softmax(x+c)证明如下:

 

测试准确率

layer {
  name: "accuracy"
  type: "Accuracy"
  bottom: "ip2"
  bottom: "label"
  top: "accuracy"
  accuracy_param{
    top_k:5
  }
  include {
    phase: TEST
  }
}
message AccuracyParameter {
  // When computing accuracy, count as correct by comparing the true label to
  // the top k scoring classes.  By default, only compare to the top scoring
  // class (i.e. argmax).
  optional uint32 top_k = 1 [default = 1];

  // The "label" axis of the prediction blob, whose argmax corresponds to the
  // predicted label -- may be negative to index from the end (e.g., -1 for the
  // last axis).  For example, if axis == 1 and the predictions are
  // (N x C x H x W), the label blob is expected to contain N*H*W ground truth
  // labels with integer values in {0, 1, ..., C-1}.
  optional int32 axis = 2 [default = 1];

  // If specified, ignore instances with the given label.
  optional int32 ignore_label = 3;
}

参考

softmax函数计算时候为什么要减去一个最大值?
caffe层解读系列-softmax_loss(http://blog.csdn.net/shuzfan/article/details/51460895)

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值