activation_functor.h 3.6 KB
Newer Older
Q
qijun 已提交
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
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License. */

#pragma once
#include "paddle/framework/eigen.h"
#include "paddle/framework/tensor.h"

namespace paddle {
namespace operators {
namespace math {

template <typename Place, typename T>
struct Sigmoid {
  void operator()(const platform::DeviceContext& device_context,
                  const framework::Tensor& X, framework::Tensor* Y) {
    auto x = framework::EigenVector<T>::Flatten(X);
    auto y = framework::EigenVector<T>::Flatten(*Y);
    auto* place = device_context.template get_eigen_device<Place>();
    y.device(*place) = 1. / (1. + (-x).exp());
  }
};

template <typename Place, typename T>
struct SigmoidGrad {
  void operator()(const platform::DeviceContext& device_context,
                  const framework::Tensor& X, const framework::Tensor& Y,
                  const framework::Tensor& dY, framework::Tensor* dX) {
    auto dx = framework::EigenVector<T>::Flatten(*dX);
    auto y = framework::EigenVector<T>::Flatten(Y);
    auto dy = framework::EigenVector<T>::Flatten(dY);
    auto* place = device_context.template get_eigen_device<Place>();
    dx.device(*place) = dy * y * (1. - y);
  }
};

template <typename Place, typename T>
struct Exp {
  void operator()(const platform::DeviceContext& device_context,
                  const framework::Tensor& input, framework::Tensor* output) {
    auto x = framework::EigenVector<T>::Flatten(input);
    auto y = framework::EigenVector<T>::Flatten(*output);
    auto* place = device_context.template get_eigen_device<Place>();
    y.device(*place) = x.exp();
  }
};

template <typename Place, typename T>
struct ExpGrad {
  void operator()(const platform::DeviceContext& device_context,
                  const framework::Tensor& X, const framework::Tensor& Y,
                  const framework::Tensor& dY, framework::Tensor* dX) {
    auto dx = framework::EigenVector<T>::Flatten(*dX);
Q
qijun 已提交
64
    auto y = framework::EigenVector<T>::Flatten(Y);
Q
qijun 已提交
65
    auto* place = device_context.template get_eigen_device<Place>();
Q
qijun 已提交
66
    dx.device(*place) = y;
Q
qijun 已提交
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
  }
};

template <typename Place, typename T>
struct Relu {
  void operator()(const platform::DeviceContext& device_context,
                  const framework::Tensor& input, framework::Tensor* output) {
    auto x = framework::EigenVector<T>::Flatten(input);
    auto y = framework::EigenVector<T>::Flatten(*output);
    auto* place = device_context.template get_eigen_device<Place>();
    y.device(*place) = x.cwiseMax(static_cast<T>(0));
  }
};

template <typename Place, typename T>
struct ReluGrad {
  void operator()(const platform::DeviceContext& device_context,
                  const framework::Tensor& X, const framework::Tensor& Y,
                  const framework::Tensor& dY, framework::Tensor* dX) {
    auto dx = framework::EigenVector<T>::Flatten(*dX);
    auto dy = framework::EigenVector<T>::Flatten(dY);
    auto x = framework::EigenVector<T>::Flatten(X);
    auto* place = device_context.template get_eigen_device<Place>();
    dx.device(*place) = dy * (x > static_cast<T>(0)).template cast<T>();
  }
};

}  // namespace math
}  // namespace operators
}  // namespace paddle