MCPcopy Create free account
hub / github.com/BVLC/caffe / Forward_cpu

Method Forward_cpu

src/caffe/layers/eltwise_layer.cpp:47–99  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

45
46template <typename Dtype>
47void EltwiseLayer<Dtype>::Forward_cpu(
48 const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
49 int* mask = NULL;
50 const Dtype* bottom_data_a = NULL;
51 const Dtype* bottom_data_b = NULL;
52 const int count = top[0]->count();
53 Dtype* top_data = top[0]->mutable_cpu_data();
54 switch (op_) {
55 case EltwiseParameter_EltwiseOp_PROD:
56 caffe_mul(count, bottom[0]->cpu_data(), bottom[1]->cpu_data(), top_data);
57 for (int i = 2; i < bottom.size(); ++i) {
58 caffe_mul(count, top_data, bottom[i]->cpu_data(), top_data);
59 }
60 break;
61 case EltwiseParameter_EltwiseOp_SUM:
62 caffe_set(count, Dtype(0), top_data);
63 // TODO(shelhamer) does BLAS optimize to sum for coeff = 1?
64 for (int i = 0; i < bottom.size(); ++i) {
65 caffe_axpy(count, coeffs_[i], bottom[i]->cpu_data(), top_data);
66 }
67 break;
68 case EltwiseParameter_EltwiseOp_MAX:
69 // Initialize
70 mask = max_idx_.mutable_cpu_data();
71 caffe_set(count, -1, mask);
72 caffe_set(count, Dtype(-FLT_MAX), top_data);
73 // bottom 0 & 1
74 bottom_data_a = bottom[0]->cpu_data();
75 bottom_data_b = bottom[1]->cpu_data();
76 for (int idx = 0; idx < count; ++idx) {
77 if (bottom_data_a[idx] > bottom_data_b[idx]) {
78 top_data[idx] = bottom_data_a[idx]; // maxval
79 mask[idx] = 0; // maxid
80 } else {
81 top_data[idx] = bottom_data_b[idx]; // maxval
82 mask[idx] = 1; // maxid
83 }
84 }
85 // bottom 2++
86 for (int blob_idx = 2; blob_idx < bottom.size(); ++blob_idx) {
87 bottom_data_b = bottom[blob_idx]->cpu_data();
88 for (int idx = 0; idx < count; ++idx) {
89 if (bottom_data_b[idx] > top_data[idx]) {
90 top_data[idx] = bottom_data_b[idx]; // maxval
91 mask[idx] = blob_idx; // maxid
92 }
93 }
94 }
95 break;
96 default:
97 LOG(FATAL) << "Unknown elementwise operation.";
98 }
99}
100
101template <typename Dtype>
102void EltwiseLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,

Callers

nothing calls this directly

Calls 5

caffe_setFunction · 0.85
countMethod · 0.80
mutable_cpu_dataMethod · 0.45
cpu_dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected