前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Caffe Loss 层 - LossLayers

Caffe Loss 层 - LossLayers

作者头像
AIHGF
修改2020-06-12 15:43:03
1K0
修改2020-06-12 15:43:03
举报
文章被收录于专栏:AIUAIAIUAI

Caffe Loss 层

Loss 计算的是网络输出的 target 值与真实label之间的误差,最小化以优化网络.

Loss 值由 forward-pass 计算得到,并在 backward-pass 计算关于 loss 的梯度值.

Caffe 主要提供了以下 Loss 层:

1. SoftmaxWithLoss

用于一对多(one-of-many) 的分类任务,计算多项 logistic 损失值. 通过 softmax 来传递实值预测值,以得到关于各类的概率分布.

该网络层可以分解为 SoftmaxLayer + MultinomialLogisticLoss 层的组合,不过其梯度计算更加数值稳定.

测试时,该网络层可以由 SoftmaxLayer 层代替.

1.1 Forward 参数

输入参数

1.2 Backward 参数

计算关于预测值的 softmax loss 误差值梯度.

不计算关于 label 输入[bottom[1]]的 梯度.

代码语言:javascript
复制
template<typename Dtype >
void caffe::SoftmaxWithLossLayer< Dtype >::Backward_cpu (   
    const vector< Blob< Dtype > *> &    top,
    const vector< bool > &  propagate_down,
    const vector< Blob< Dtype > *> &    bottom 
)   

参数:

1.3 prototxt 定义

代码语言:javascript
复制
layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "fc8"
  bottom: "label"
  top: "loss"
  loss_param{
    ignore_label:0  # 指定 label 值,在计算 loss 时忽略该值.
    normalize: true # 如果为 true,则基于当前 labels 数量(不包含忽略的 label) 进行归一化; 否则,只是加和.
  }
}

2. EuclideanLoss

2.2 Backward 参数

计算关于输入的 Euclidean 误差梯度.

代码语言:javascript
复制
template<typename Dtype >
void caffe::EuclideanLossLayer< Dtype >::Backward_cpu   (   
    const vector< Blob< Dtype > *> &    top,
    const vector< bool > &  propagate_down,
    const vector< Blob< Dtype > *> &    bottom 
)   

参数:

  • top - 如上.

2.3 prototxt 定义

代码语言:javascript
复制
layer {
  name: "loss"
  type: "EuclideanLoss"
  bottom: "pred"
  bottom: "label"
  top: "loss"
  loss_weight: 1
}

3. MultinomialLogisticLoss

多项 logistic 损失函数层,用于一对多的分类任务,其直接采用预测的概率分布作为网络层输入.

当预测值不是概率分布时,应该采用 SoftmaxWithLossLayer,其在计算多项 logistic loss 前,采用 SoftmaxLayer 将预测值映射到概率分布.

3.1 Forward 参数

3.2 prototxt 定义

代码语言:javascript
复制
layer {
  name: "loss"
  type: "MultinomialLogisticLoss"
  bottom: "fc8"
  bottom: "label"
  top: "loss"
  loss_param{
    ignore_label:0
    normalize: true
    FULL = 0
  }
}
代码语言:javascript
复制
message LossParameter {
  optional int32 ignore_label = 1;
  enum NormalizationMode {
    FULL = 0;
    VALID = 1;
    BATCH_SIZE = 2;
    NONE = 3;
  }
  optional NormalizationMode normalization = 3 [default = VALID];
  optional bool normalize = 2;
}

4. InfogainLoss

信息增益损失函数

InfogainLossLayer 是 MultinomalLogisticLossLayer 的一种泛化形式.

其采用“信息增益”(information gain, infogain) 矩阵来指定所有的 label pairs 的“值”(value).

不仅仅接受预测的每个样本在每类上的概率信息,还接受信息增益矩阵信息.

当 infogain 矩阵是单位矩阵时,则与 MultinomalLogisticLossLayer 等价.

代码语言:javascript
复制
message InfogainLossParameter {
  // Specify the infogain matrix source.
  optional string source = 1;
  optional int32 axis = 2 [default = 1]; // axis of prob
}

