#include #include #include #include "caffe/layer.hpp" #include "caffe/vision_layers.hpp" namespace caffe { template inline Dtype sigmoid(Dtype x) { return 1. / (1. + exp(-x)); } template void SigmoidLayer::Forward_cpu(const vector*>& bottom, const vector*>& top) { const Dtype* bottom_data = bottom[0]->cpu_data(); Dtype* top_data = top[0]->mutable_cpu_data(); const int count = bottom[0]->count(); for (int i = 0; i < count; ++i) { top_data[i] = sigmoid(bottom_data[i]); } } template void SigmoidLayer::Backward_cpu(const vector*>& top, const vector& propagate_down, const vector*>& bottom) { if (propagate_down[0]) { const Dtype* top_data = top[0]->cpu_data(); const Dtype* top_diff = top[0]->cpu_diff(); Dtype* bottom_diff = bottom[0]->mutable_cpu_diff(); const int count = bottom[0]->count(); for (int i = 0; i < count; ++i) { const Dtype sigmoid_x = top_data[i]; bottom_diff[i] = top_diff[i] * sigmoid_x * (1. - sigmoid_x); } } } #ifdef CPU_ONLY STUB_GPU(SigmoidLayer); #endif INSTANTIATE_CLASS(SigmoidLayer); } // namespace caffe