1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_KERNELS_SOFTSIGN_OP_H_ 17 #define TENSORFLOW_KERNELS_SOFTSIGN_OP_H_ 18 // Functor definition for SoftsignOp and SoftsignGradOp, must be compilable by 19 // nvcc. 20 21 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor" 22 #include "tensorflow/core/framework/tensor_types.h" 23 24 namespace tensorflow { 25 namespace functor { 26 27 // Functor used by SoftsignOp to do the computations. 28 template <typename Device, typename T> 29 struct Softsign { 30 // Computes Softsign activation. 31 // 32 // features: any shape. 33 // activations: same shape as "features". 34 void operator()(const Device& d, typename TTypes<T>::ConstTensor features, 35 typename TTypes<T>::Tensor activations) { 36 activations.device(d) = 37 features / (features.abs() + features.constant(T(1))); 38 } 39 }; 40 41 // Functor used by SoftsignGradOp to do the computations. 42 template <typename Device, typename T> 43 struct SoftsignGrad { 44 // Computes SoftsignGrad backprops. 45 // 46 // gradients: gradients backpropagated to the Softsign op. 47 // features: inputs that were passed to the Softsign op. 48 // backprops: gradients to backpropagate to the Softsign inputs. 49 void operator()(const Device& d, typename TTypes<T>::ConstTensor gradients, 50 typename TTypes<T>::ConstTensor features, 51 typename TTypes<T>::Tensor backprops) { 52 backprops.device(d) = 53 gradients / (features.abs() + features.constant(T(1))).square(); 54 } 55 }; 56 57 } // namespace functor 58 } // namespace tensorflow 59 60 #endif // TENSORFLOW_KERNELS_SOFTSIGN_OP_H_ 61