text: #include <cmath>
  #include <vector>
  
  //#include "caffe/layers/swish_layer.hpp"
  #include "swish_layer.hpp"
  
  namespace caffe {
  
  template <typename Dtype>
  inline Dtype sigmoid(Dtype x) {
  //sigmoid的近似快速实现[参考这里](https://stackoverflow.com/questions/10732027/fast-sigmoid-algorithm)
    return 0.5 * tanh(0.5 * x) + 0.5;
  }
  
  template <typename Dtype>
  inline Dtype swish(Dtype x, Dtype sigmoid_x) {
    return x * sigmoid_x;
  }
  
  template <typename Dtype>
  void SwishLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
        const vector<Blob<Dtype>*>& top) {
    NeuronLayer<Dtype>::Reshape(bottom, top);
    this->sigmoid_x_.ReshapeLike(*bottom[0]);
  }
  
  template <typename Dtype>
  void SwishLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top) {
    const Dtype* bottom_data = bottom[0]->cpu_data();
    Dtype* top_data = top[0]->mutable_cpu_data();
    Dtype* sigmoid_x_data = this->sigmoid_x_.mutable_cpu_data();
    const int count = bottom[0]->count();
    for (int i = 0; i < count; ++i) {
      sigmoid_x_data[i] = sigmoid(bottom_data[i]);
      top_data[i] = swish(bottom_data[i], sigmoid_x_data[i]);
    }
  }
  
  template <typename Dtype>
  void SwishLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down,
      const vector<Blob<Dtype>*>& bottom) {
    if (propagate_down[0]) {
      const Dtype* top_data = top[0]->cpu_data();
      const Dtype* top_diff = top[0]->cpu_diff();
      const Dtype* sigmoid_x_data = this->sigmoid_x_.cpu_data();
      Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
      const int count = bottom[0]->count();
      for (int i = 0; i < count; ++i) {
        const Dtype swish_x = top_data[i];
        bottom_diff[i] = top_diff[i] * (swish_x + sigmoid_x_data[i]
            * (1. - swish_x));
      }
    }
  }
  
  #ifdef CPU_ONLY
  STUB_GPU(SwishLayer);
  #endif
  
  INSTANTIATE_CLASS(SwishLayer);
  REGISTER_LAYER_CLASS(Swish);
  
  }  // namespace caffe
  