tiny_dnn  1.0.0
A header only, dependency-free deep learning framework in C++11
random.h
1 /*
2  Copyright (c) 2013, Taiga Nomi
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the <organization> nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
17  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 #pragma once
28 #include <random>
29 #include <type_traits>
30 #include <limits>
31 #include "nn_error.h"
32 #include "tiny_dnn/config.h"
33 
34 namespace tiny_dnn {
35 
37 public:
38  static random_generator& get_instance() {
39  static random_generator instance;
40  return instance;
41  }
42 
43  std::mt19937& operator()() {
44  return gen_;
45  }
46 
47  void set_seed(unsigned int seed) {
48  gen_.seed(seed);
49  }
50 private:
51  // avoid gen_(0) for MSVC known issue
52  // https://connect.microsoft.com/VisualStudio/feedback/details/776456
53  random_generator() : gen_(1) {}
54  std::mt19937 gen_;
55 };
56 
57 template<typename T> inline
58 typename std::enable_if<std::is_integral<T>::value, T>::type
59 uniform_rand(T min, T max) {
60  std::uniform_int_distribution<T> dst(min, max);
61  return dst(random_generator::get_instance()());
62 }
63 
64 template<typename T> inline
65 typename std::enable_if<std::is_floating_point<T>::value, T>::type
66 uniform_rand(T min, T max) {
67  std::uniform_real_distribution<T> dst(min, max);
68  return dst(random_generator::get_instance()());
69 }
70 
71 template<typename T> inline
72 typename std::enable_if<std::is_floating_point<T>::value, T>::type
73 gaussian_rand(T mean, T sigma) {
74  std::normal_distribution<T> dst(mean, sigma);
75  return dst(random_generator::get_instance()());
76 }
77 
78 inline void set_random_seed(unsigned int seed) {
79  random_generator::get_instance().set_seed(seed);
80 }
81 
82 template<typename Container>
83 inline int uniform_idx(const Container& t) {
84  return uniform_rand(0, int(t.size() - 1));
85 }
86 
87 inline bool bernoulli(float_t p) {
88  return uniform_rand(float_t(0), float_t(1)) <= p;
89 }
90 
91 template<typename Iter>
92 void uniform_rand(Iter begin, Iter end, float_t min, float_t max) {
93  for (Iter it = begin; it != end; ++it)
94  *it = uniform_rand(min, max);
95 }
96 
97 template<typename Iter>
98 void gaussian_rand(Iter begin, Iter end, float_t mean, float_t sigma) {
99  for (Iter it = begin; it != end; ++it)
100  *it = gaussian_rand(mean, sigma);
101 }
102 
103 } // namespace tiny_dnn
Definition: random.h:36