Home | History | Annotate | Download | only in kernels
      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_PARAMETERIZED_TRUNCATED_NORMAL_OP_H_
     17 #define TENSORFLOW_KERNELS_PARAMETERIZED_TRUNCATED_NORMAL_OP_H_
     18 
     19 #include "tensorflow/core/framework/tensor_types.h"
     20 #include "tensorflow/core/lib/random/random_distributions.h"
     21 
     22 namespace tensorflow {
     23 
     24 class OpKernelContext;
     25 
     26 namespace functor {
     27 
     28 // Sample a truncated normal random variable, with mean, stddev, minval, and
     29 // maxval parameters for each batch. Uses two rejection sampling algorithms
     30 // described in http://rd.springer.com/article/10.1007/BF00143942.
     31 //
     32 // Either minval may be -infinity, or maxval may be +infinity. If the interval
     33 // (minval, maxval) is empty, the result is NaN. Large intervals which include
     34 // both tails may have reduced accuracy.
     35 template <typename Device, typename T>
     36 struct TruncatedNormalFunctor {
     37   void operator()(OpKernelContext* ctx, const Device& d, int64 num_batches,
     38                   int64 samples_per_batch, int64 num_elements,
     39                   typename TTypes<T>::ConstFlat means,
     40                   typename TTypes<T>::ConstFlat stddevs,
     41                   typename TTypes<T>::ConstFlat minvals,
     42                   typename TTypes<T>::ConstFlat maxvals,
     43                   const random::PhiloxRandom& gen,
     44                   typename TTypes<T>::Flat output);
     45 
     46   static const int kMaxIterations = 100;
     47 };
     48 
     49 }  // namespace functor
     50 }  // namespace tensorflow
     51 
     52 #endif  // TENSORFLOW_KERNELS_PARAMETERIZED_TRUNCATED_NORMAL_OP_H_
     53