forked from kevinlin311tw/Caffe-DeepBinaryCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_layer.cpp
More file actions
87 lines (79 loc) · 2.96 KB
/
log_layer.cpp
File metadata and controls
87 lines (79 loc) · 2.96 KB
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
#include <algorithm>
#include <vector>
#include "caffe/layer.hpp"
#include "caffe/neuron_layers.hpp"
#include "caffe/util/math_functions.hpp"
namespace caffe {
template <typename Dtype>
void LogLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
NeuronLayer<Dtype>::LayerSetUp(bottom, top);
const Dtype base = this->layer_param_.log_param().base();
if (base != Dtype(-1)) {
CHECK_GT(base, 0) << "base must be strictly positive.";
}
// If base == -1, interpret the base as e and set log_base = 1 exactly.
// Otherwise, calculate its log explicitly.
const Dtype log_base = (base == Dtype(-1)) ? Dtype(1) : log(base);
CHECK(!isnan(log_base))
<< "NaN result: log(base) = log(" << base << ") = " << log_base;
CHECK(!isinf(log_base))
<< "Inf result: log(base) = log(" << base << ") = " << log_base;
base_scale_ = Dtype(1) / log_base;
CHECK(!isnan(base_scale_))
<< "NaN result: 1/log(base) = 1/log(" << base << ") = " << base_scale_;
CHECK(!isinf(base_scale_))
<< "Inf result: 1/log(base) = 1/log(" << base << ") = " << base_scale_;
input_scale_ = this->layer_param_.log_param().scale();
input_shift_ = this->layer_param_.log_param().shift();
backward_num_scale_ = input_scale_ / log_base;
}
template <typename Dtype>
void LogLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
const int count = bottom[0]->count();
const Dtype* bottom_data = bottom[0]->cpu_data();
Dtype* top_data = top[0]->mutable_cpu_data();
if (input_scale_ == Dtype(1) && input_shift_ == Dtype(0)) {
caffe_log(count, bottom_data, top_data);
} else {
caffe_copy(count, bottom_data, top_data);
if (input_scale_ != Dtype(1)) {
caffe_scal(count, input_scale_, top_data);
}
if (input_shift_ != Dtype(0)) {
caffe_add_scalar(count, input_shift_, top_data);
}
caffe_log(count, top_data, top_data);
}
if (base_scale_ != Dtype(1)) {
caffe_scal(count, base_scale_, top_data);
}
}
template <typename Dtype>
void LogLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
if (!propagate_down[0]) { return; }
const int count = bottom[0]->count();
const Dtype* bottom_data = bottom[0]->cpu_data();
const Dtype* top_diff = top[0]->cpu_diff();
Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
caffe_copy(count, bottom_data, bottom_diff);
if (input_scale_ != Dtype(1)) {
caffe_scal(count, input_scale_, bottom_diff);
}
if (input_shift_ != Dtype(0)) {
caffe_add_scalar(count, input_shift_, bottom_diff);
}
caffe_powx(count, bottom_diff, Dtype(-1), bottom_diff);
if (backward_num_scale_ != Dtype(1)) {
caffe_scal(count, backward_num_scale_, bottom_diff);
}
caffe_mul(count, top_diff, bottom_diff, bottom_diff);
}
#ifdef CPU_ONLY
STUB_GPU(LogLayer);
#endif
INSTANTIATE_CLASS(LogLayer);
REGISTER_LAYER_CLASS(Log);
} // namespace caffe