caffe 源码学习笔记(9) reduce layer

背景

其实没什么背景,继续啃caffe代码而已2333

reduce layer其实就是做reduce操作,把一个任意shape的blob通过某种运算变成一个scalar.

caffe目前支持求和(SUM),绝对值的和(ASUM),平方和(SUMSQ),以及对得到的scalar的总数求平均的求和(MEAN).

说句题外话,TensorRT支持的操作是求和,求积,max,min和ave. 还是有一些gap的

proto

先看proto

 1
 2message ReductionParameter {
 3  enum ReductionOp {
 4    SUM = 1;
 5    ASUM = 2;
 6    SUMSQ = 3;
 7    MEAN = 4;
 8  }
 9
10  optional ReductionOp operation = 1 [default = SUM]; // reduction operation
11
12  // The first axis to reduce to a scalar -- may be negative to index from the
13  // end (e.g., -1 for the last axis).
14  // (Currently, only reduction along ALL "tail" axes is supported; reduction
15  // of axis M through N, where N < num_axes - 1, is unsupported.)
16  // Suppose we have an n-axis bottom Blob with shape:
17  //     (d0, d1, d2, ..., d(m-1), dm, d(m+1), ..., d(n-1)).
18  // If axis == m, the output Blob will have shape
19  //     (d0, d1, d2, ..., d(m-1)),
20  // and the ReductionOp operation is performed (d0 * d1 * d2 * ... * d(m-1))
21  // times, each including (dm * d(m+1) * ... * d(n-1)) individual data.
22  // If axis == 0 (the default), the output Blob always has the empty shape
23  // (count 1), performing reduction across the entire input --
24  // often useful for creating new loss functions.
25  optional int32 axis = 2 [default = 0];
26
27  optional float coeff = 3 [default = 1.0]; // coefficient for output
28}

operation不用说,表示要进行哪种reduce操作. coeff也比较简单,就是最后结果中每个元素都可以乘一个额外的系数,默认是1.

axis的含义是,从axis这个维度开始做reduce.

比如blob的shape是(a,b,c,axis,d,e)

那么就需要对 (axis,d,e)这部分做a*b*c次reduce,得到 a*b*c个标量.

需要注意,目前只支持从某个维度一直到最后的维度都去做reduce,不支持中间的几个维度去做reduce

c++ 代码

头文件中有两个成员变量num_,dim_值得一说

 1
 2template <typename Dtype>
 3class ReductionLayer : public Layer<Dtype> {
 4 public:
 5//    ...省略无关部分
 6
 7  /// @brief the reduction operation performed by the layer
 8  ReductionParameter_ReductionOp op_;
 9  /// @brief a scalar coefficient applied to all outputs
10  Dtype coeff_;
11  /// @brief the index of the first input axis to reduce
12  int axis_;
13  /// @brief the number of reductions performed
14  int num_;
15  /// @brief the input size of each reduction
16  int dim_;
17  /// @brief a helper Blob used for summation (op_ == SUM)
18  Blob<Dtype> sum_multiplier_;
19};
20
21
22num_表示的是要做reduce操作的次数
23dim_表示的是每次reduce的维度部分的count
24
25Reshape部分,主要就是计算了几个成员变量 num_,dim_,coeff_
26```c++
27
28template <typename Dtype>
29void ReductionLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
30      const vector<Blob<Dtype>*>& top) {
31  axis_ = bottom[0]->CanonicalAxisIndex(
32      this->layer_param_.reduction_param().axis());
33  // In the output, we'll keep all axes up to the reduction axis, but
34  // throw away any after that.
35  // Note: currently reducing along non-tail axes is not supported; otherwise,
36  // we'd need to also copy any axes following an "end_axis".
37  vector<int> top_shape(bottom[0]->shape().begin(),
38                        bottom[0]->shape().begin() + axis_);
39  top[0]->Reshape(top_shape);
40  //  count都是左闭又开,一个参数就是到末尾
41  num_ = bottom[0]->count(0, axis_);
42  //  num_表示的是要做reduce操作的次数
43  //  range [0,axis_)
44  dim_ = bottom[0]->count(axis_);
45  //  dim_表示的是reduce操作部分的count
46  //  range [axis_,num_axes())
47  CHECK_EQ(num_, top[0]->count());
48  if (op_ == ReductionParameter_ReductionOp_SUM ||
49```cpp
50  op_ == ReductionParameter_ReductionOp_MEAN) {
51vector<int> sum_mult_shape(1, dim_);
52// sum_mult_shape[0] = dim_
53sum_multiplier_.Reshape(sum_mult_shape);
caffe_set(dim_, Dtype(1), sum_multiplier_.mutable_cpu_data());
// sum_multiplier_是dim_长度的vector,值都为1

} coeff_ = this->layer_param().reduction_param().coeff(); if (op_ == ReductionParameter_ReductionOp_MEAN) { coeff_ /= dim_; } }

 1注意到求和的计算是通过构造了一个dim_长度的都是为1的向量来和bottom做点积实现的.
 2
 3```c++
 4
 5template <typename Dtype>
 6void ReductionLayer<Dtype>::Forward_cpu(
 7    const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
 8  const Dtype* bottom_data = bottom[0]->cpu_data();
 9  const Dtype* mult_data = NULL;
10  if (sum_multiplier_.count() > 0) {
11    mult_data = sum_multiplier_.cpu_data();
12  }
13  Dtype* top_data = top[0]->mutable_cpu_data();
14  // 得到num_个结果
15  for (int i = 0; i < num_; ++i) {
16```python
17switch (op_) {
18case ReductionParameter_ReductionOp_SUM:
19case ReductionParameter_ReductionOp_MEAN:
20  *top_data = caffe_cpu_dot(dim_, mult_data, bottom_data);
21  break;
22case ReductionParameter_ReductionOp_ASUM:
23  *top_data = caffe_cpu_asum(dim_, bottom_data);
24  break;
25case ReductionParameter_ReductionOp_SUMSQ:
26  *top_data = caffe_cpu_dot(dim_, bottom_data, bottom_data);
27  break;
28default:
29  LOG(FATAL) << "Unknown reduction op: "
30      << ReductionParameter_ReductionOp_Name(op_);
31}
32bottom_data += dim_;
33++top_data;

} // coeff_是每一个位置的系数,默认为1 // 此时才真的把系数放在每一个元素上 if (coeff_ != Dtype(1)) {

1// Reset the top_data pointer.
2top_data = top[0]->mutable_cpu_data();
3caffe_scal(num_, coeff_, top_data);

} }

Posts in this Series