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_CORE_KERNELS_SOFTPLUS_OP_H_ 17 #define TENSORFLOW_CORE_KERNELS_SOFTPLUS_OP_H_ 18 // Functor definition for SoftplusOp and SoftplusGradOp, 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 SoftplusOp to do the computations. 28 template <typename Device, typename T> 29 struct Softplus { 30 // Computes Softplus activation. 31 // 32 // features: any shape. 33 // activations: same shape as "features". operatorSoftplus34 void operator()(const Device& d, typename TTypes<T>::ConstTensor features, 35 typename TTypes<T>::Tensor activations) { 36 // Choose a threshold on x below which exp(x) may underflow 37 // when added to 1, but for which exp(x) is always within epsilon of the 38 // true softplus(x). Offset of 2 from machine epsilon checked 39 // experimentally for float16, float32, float64. Checked against 40 // softplus implemented with numpy's log1p and numpy's logaddexp. 41 static const T threshold = 42 Eigen::numext::log(Eigen::NumTraits<T>::epsilon()) + T(2); 43 // Value above which exp(x) may overflow, but softplus(x) == x 44 // is within machine epsilon. 45 auto too_large = features > features.constant(-threshold); 46 // Value below which exp(x) may underflow, but softplus(x) == exp(x) 47 // is within machine epsilon. 48 auto too_small = features < features.constant(threshold); 49 auto features_exp = features.exp(); 50 activations.device(d) = too_large.select( 51 features, // softplus(x) ~= x for x large 52 too_small.select(features_exp, // softplus(x) ~= exp(x) for x small 53 (features_exp + features.constant(T(1))).log())); 54 } 55 }; 56 57 // Functor used by SoftplusGradOp to do the computations. 58 template <typename Device, typename T> 59 struct SoftplusGrad { 60 // Computes SoftplusGrad backprops. 61 // 62 // gradients: gradients backpropagated to the Softplus op. 63 // features: inputs that where passed to the Softplus op. 64 // backprops: gradients to backpropagate to the Softplus inputs. operatorSoftplusGrad65 void operator()(const Device& d, typename TTypes<T>::ConstTensor gradients, 66 typename TTypes<T>::ConstTensor features, 67 typename TTypes<T>::Tensor backprops) { 68 backprops.device(d) = 69 gradients / ((-features).exp() + features.constant(T(1))); 70 } 71 }; 72 73 } // namespace functor 74 } // namespace tensorflow 75 76 #endif // TENSORFLOW_CORE_KERNELS_SOFTPLUS_OP_H_ 77