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_PARAMETERIZED_TRUNCATED_NORMAL_OP_H_ |
17 | #define TENSORFLOW_CORE_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 | #include "tensorflow/core/util/bcast.h" |
22 | |
23 | namespace tensorflow { |
24 | |
25 | class OpKernelContext; |
26 | |
27 | namespace functor { |
28 | |
29 | // Sample a truncated normal random variable, with mean, stddev, minval, and |
30 | // maxval parameters for each batch. Uses two rejection sampling algorithms |
31 | // described in http://rd.springer.com/article/10.1007/BF00143942 and a randn |
32 | // rejection sampler when most of the normal is inside the bounds. |
33 | // |
34 | // Either minval may be -infinity, or maxval may be +infinity. If the interval |
35 | // (minval, maxval) is empty, the result is NaN. |
36 | template <typename Device, typename T> |
37 | struct TruncatedNormalFunctor { |
38 | void operator()(OpKernelContext* ctx, const Device& d, int64_t num_batches, |
39 | int64_t samples_per_batch, int64_t num_elements, |
40 | typename TTypes<T>::ConstFlat means, |
41 | typename TTypes<T>::ConstFlat stddevs, |
42 | typename TTypes<T>::ConstFlat minvals, |
43 | typename TTypes<T>::ConstFlat maxvals, |
44 | const random::PhiloxRandom& gen, |
45 | typename TTypes<T>::Flat output); |
46 | }; |
47 | |
48 | // This version supports broadcasting of the arguments, as well as puts |
49 | // the sample dimension on the left. |
50 | template <typename Device, typename T> |
51 | struct TruncatedNormalFunctorV2 { |
52 | void operator()(OpKernelContext* ctx, const Device& d, int64_t num_batches, |
53 | int64_t samples_per_batch, int64_t num_elements, |
54 | const BCastList<4>& bcast, |
55 | typename TTypes<T>::ConstFlat means, |
56 | typename TTypes<T>::ConstFlat stddevs, |
57 | typename TTypes<T>::ConstFlat minvals, |
58 | typename TTypes<T>::ConstFlat maxvals, |
59 | const random::PhiloxRandom& gen, |
60 | typename TTypes<T>::Flat output); |
61 | }; |
62 | |
63 | } // namespace functor |
64 | } // namespace tensorflow |
65 | |
66 | #endif // TENSORFLOW_CORE_KERNELS_PARAMETERIZED_TRUNCATED_NORMAL_OP_H_ |
67 | |