caffe学习(7)损失层、通用层

Caffe Layers
Caffe学习系列(5):其它常用层及参数,denny402

损失层Loss Layers


损失通过将输出与目标进行比较,并不断优化减小loss。

Softmax(with loss)

  • 层类型:SoftmaxWithLoss
  • 示例:

    layer {
      name: "loss"
      type: "SoftmaxWithLoss"
      bottom: "ip1"
      bottom: "label"
      top: "loss"
    }

在概念上等同于softmax layer+多项对数损失层(multinomial logistic loss layer),但提供了更稳定的梯度。softmax只是输出每一类的概率,并没有与label做比较。

Sum-of-Squares / Euclidean

  • 层类型:EuclideanLoss
    这是比较传统的求偏差的方法, 12NNi=1x1ix2i22 ,直接计算欧氏距离。

Hinge / Margin

  • 层类型:HingeLoss
  • 参数(HingeLossParameter hinge_loss_param):

    • 可选
      • norm [default L1]:应该是正则化方法,目前只有L1、L2。
    • 输入:
      • n * c * h * w Predictions预测值
      • n * 1 * 1 * 1 Labels标签
    • 输出:1 * 1 * 1 * 1 Computed Loss
  • 示例

# L1 Norm L1正则
layer {
  name: "loss"
  type: "HingeLoss"
  bottom: "pred"
  bottom: "label"
}

# L2 Norm L2正则
layer {
  name: "loss"
  type: "HingeLoss"
  bottom: "pred"
  bottom: "label"
  top: "loss"
  hinge_loss_param {
    norm: L2
  }
}

Hinge loss主要用于SVM。

Accuracy

  • 层类型:Accuracy
  • 示例

    layer {
      name: "accuracy"
      type: "Accuracy"
      bottom: "ip2"
      bottom: "label"
      top: "accuracy"
      include {
            phase: TEST
          }
    }

    只有test阶段才有,因此需要加入include参数。它实际上不是损失并且没有后退步骤。

通用层Common Layers


Inner Product

  • 层类型:InnerProduct
  • 参数 (InnerProductParameter inner_product_param):
    • 必须参数
      • num_output (c_o):滤波器数量。
    • 推荐参数
      • weight_filler [default type: ‘constant’ value: 0]:全职初始化方式、值。还可以选择”xavier”算法来进行初始化,也可以设置为”gaussian”。
    • 可选参数
      • bias_filler [default type: ‘constant’ value: 0]:偏置初始化。
      • bias_term [default true]: 是否启用偏置项。
  • 输入:n * c_i * h_i * w_i
  • 输出:n * c_o * 1 * 1
  • 示例:

    layer {
      name: "fc8"
      type: "InnerProduct"
      # learning rate and decay multipliers for the weights
      param { lr_mult: 1 decay_mult: 1 }
    
    # learning rate and decay multipliers for the biases
    
      param { lr_mult: 2 decay_mult: 0 }
      inner_product_param {
            num_output: 1000
            weight_filler {
                  type: "gaussian"
                  std: 0.01
                }
            bias_filler {
                  type: "constant"
                  value: 0
                }
          }
      bottom: "fc7"
      top: "fc8"
    }

Reshape

  • 层类型:Reshape
  • 参数 (ReshapeParameter reshape_param):

    • 可选参数:
      • shape
  • 输入:单独的blob

  • 输出:变形后的blob
  • 示例:

    layer {
        name: "reshape"
        type: "Reshape"
        bottom: "input"
        top: "output"
        reshape_param {
              shape {
                dim: 0  # copy the dimension from below
                dim: 2
                dim: 3
                dim: -1 # infer it from the other dimensions
              }
        }
    }

这一操作不改变数据,只改变维度,也没有在过程中拷贝数据。输出的尺寸有shape参数的值规定,正数是对应的维度,除此外还有两个特殊值:

  • 0表示复制底层对应的维度。bottom第一维度值为2,top第一维度也是2。
  • -1表示从其他维度推断。为了保证数据总数不变,可以根据其他维数值计算。

特别的,当时用参数:reshape_param { shape { dim: 0 dim: -1 } }时,reshape层相当于flatten层,将n * c * h * w的数据变为n * (c*h*w)。

Concatenation

  • 层类型: Concat
  • 参数 (ConcatParameter concat_param):
    • 可选参数
      • axis [default 1]: 0表示沿着数量(n),1表示沿着通道(C)。
    • 输入:n_i * c_i * h * w 对于每个blob输入,i= 1 到 K。
    • 输出:
      • 当 axis = 0: (n_1 + n_2 + … + n_K) * c_1 * h * w, 所有的c_i应该相同。
      • 当 axis = 1: n_1 * (c_1 + c_2 + … + c_K) * h * w, 所有的n_i 应该相同。

这个层把多个blob连接为一个blob。


层的学习暂时到这里。。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值