| 34 | } |
| 35 | |
| 36 | void TestForward(ReductionParameter_ReductionOp op, |
| 37 | float coeff = 1, int axis = 0) { |
| 38 | LayerParameter layer_param; |
| 39 | ReductionParameter* reduction_param = layer_param.mutable_reduction_param(); |
| 40 | reduction_param->set_operation(op); |
| 41 | if (coeff != 1.0) { reduction_param->set_coeff(coeff); } |
| 42 | if (axis != 0) { reduction_param->set_axis(axis); } |
| 43 | shared_ptr<ReductionLayer<Dtype> > layer( |
| 44 | new ReductionLayer<Dtype>(layer_param)); |
| 45 | layer->SetUp(this->blob_bottom_vec_, this->blob_top_vec_); |
| 46 | layer->Forward(this->blob_bottom_vec_, this->blob_top_vec_); |
| 47 | const Dtype* in_data = this->blob_bottom_->cpu_data(); |
| 48 | const int num = this->blob_bottom_->count(0, axis); |
| 49 | const int dim = this->blob_bottom_->count(axis); |
| 50 | for (int n = 0; n < num; ++n) { |
| 51 | Dtype expected_result = 0; |
| 52 | for (int d = 0; d < dim; ++d) { |
| 53 | switch (op) { |
| 54 | case ReductionParameter_ReductionOp_SUM: |
| 55 | expected_result += *in_data; |
| 56 | break; |
| 57 | case ReductionParameter_ReductionOp_MEAN: |
| 58 | expected_result += *in_data / dim; |
| 59 | break; |
| 60 | case ReductionParameter_ReductionOp_ASUM: |
| 61 | expected_result += fabs(*in_data); |
| 62 | break; |
| 63 | case ReductionParameter_ReductionOp_SUMSQ: |
| 64 | expected_result += (*in_data) * (*in_data); |
| 65 | break; |
| 66 | default: |
| 67 | LOG(FATAL) << "Unknown reduction op: " |
| 68 | << ReductionParameter_ReductionOp_Name(op); |
| 69 | } |
| 70 | ++in_data; |
| 71 | } |
| 72 | expected_result *= coeff; |
| 73 | const Dtype computed_result = this->blob_top_->cpu_data()[n]; |
| 74 | EXPECT_FLOAT_EQ(expected_result, computed_result) |
| 75 | << "Incorrect result computed with op " |
| 76 | << ReductionParameter_ReductionOp_Name(op) << ", coeff " << coeff; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void TestGradient(ReductionParameter_ReductionOp op, |
| 81 | float coeff = 1, int axis = 0) { |
no test coverage detected