caffe同层输出精度和loss

Output accuracy and loss from the same layer in caffe

我写了一个自定义图层,想同时输出accuracyloss。是否可以通过以下方式使用 caffe 来完成?

类似于:

layer {
name: ""
bottom: ""
top: loss1
top: loss2
top: accuracy
}

您可以为图层设置任意数量的 "top"。

首先,您需要定义层计算的 "top" 数量。这是通过覆盖 ExactNumBottomBlobs().
来完成的 您的 LayerSetupReshape 方法还应考虑 "top" 的新数量,并设置和重塑这些 "top"。

请注意,由于您的层是损失层,因此每个 "top":

都必须具有 loss_weight
layer {
  name: "my_new_layer"
  type: "MyNewLayer"
  bottom: "x"
  top: "loss1"
  top: "loss2"
  top: "accuracy"
  loss_weight: 1
  loss_weight: 1.3 # you might want loss2 to have a bit more impact
  loss_weight: 0   # accuracy should not affect gradients...
}

而你的层 class 应该派生自 LossLayer<Dtype> class,而不是更抽象的 Layer<Dtype> class.

有关如何在 caffe 中实现新层的更多信息,请参阅 this page
另请注意 "SoftmaxWithLoss" 层有一个可选的第二个 "top",您可能想查看该层的代码以了解其实现方式。