[英]Trying to understand custom loss layer in caffe
我已经看到可以定义一个自定义损失层,例如在caffe中的EuclideanLoss,如下所示:
import caffe
import numpy as np
class EuclideanLossLayer(caffe.Layer):
"""
Compute the Euclidean Loss in the same manner as the C++
EuclideanLossLayer
to demonstrate the class interface for developing layers in Python.
"""
def setup(self, bottom, top):
# check input pair
if len(bottom) != 2:
raise Exception("Need two inputs to compute distance.")
def reshape(self, bottom, top):
# check input dimensions match
if bottom[0].count != bottom[1].count:
raise Exception("Inputs must have the same dimension.")
# difference is shape of inputs
self.diff = np.zeros_like(bottom[0].data, dtype=np.float32)
# loss output is scalar
top[0].reshape(1)
def forward(self, bottom, top):
self.diff[...] = bottom[0].data - bottom[1].data
top[0].data[...] = np.sum(self.diff**2) / bottom[0].num / 2.
def backward(self, top, propagate_down, bottom):
for i in range(2):
if not propagate_down[i]:
continue
if i == 0:
sign = 1
else:
sign = -1
bottom[i].diff[...] = sign * self.diff / bottom[i].num
但是,我对该代码有一些疑问:
如果要自定义此层并更改此行中的损耗计算:
top[0].data[...] = np.sum(self.diff**2) / bottom[0].num / 2.
让我们说:
channelsAxis = bottom[0].data.shape[1]
self.diff[...] = np.sum(bottom[0].data, axis=channelAxis) - np.sum(bottom[1].data, axis=channelAxis)
top[0].data[...] = np.sum(self.diff**2) / bottom[0].num / 2.
如何更改后退功能? 对于欧几里得损失是:
bottom[i].diff[...] = sign * self.diff / bottom[i].num
如何查看我描述的损失?
标志是什么?
尽管将损失作为"Python"
层实现可能是非常有教育意义的练习,但是使用现有层也可以得到相同的损失。 您需要做的是在调用常规的"EuclideanLoss"
层之前为每个斑点添加一个"Reduction"
"EuclideanLoss"
层:
layer {
type: "Reduction"
name: "rx1"
bottom: "x1"
top: "rx1"
reduction_param { axis: 1 operation: SUM }
}
layer {
type: "Reduction"
name: "rx2"
bottom: "x2"
top: "rx2"
reduction_param { axis: 1 operation: SUM }
}
layer {
type: "EuclideanLoss"
name: "loss"
bottom: "rx1"
bottom: "rx2"
top: "loss"
}
更新:
根据您的评论 ,如果您只想对通道尺寸求和,而保持所有其他尺寸不变,则可以使用固定的1x1转换(如您建议的那样):
layer {
type: "Convolution"
name: "rx1"
bottom: "x1"
top: "rx1"
param { lr_mult: 0 decay_mult: 0 } # make this layer *fixed*
convolution_param {
num_output: 1
kernel_size: 1
bias_term: 0 # no need for bias
weight_filler: { type: "constant" value: 1 } # sum
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.