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_EIGEN_SOFTMAX_H_ 17 #define TENSORFLOW_CORE_KERNELS_EIGEN_SOFTMAX_H_ 18 19 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor" 20 21 namespace Eigen { 22 23 /** SoftMax 24 * \ingroup CXX11_NeuralNetworks_Module 25 * 26 * \brief Applies a softmax 27 * 28 * The input parameter is expected to be a col-major tensor with a rank of 2 29 * (depth and other). 30 * 31 * The result can be assigned to a tensor of rank and dimensions equal to that 32 * of the input. The result will be laid out in col-major order. 33 * 34 */ 35 36 namespace { 37 struct SoftmaxOp { 38 SoftmaxOp(const float beta) : beta_(beta) {} 39 40 template <typename Input> 41 typename Input::Dimensions dimensions(const Input& input) const { 42 return input.dimensions(); 43 } 44 45 template <typename Input, typename Output, typename Device> 46 void eval(const Input& input, Output& output, const Device& device) const { 47 #if !defined(EIGEN_HAS_INDEX_LIST) 48 // nvcc doesn't support cxx11 49 Eigen::array<typename internal::traits<Input>::Index, 1> depth_dim; 50 depth_dim[0] = 0; 51 Eigen::array<typename internal::traits<Input>::Index, 2> bcast; 52 bcast[0] = dimensions(input)[0]; 53 bcast[1] = 1; 54 DSizes<typename internal::traits<Input>::Index, 2> dims2d; 55 dims2d[0] = 1; 56 dims2d[1] = dimensions(input)[1]; 57 #else 58 // Take advantage of cxx11 to give the compiler information it can use to 59 // optimize the code. 60 Eigen::IndexList<Eigen::type2index<0> > depth_dim; 61 Eigen::IndexList<int, Eigen::type2index<1> > bcast; 62 bcast.set(0, dimensions(input)[0]); 63 Eigen::IndexList<Eigen::type2index<1>, 64 typename internal::traits<Input>::Index> 65 dims2d; 66 dims2d.set(1, dimensions(input)[1]); 67 #endif 68 69 output.device(device) = 70 ((input - 71 input.maximum(depth_dim).eval().reshape(dims2d).broadcast(bcast)) * 72 beta_) 73 .exp(); 74 output.device(device) = 75 output / 76 (output.sum(depth_dim).eval().reshape(dims2d).broadcast(bcast)); 77 } 78 79 private: 80 const float beta_; 81 }; 82 } // namespace 83 84 template <typename Input> 85 EIGEN_ALWAYS_INLINE static const TensorCustomUnaryOp<const SoftmaxOp, 86 const Input> 87 SoftMax(const Input& input, const float beta) { 88 EIGEN_STATIC_ASSERT(internal::traits<Input>::Layout == ColMajor, 89 YOU_MADE_A_PROGRAMMING_MISTAKE); 90 EIGEN_STATIC_ASSERT(internal::traits<Input>::NumDimensions == 2, 91 YOU_MADE_A_PROGRAMMING_MISTAKE); 92 93 const SoftmaxOp op(beta); 94 return input.customOp(op); 95 } 96 97 } // end namespace Eigen 98 99 #endif // TENSORFLOW_CORE_KERNELS_EIGEN_SOFTMAX_H_ 100