tiny_dnn 1.0.0
A header only, dependency-free deep learning framework in C++11
Loading...
Searching...
No Matches
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
34namespace tiny_dnn {
35
37public:
38 static random_generator& get_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 }
50private:
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
57template<typename T> inline
58typename std::enable_if<std::is_integral<T>::value, T>::type
59uniform_rand(T min, T max) {
60 std::uniform_int_distribution<T> dst(min, max);
61 return dst(random_generator::get_instance()());
62}
63
64template<typename T> inline
65typename std::enable_if<std::is_floating_point<T>::value, T>::type
66uniform_rand(T min, T max) {
67 std::uniform_real_distribution<T> dst(min, max);
68 return dst(random_generator::get_instance()());
69}
70
71template<typename T> inline
72typename std::enable_if<std::is_floating_point<T>::value, T>::type
73gaussian_rand(T mean, T sigma) {
74 std::normal_distribution<T> dst(mean, sigma);
75 return dst(random_generator::get_instance()());
76}
77
78inline void set_random_seed(unsigned int seed) {
79 random_generator::get_instance().set_seed(seed);
80}
81
82template<typename Container>
83inline int uniform_idx(const Container& t) {
84 return uniform_rand(0, int(t.size() - 1));
85}
86
87inline bool bernoulli(float_t p) {
88 return uniform_rand(float_t(0), float_t(1)) <= p;
89}
90
91template<typename Iter>
92void 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
97template<typename Iter>
98void 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
Simple image utility class.
Definition image.h:94
Definition random.h:36