4.1 Forward 参数

4.2 prototxt 定义

代码语言:javascript
复制
layer {
    bottom: "score"
    bottom: "label"
    top: "infoGainLoss"
    name: "infoGainLoss"
    type: "InfogainLoss"
    infogain_loss_param {
        source: "/.../infogainH.binaryproto"
        axis: 1  # compute loss and probability along axis
    }
}

5. HingeLoss

用于一对多 的分类任务.

其有时也被叫做 Max-Margin Loss. SVM 的目标函数也层用过.

比如,二分类情况时,

代码语言:javascript
复制
message HingeLossParameter {
  enum Norm {
    L1 = 1;
    L2 = 2;
  }
  // Specify the Norm to use L1 or L2
  optional Norm norm = 1 [default = L1];
}

5.1 Forward 参数

输入参数:

5.2 prototxt 定义

代码语言:javascript
复制
# L1 Norm
layer {
  name: "loss"
  type: "HingeLoss"
  bottom: "pred"
  bottom: "label"
}

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

6. ContrastiveLoss

Caffe Siamese Network 采用了 ContrastiveLoss 函数,能够有效的处理 paired data.

Caffe - mnist_siamese.ipynb.

ContrastiveLoss 计算公式:

代码语言:javascript
复制
message ContrastiveLossParameter {
  // margin for dissimilar pair
  optional float margin = 1 [default = 1.0];
  // The first implementation of this cost did not exactly match the cost of
  // Hadsell et al 2006 -- using (margin - d^2) instead of (margin - d)^2.
  // legacy_version = false (the default) uses (margin - d)^2 as proposed in the
  // Hadsell paper. New models should probably use this version.
  // legacy_version = true uses (margin - d^2). This is kept to support /
  // reproduce existing models and results
  optional bool legacy_version = 2 [default = false];
}

6.1 Forward 参数

输入参数:

6.2 prototxt 定义

代码语言:javascript
复制
layer {
  name: "loss"
  type: "ContrastiveLoss"
  bottom: "feat"
  bottom: "feat_p"
  bottom: "sim"
  top: "loss"
  contrastive_loss_param {
    margin: 1
  }
}

From mnist_siamese_train_test.prototxt

7. Accuracy

计算 一对多 分类任务的分类精度.

没有 backward 计算.

代码语言:javascript
复制
message AccuracyParameter {
  // top_k 精度
  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];

  // 精度计算,忽略 ignore_label 
  optional int32 ignore_label = 3;
}

7.1 参数

AccuracyLayer 提供了 AccuracyParameter accuracy_param 参数选项:

  • top_k - 可选,默认为 1. 选取最大的 kkk 个预测值为正确预测. 如,k=5k=5k=5 表示,如果 groundtruth label 在 top 5 的预测 labels 内,则认为是预测正确.

Reference

[1] - 交叉熵代价函数(损失函数)及其求导推导

[2] - caffe层解读系列——hinge_loss

[3] - 损失函数改进方法总览

[4] - 视觉分类任务中处理不平衡问题的loss比较

[5] - Caffe Loss层 - HingelossLayer

[6] - caffe Namespace Reference

[7] - 机器学习中的损失函数 (着重比较:hinge loss vs softmax loss)

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年01月24日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Caffe Loss 层
    • 1. SoftmaxWithLoss
      • 1.1 Forward 参数
      • 1.2 Backward 参数
      • 1.3 prototxt 定义
    • 2. EuclideanLoss
      • 2.2 Backward 参数
      • 2.3 prototxt 定义
    • 3. MultinomialLogisticLoss
      • 3.1 Forward 参数
      • 3.2 prototxt 定义
    • 4. InfogainLoss
      • 4.1 Forward 参数
      • 4.2 prototxt 定义
    • 5. HingeLoss
      • 5.1 Forward 参数
      • 5.2 prototxt 定义
    • 6. ContrastiveLoss
      • 6.1 Forward 参数
      • 6.2 prototxt 定义
    • 7. Accuracy
      • 7.1 参数
    • Reference
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档