Caffe Tutorial(Loss:the task to be learned is defined by the loss)

三、Loss

在Caffe中,和大多数机器学习一样,学习是由损失函数(也称为error,cost或objective function目标函数)驱动的。损失函数通过将参数设置(即,当前网络权重)映射到指定这些参数设置的“不良”的标量值来指定学习的目标。因此,学习的目标是找到一个最小化损失函数的权重设置。

Caffe的loss由网络的正向传递计算。每一层都有一组输入(底部)blobs,并产生一组输出(顶部)blobs。其中一些层的输出可以用于损失函数。一对一分类任务的损失函数的典型选择是SoftmaxWithLoss函数,用于网络定义如下,例如:

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

在SoftmaxWithLoss函数中,顶部blob是一个标量(empty shape),它在整个小批量中对损失(从预测标签和实际标签计算)进行平均。

1、loss weights

对于多层产生损失的网络(例如,使用SoftmaxWithLoss层对输入进行分类并使用欧几里德丢失层重建它的网络),可以使用损失权重来指定它们的相对重要性。

按照惯例,具有Loss的Caffe层类型有助于损失函数,但其他层被假定为纯粹用于中间计算。However, any layer can be used as a loss by adding a field loss_weight: <float> to a layer definition for each top blob produced by the layer.Layers with the suffix Loss have an implicit loss_weight: 1 for the first top blob (and loss_weight: 0 for any additional tops); other layers have an implicit loss_weight: 0 for all tops.So, the above SoftmaxWithLoss layer could be equivalently written as:

layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "pred"
  bottom: "label"
  top: "loss"
  loss_weight: 1
}

然而,任何能够反向传播的层可以被给予非零的loss_weight,允许人们例如规则化网络的一些中间层所产生的激活(如果需要的话)。 对于具有相关非零丢失的非单例输出,仅通过在BLOB的所有条目上求和来计算损失。

那么,Caffe中的最终损失是通过将网络上的总加权损失相加来计算的,如下面的伪代码所示:

loss := 0
for layer in layers:
  for top, loss_weight in layer.tops, layer.loss_weights:
    loss += loss_weight * sum(top)


猜你喜欢

转载自blog.csdn.net/littlestudent12/article/details/80843852