- Layer type: Reduction
- 头文件位置:./include/caffe/layers/reduction_layer.hpp
- CPU 执行源文件位置: ./src/caffe/layers/reduction_layer.cpp
- CUDA GPU 执行源文件位置: ./src/caffe/layers/reduction_layer.cu
- Reduction层的功能:使用sum或mean等操作作用于输入blob按照给定参数规定的维度。(通俗的讲就是将输入的特征图按照给定的维度进行求和或求平均)。
参数解读
layer {
name: "rnn_o_concat"
type: "Concat"
bottom: "o_1"
bottom: "o_2"
bottom: "o_3"
top: "o"
concat_param {
axis: 0
}
}
layer {
name: "o_pseudoloss"
type: "Reduction"
bottom: "o"
top: "o_pseudoloss"
loss_weight: 1
reduction_param{
axis: 0//default = 0
optional ReductionOp operation = SUM //default = SUM
}
}
根据Reduction的定义,我们知道就是将输入的特征图“o”的第一个维度的特征图都加起来。concat_layer的用法见:【caffe】Layer解读之:Concat
参数定义
参数(ReductionParameter reduction_param)
定义位置 ./src/caffe/proto/caffe.proto:
// Message that stores parameters used by ReductionLayer
message ReductionParameter {
enum ReductionOp {
SUM = 1;
ASUM = 2;
SUMSQ = 3;
MEAN = 4;
}
optional ReductionOp operation = 1 [default = SUM]; // reduction operation
// The first axis to reduce to a scalar -- may be negative to index from the
// end (e.g., -1 for the last axis).
// (Currently, only reduction along ALL "tail" axes is supported; reduction
// of axis M through N, where N < num_axes - 1, is unsupported.)
// Suppose we have an n-axis bottom Blob with shape:
// (d0, d1, d2, ..., d(m-1), dm, d(m+1), ..., d(n-1)).
// If axis == m, the output Blob will have shape
// (d0, d1, d2, ..., d(m-1)),
// and the ReductionOp operation is performed (d0 * d1 * d2 * ... * d(m-1))
// times, each including (dm * d(m+1) * ... * d(n-1)) individual data.
// If axis == 0 (the default), the output Blob always has the empty shape
// (count 1), performing reduction across the entire input --
// often useful for creating new loss functions.
optional int32 axis = 2 [default = 0];
optional float coeff = 3 [default = 1.0]; // coefficient for output
}
番外篇
若想用Reduction解决如下问题:
我想把n*c*h*w的blob变成n*1*h*w(使用SUM),param该如何设定?
答:ReductionLayer 干不了这事儿。因为它只支持从你指定的axis到tail axis为止的reduction操作。它不支持针对某个坐标轴独立做reduction,而是从某个坐标轴开始做到最后一个坐标轴。即无论你的指定哪个坐标轴,它都会默认reduce做到最后一个坐标轴的。n*c*h*w, 你若设axis=1,它就变成n, 若你指定2,它就变成n*c。
正确解决方案(来自网络):
答:你可以用convolutionLayer做你想做的事儿。如下,注意,kernel_size,num_output,weight的值都必须为1才能做到这个效果。
convolution_param {
num_output: 1
bias_term: false
kernel_size: 1
weight_filler {
type: 'constant'
value: 1
}
}