Caffe4:其它常用层及参数



本文讲解一些其它的常用层,包括:softmax_loss层,Inner Product层,accuracy层,reshape层和dropout层及其它们的参数配置。

1、softmax-loss

softmax-loss层和softmax层计算大致是相同的。softmax是一个分类器,计算的是类别的概率(Likelihood),是Logistic Regression 的一种推广。Logistic Regression 只能用于二分类,而softmax可以用于多分类。

softmax与softmax-loss的区别:

softmax计算公式:

而softmax-loss计算公式:

关于两者的区别更加具体的介绍,可参考:softmax vs. softmax-loss

用户可能最终目的就是得到各个类别的概率似然值,这个时候就只需要一个 Softmax层,而不一定要进行softmax-Loss 操作;或者是用户有通过其他什么方式已经得到了某种概率似然值,然后要做最大似然估计,此时则只需要后面的 softmax-Loss 而不需要前面的 Softmax 操作。因此提供两个不同的 Layer 结构比只提供一个合在一起的 Softmax-Loss Layer 要灵活许多。

不管是softmax layer还是softmax-loss layer,都是没有参数的,只是层类型不同而也

softmax-loss layer:输出loss值


    
    
  1. layer {
  2. name: "loss"
  3. type: "SoftmaxWithLoss"
  4. bottom: "ip1"
  5. bottom: "label"
  6. top: "loss"
  7. }

softmax layer: 输出似然值


    
    
  1. layers {
  2. bottom: "cls3_fc"
  3. top: "prob"
  4. name: "prob"
  5. type: “Softmax "
  6. }

2、Inner Product

全连接层,把输入当作成一个向量,输出也是一个简单向量(把输入数据blobs的width和height全变为1)。

输入: n*c0*h*w

输出: n*c1*1*1

全连接层实际上也是一种卷积层,只是它的卷积核大小和原数据大小一致。因此它的参数基本和卷积层的参数一样。

层类型:InnerProduct

lr_mult: 学习率的系数,最终的学习率是这个数乘以solver.prototxt配置文件中的base_lr。如果有两个lr_mult, 则第一个表示权值的学习率,第二个表示偏置项的学习率。一般偏置项的学习率是权值学习率的两倍。

必须设置的参数:

    num_output: 过滤器(filfter)的个数

其它参数:

      weight_filler: 权值初始化。 默认为“constant”,值全为0,很多时候我们用”xavier”算法来进行初始化,也可以设置为”gaussian”
      bias_filler: 偏置项的初始化。一般设置为”constant”,值全为0。
      bias_term: 是否开启偏置项,默认为true, 开启

     
     
  1. layer {
  2. name: “ip1”
  3. type: “InnerProduct”
  4. bottom: “pool2”
  5. top: “ip1”
  6. param {
  7. lr_mult: 1
  8. }
  9. param {
  10. lr_mult: 2
  11. }
  12. inner_product_param {
  13. num_output: 500
  14. weight_filler {
  15. type: “xavier”
  16. }
  17. bias_filler {
  18. type: “constant”
  19. }
  20. }
  21. }

3、accuracy

输出分类(预测)精确度,只有test阶段才有,因此需要加入include参数。

层类型:Accuracy


     
     
  1. layer {
  2. name: "accuracy"
  3. type: "Accuracy"
  4. bottom: "ip2"
  5. bottom: "label"
  6. top: "accuracy"
  7. include {
  8. phase: TEST
  9. }
  10. }

4、reshape

在不改变数据的情况下,改变输入的维度。

层类型:Reshape

先来看例子


     
     
  1. layer {
  2. name: "reshape"
  3. type: "Reshape"
  4. bottom: "input"
  5. top: "output"
  6. reshape_param {
  7. shape {
  8. dim: 0 # copy the dimension from below
  9. dim: 2
  10. dim: 3
  11. dim: -1 # infer it from the other dimensions
  12. }
  13. }
  14. }

有一个可选的参数组shape, 用于指定blob数据的各维的值(blob是一个四维的数据:n*c*w*h)。

dim:0  表示维度不变,即输入和输出是相同的维度。

dim:2 或 dim:3 将原来的维度变成2或3

dim:-1 表示由系统自动计算维度。数据的总量不变,系统会根据blob数据的其它三维来自动计算当前维的维度值 。

假设原数据为:64*3*28*28, 表示64张3通道的28*28的彩色图片

经过reshape变换:


     
     
  1. reshape_param {
  2. shape {
  3. dim: 0
  4. dim: 0
  5. dim: 14
  6. dim: -1
  7. }
  8. }

输出数据为:64*3*14*56

5、Dropout

Dropout是一个防止过拟合的trick。可以随机让网络某些隐含层节点的权重不工作。

先看例子:


     
     
  1. layer {
  2. name: "drop7"
  3. type: "Dropout"
  4. bottom: "fc7-conv"
  5. top: "fc7-conv"
  6. dropout_param {
  7. dropout_ratio: 0.5
  8. }
  9. }layer {
  10. name: "drop7"
  11. type: "Dropout"
  12. bottom: "fc7-conv"
  13. top: "fc7-conv"
  14. dropout_param {
  15. dropout_ratio: 0.5
  16. }
  17. }
只需要设置一个dropout_ratio就可以了。

以上部分转自:http://www.cnblogs.com/denny402/p/5083300.html







