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

来源:互联网 发布:网络文件夹中 编辑:程序博客网 时间:2024/06/04 19:23

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。


层的学习暂时到这里。。

0 0