-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkl_loss_layer.cpp
48 lines (40 loc) · 1.3 KB
/
kl_loss_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
#include <vector>
#include "caffe/layers/kl_loss_layer.hpp"
namespace caffe {
template <typename Dtype>
void KlLossLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
LossLayer<Dtype>::LayerSetUp(bottom, top);
has_weights_=(bottom.size()==4);
}
template <typename Dtype>
void KlLossLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
LossLayer<Dtype>::Reshape(bottom, top);
CHECK(bottom[0]->shape()==bottom[1]->shape());
CHECK(bottom[0]->shape()==bottom[2]->shape());
if (has_weights_)
CHECK(bottom[0]->shape()==bottom[3]->shape());
diff_.ReshapeLike(*bottom[0]);
error_.ReshapeLike(*bottom[0]);
ones_.ReshapeLike(*bottom[0]);
for (int i=0; i<bottom[0]->count(); ++i) {
ones_.mutable_cpu_data()[i] = Dtype(1);
}
}
template <typename Dtype>
void KlLossLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
NOT_IMPLEMENTED;
}
template <typename Dtype>
void KlLossLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
NOT_IMPLEMENTED;
}
#ifdef CPU_ONLY
STUB_GPU(KlLossLayer);
#endif
INSTANTIATE_CLASS(KlLossLayer);
REGISTER_LAYER_CLASS(KlLoss);
} //namespace caffe