| 75 | |
| 76 | template <typename Dtype> |
| 77 | void ReductionLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top, |
| 78 | const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) { |
| 79 | if (!propagate_down[0]) { return; } |
| 80 | // Get bottom_data, if needed. |
| 81 | const Dtype* bottom_data = NULL; |
| 82 | switch (op_) { |
| 83 | // Operations that don't need bottom_data |
| 84 | case ReductionParameter_ReductionOp_SUM: |
| 85 | case ReductionParameter_ReductionOp_MEAN: |
| 86 | break; |
| 87 | // Operations that need bottom_data |
| 88 | case ReductionParameter_ReductionOp_ASUM: |
| 89 | case ReductionParameter_ReductionOp_SUMSQ: |
| 90 | bottom_data = bottom[0]->cpu_data(); |
| 91 | break; |
| 92 | default: |
| 93 | LOG(FATAL) << "Unknown reduction op: " |
| 94 | << ReductionParameter_ReductionOp_Name(op_); |
| 95 | } |
| 96 | const Dtype* top_diff = top[0]->cpu_diff(); |
| 97 | Dtype* bottom_diff = bottom[0]->mutable_cpu_diff(); |
| 98 | for (int i = 0; i < num_; ++i) { |
| 99 | const Dtype bottom_coeff = (*top_diff) * coeff_; |
| 100 | switch (op_) { |
| 101 | case ReductionParameter_ReductionOp_SUM: |
| 102 | case ReductionParameter_ReductionOp_MEAN: |
| 103 | caffe_set(dim_, bottom_coeff, bottom_diff); |
| 104 | break; |
| 105 | case ReductionParameter_ReductionOp_ASUM: |
| 106 | caffe_cpu_sign(dim_, bottom_data, bottom_diff); |
| 107 | caffe_scal(dim_, bottom_coeff, bottom_diff); |
| 108 | break; |
| 109 | case ReductionParameter_ReductionOp_SUMSQ: |
| 110 | caffe_cpu_scale(dim_, 2 * bottom_coeff, bottom_data, bottom_diff); |
| 111 | break; |
| 112 | default: |
| 113 | LOG(FATAL) << "Unknown reduction op: " |
| 114 | << ReductionParameter_ReductionOp_Name(op_); |
| 115 | } |
| 116 | bottom_data += dim_; |
| 117 | bottom_diff += dim_; |
| 118 | ++top_diff; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | #ifdef CPU_ONLY |
| 123 | STUB_GPU(ReductionLayer); |
nothing calls this directly
no test coverage detected