/* 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 struct Sigmoid { void operator()(const platform::DeviceContext& device_context, const framework::Tensor& X, framework::Tensor* Y) { auto x = framework::EigenVector::Flatten(X); auto y = framework::EigenVector::Flatten(*Y); auto* place = device_context.template get_eigen_device(); y.device(*place) = 1. / (1. + (-x).exp()); } }; template 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::Flatten(*dX); auto y = framework::EigenVector::Flatten(Y); auto dy = framework::EigenVector::Flatten(dY); auto* place = device_context.template get_eigen_device(); dx.device(*place) = dy * y * (1. - y); } }; template struct Exp { void operator()(const platform::DeviceContext& device_context, const framework::Tensor& input, framework::Tensor* output) { auto x = framework::EigenVector::Flatten(input); auto y = framework::EigenVector::Flatten(*output); auto* place = device_context.template get_eigen_device(); y.device(*place) = x.exp(); } }; template 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::Flatten(*dX); auto dy = framework::EigenVector::Flatten(dY); auto* place = device_context.template get_eigen_device(); dx.device(*place) = dy.exp(); } }; template struct Relu { void operator()(const platform::DeviceContext& device_context, const framework::Tensor& input, framework::Tensor* output) { auto x = framework::EigenVector::Flatten(input); auto y = framework::EigenVector::Flatten(*output); auto* place = device_context.template get_eigen_device(); y.device(*place) = x.cwiseMax(static_cast(0)); } }; template 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::Flatten(*dX); auto dy = framework::EigenVector::Flatten(dY); auto x = framework::EigenVector::Flatten(X); auto* place = device_context.template get_eigen_device(); dx.device(*place) = dy * (x > static_cast(0)).template cast(); } }; } // namespace math } // namespace operators } // namespace paddle