本文讲解一些其它的常用层,包括:softmax_loss层,Inner Product层,accuracy层,reshape层和dropout层及其它们的参数配置。

1、softmax-loss

softmax-loss层和softmax层计算大致是相同的。softmax是一个分类器,计算的是类别的概率(Likelihood),是Logistic Regression 的一种推广。Logistic Regression 只能用于二分类,而softmax可以用于多分类。

softmax与softmax-loss的区别:

softmax计算公式:

而softmax-loss计算公式:

关于两者的区别更加具体的介绍,可参考:softmax vs. softmax-loss

用户可能最终目的就是得到各个类别的概率似然值,这个时候就只需要一个 Softmax层,而不一定要进行softmax-Loss 操作;或者是用户有通过其他什么方式已经得到了某种概率似然值,然后要做最大似然估计,此时则只需要后面的 softmax-Loss 而不需要前面的 Softmax 操作。因此提供两个不同的 Layer 结构比只提供一个合在一起的 Softmax-Loss Layer 要灵活许多。

不管是softmax layer还是softmax-loss layer,都是没有参数的,只是层类型不同而也

softmax-loss layer:输出loss值


  
  
  1. layer {
  2. name: "loss"
  3. type: "SoftmaxWithLoss"
  4. bottom: "ip1"
  5. bottom: "label"
  6. top: "loss"
  7. }

softmax layer: 输出似然值


  
  
  1. layers {
  2. bottom: "cls3_fc"
  3. top: "prob"
  4. name: "prob"
  5. type: “Softmax "
  6. }

2、Inner Product

全连接层,把输入当作成一个向量,输出也是一个简单向量(把输入数据blobs的width和height全变为1)。

输入: n*c0*h*w

输出: n*c1*1*1

全连接层实际上也是一种卷积层,只是它的卷积核大小和原数据大小一致。因此它的参数基本和卷积层的参数一样。

层类型:InnerProduct

lr_mult: 学习率的系数,最终的学习率是这个数乘以solver.prototxt配置文件中的base_lr。如果有两个lr_mult, 则第一个表示权值的学习率,第二个表示偏置项的学习率。一般偏置项的学习率是权值学习率的两倍。

必须设置的参数:

    num_output: 过滤器(filfter)的个数

其它参数:

      weight_filler: 权值初始化。 默认为“constant”,值全为0,很多时候我们用”xavier”算法来进行初始化,也可以设置为”gaussian”
      bias_filler: 偏置项的初始化。一般设置为”constant”,值全为0。
      bias_term: 是否开启偏置项,默认为true, 开启

   
   
  1. layer {
  2. name: “ip1”
  3. type: “InnerProduct”
  4. bottom: “pool2”
  5. top: “ip1”
  6. param {
  7. lr_mult: 1
  8. }
  9. param {
  10. lr_mult: 2
  11. }
  12. inner_product_param {
  13. num_output: 500
  14. weight_filler {
  15. type: “xavier”
  16. }
  17. bias_filler {
  18. type: “constant”
  19. }
  20. }
  21. }

3、accuracy

输出分类(预测)精确度,只有test阶段才有,因此需要加入include参数。

层类型:Accuracy


   
   
  1. layer {
  2. name: "accuracy"
  3. type: "Accuracy"
  4. bottom: "ip2"
  5. bottom: "label"
  6. top: "accuracy"
  7. include {
  8. phase: TEST
  9. }
  10. }

4、reshape

在不改变数据的情况下,改变输入的维度。

层类型:Reshape

先来看例子


   
   
  1. layer {
  2. name: "reshape"
  3. type: "Reshape"
  4. bottom: "input"
  5. top: "output"
  6. reshape_param {
  7. shape {
  8. dim: 0 # copy the dimension from below
  9. dim: 2
  10. dim: 3
  11. dim: -1 # infer it from the other dimensions
  12. }
  13. }
  14. }

有一个可选的参数组shape, 用于指定blob数据的各维的值(blob是一个四维的数据:n*c*w*h)。

dim:0  表示维度不变,即输入和输出是相同的维度。

dim:2 或 dim:3 将原来的维度变成2或3

dim:-1 表示由系统自动计算维度。数据的总量不变,系统会根据blob数据的其它三维来自动计算当前维的维度值 。

假设原数据为:64*3*28*28, 表示64张3通道的28*28的彩色图片

经过reshape变换:


   
   
  1. reshape_param {
  2. shape {
  3. dim: 0
  4. dim: 0
  5. dim: 14
  6. dim: -1
  7. }
  8. }

输出数据为:64*3*14*56

5、Dropout

Dropout是一个防止过拟合的trick。可以随机让网络某些隐含层节点的权重不工作。

先看例子:


   
   
  1. layer {
  2. name: "drop7"
  3. type: "Dropout"
  4. bottom: "fc7-conv"
  5. top: "fc7-conv"
  6. dropout_param {
  7. dropout_ratio: 0.5
  8. }
  9. }layer {
  10. name: "drop7"
  11. type: "Dropout"
  12. bottom: "fc7-conv"
  13. top: "fc7-conv"
  14. dropout_param {
  15. dropout_ratio: 0.5
  16. }
  17. }
只需要设置一个dropout_ratio就可以了。

以上部分转自:http://www.cnblogs.com/denny402/p/5083300.html





猜你喜欢

转载自blog.csdn.net/weixin_41774576/article/details/81269087
今日推荐