我见过可以像这样在 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.
我该如何更改向后功能?对于 EuclideanLoss,它是:
bottom[i].diff[...] = sign * self.diff / bottom[i].num
它必须如何查找我描述的损失?
标志有什么用?
尽管将您所追求的损失作为一个"Python"
层来实现可能是一项非常有教育意义的练习,但您可以使用现有层获得相同的损失。您只需要"Reduction"
在调用常规"EuclideanLoss"
层之前为每个 blob添加一个层:
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
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句