-
Notifications
You must be signed in to change notification settings - Fork 395
/
power_layer.cpp
104 lines (96 loc) · 3.55 KB
/
power_layer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <algorithm>
#include <vector>
#include "caffe/layer.hpp"
#include "caffe/util/math_functions.hpp"
#include "caffe/vision_layers.hpp"
namespace caffe {
template <typename Dtype>
void PowerLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
NeuronLayer<Dtype>::LayerSetUp(bottom, top);
power_ = this->layer_param_.power_param().power();
scale_ = this->layer_param_.power_param().scale();
shift_ = this->layer_param_.power_param().shift();
diff_scale_ = power_ * scale_;
}
// Compute y = (shift + scale * x)^power
template <typename Dtype>
void PowerLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
Dtype* top_data = top[0]->mutable_cpu_data();
const int count = bottom[0]->count();
// Special case where we can ignore the input: scale or power is 0.
if (diff_scale_ == Dtype(0)) {
Dtype value = (power_ == 0) ? Dtype(1) : pow(shift_, power_);
caffe_set(count, value, top_data);
return;
}
const Dtype* bottom_data = bottom[0]->cpu_data();
caffe_copy(count, bottom_data, top_data);
if (scale_ != Dtype(1)) {
caffe_scal(count, scale_, top_data);
}
if (shift_ != Dtype(0)) {
caffe_add_scalar(count, shift_, top_data);
}
if (power_ != Dtype(1)) {
caffe_powx(count, top_data, power_, top_data);
}
}
template <typename Dtype>
void PowerLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down,
const vector<Blob<Dtype>*>& bottom) {
if (propagate_down[0]) {
Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
const int count = bottom[0]->count();
const Dtype* top_diff = top[0]->cpu_diff();
if (diff_scale_ == Dtype(0) || power_ == Dtype(1)) {
caffe_set(count, diff_scale_, bottom_diff);
} else {
const Dtype* bottom_data = bottom[0]->cpu_data();
// Compute dy/dx = scale * power * (shift + scale * x)^(power - 1)
// = diff_scale * y / (shift + scale * x)
if (power_ == Dtype(2)) {
// Special case for y = (shift + scale * x)^2
// -> dy/dx = 2 * scale * (shift + scale * x)
// = diff_scale * shift + diff_scale * scale * x
caffe_cpu_axpby(count, diff_scale_ * scale_, bottom_data,
Dtype(0), bottom_diff);
if (shift_ != Dtype(0)) {
caffe_add_scalar(count, diff_scale_ * shift_, bottom_diff);
}
} else if (shift_ == Dtype(0)) {
// Special case for y = (scale * x)^power
// -> dy/dx = scale * power * (scale * x)^(power - 1)
// = scale * power * (scale * x)^power * (scale * x)^(-1)
// = power * y / x
const Dtype* top_data = top[0]->cpu_data();
caffe_div(count, top_data, bottom_data, bottom_diff);
caffe_scal(count, power_, bottom_diff);
} else {
caffe_copy(count, bottom_data, bottom_diff);
if (scale_ != Dtype(1)) {
caffe_scal(count, scale_, bottom_diff);
}
if (shift_ != Dtype(0)) {
caffe_add_scalar(count, shift_, bottom_diff);
}
const Dtype* top_data = top[0]->cpu_data();
caffe_div<Dtype>(count, top_data, bottom_diff, bottom_diff);
if (diff_scale_ != Dtype(1)) {
caffe_scal(count, diff_scale_, bottom_diff);
}
}
}
if (diff_scale_ != Dtype(0)) {
caffe_mul(count, top_diff, bottom_diff, bottom_diff);
}
}
}
#ifdef CPU_ONLY
STUB_GPU(PowerLayer);
#endif
INSTANTIATE_CLASS(PowerLayer);
REGISTER_LAYER_CLASS(Power);
} // namespace caffe