forked from seung-lab/znn-release
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitializer.hpp
78 lines (62 loc) · 2.01 KB
/
initializer.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// Copyright (C) 2014 Kisuk Lee <[email protected]>
// ----------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#ifndef ZNN_INITIALIZER_HPP_INCLUDED
#define ZNN_INITIALIZER_HPP_INCLUDED
#include "../core/types.hpp"
#include <boost/random.hpp>
namespace zi {
namespace znn {
class initializer
{
public:
virtual void initialize( double3d_ptr w ) = 0;
virtual void init( const std::string& params ) = 0;
}; // abstract class initializer
typedef boost::shared_ptr<initializer> initializer_ptr;
// global random number generator
namespace {
boost::mt19937 rng = boost::mt19937(time(0));
} // anonymous namespace
// for generating thread-safe uniform[0,1] random numbers
struct zero_one_generator_impl
{
private:
typedef boost::uniform_real<> uniform_dist;
typedef boost::variate_generator<boost::mt19937&, uniform_dist> generator;
boost::mt19937 rng_;
uniform_dist dis_;
generator gen_;
zi::mutex m_;
public:
zero_one_generator_impl()
: rng_(time(0))
, dis_(0,1)
, gen_(rng_, dis_)
{}
double rand()
{
zi::mutex::guard g(m_);
return gen_();
}
}; // struct zero_one_generator_impl
// global thread-safe unit uniform random number generator
namespace {
zero_one_generator_impl& zero_one_generator = zi::singleton<zero_one_generator_impl>::instance();
} // anonymous namespace
}} // namespace zi::znn
#endif // ZNN_INITIALIZER_HPP_INCLUDED