Apache Singa
A General Distributed Deep Learning Library
initializer.h
1 
19 #ifndef SINGA_MODEL_INITIALIZER_H_
20 #define SINGA_MODEL_INITIALIZER_H_
21 #include <string>
22 #include "singa/core/tensor.h"
23 #include "singa/proto/model.pb.h"
24 #include "singa/utils/string.h"
25 namespace singa {
27 using InitializerConf = FillerConf;
28 class Initializer {
29  public:
30  Initializer() = default;
31  void Setup(const std::string& str) {
32  InitializerConf conf;
33  conf.ParseFromString(str);
34  Setup(conf);
35  }
36 
38  virtual void Setup(const InitializerConf& conf) {}
39 
40  virtual void Fill(Tensor& t) = 0;
41 };
42 
43 namespace init {
44 class Constant : public Initializer {
45 public:
46  Constant() = default;
47  Constant(const float x) : v_(x) {}
48  void Setup(const InitializerConf& conf) override { v_ = conf.value(); }
49  void Fill(Tensor& t) override { t.SetValue(v_); }
50 
51  private:
52  float v_ = 0;
53 };
54 
55 class Uniform : public Initializer {
56 public:
57  Uniform() = default;
58  Uniform(const float low, const float high) : min_(low), max_(high) {}
59  void Setup(const InitializerConf& conf) override {
60  min_ = conf.min();
61  max_ = conf.max();
62  }
63  void Fill(Tensor& t) override { singa::Uniform(min_, max_, &t); }
64 
65  private:
66  float min_ = 0, max_ = 1;
67 };
68 
69 class Gaussian : public Initializer {
70 public:
71  Gaussian() = default;
72  Gaussian(const float m, const float s): mean_(m), std_(s) {}
73  void Setup(const InitializerConf& conf) override {
74  mean_ = conf.mean();
75  std_ = conf.std();
76  }
77  void Fill(Tensor& t) override { singa::Gaussian(mean_, std_, &t); }
78 
79  private:
80  float mean_ = 0, std_ = 1;
81 };
82 
85 class Xavier : public Initializer {
86 public:
87  void Fill(Tensor& t) override {
88  CHECK_EQ(t.nDim(), 2u);
89  float scale = sqrt(6.0f / (t.shape(0) + t.shape(1)));
90  LOG(INFO) << "xavier scale " << scale;
91  singa::Uniform(-scale, scale, &t);
92  }
93 };
94 
97 class MSRA : public Initializer {
98  public:
99  void Fill(Tensor& t) override {
100  CHECK_EQ(t.nDim(), 2u);
101  float std = sqrt(2.0f / t.shape(0));
102  singa::Gaussian(0.0f, std, &t);
103  }
104 };
105 
106 } // namespace init
107 
109 std::shared_ptr<Initializer> CreateInitializer(const InitializerConf& conf) {
110  std::shared_ptr<Initializer> init;
111  if (ToLowerCase(conf.type()) == "constant") {
112  init = std::make_shared<init::Constant>();
113  } else if (ToLowerCase(conf.type()) == "uniform") {
114  init = std::make_shared<init::Uniform>();
115  } else if (ToLowerCase(conf.type()) == "gaussian") {
116  init = std::make_shared<init::Gaussian>();
117  } else if (ToLowerCase(conf.type()) == "xavier") {
118  init = std::make_shared<init::Xavier>();
119  } else if (ToLowerCase(conf.type()) == "msra") {
120  init = std::make_shared<init::MSRA>();
121  } else {
122  LOG(FATAL) << "Unknown initialization type: " << conf.type();
123  }
124  init->Setup(conf);
125  return init;
126 }
127 } // namespace singa
128 #endif // SINGA_MODEL_INITIALIZER_H_
Definition: initializer.h:69
Ref: [Bengio and Glorot 2010] Understanding the difficulty of training deep feedforward neural networ...
Definition: initializer.h:85
Definition: initializer.h:44
Ref: [He, Zhang, Ren and Sun 2015]: Delving Deep into Rectifiers: Surpassing Human-Level Performance ...
Definition: initializer.h:97
void SetValue(const SType x)
Set each element of the tensor to be x.
FillerConf InitializerConf
Base class for initializing parameter values.
Definition: initializer.h:27
A Tensor instance is a multi-dimensional array resident on a Device (default device is the host CPU)...
Definition: tensor.h:56
void Setup(const InitializerConf &conf) override
Set meta fields from user configurations.
Definition: initializer.h:59
void Gaussian(const SType mean, const SType std, Tensor *out)
Fill in Tensor &#39;t&#39; following Gaussian distribution.
void Setup(const InitializerConf &conf) override
Set meta fields from user configurations.
Definition: initializer.h:73
std::shared_ptr< Initializer > CreateInitializer(const InitializerConf &conf)
TODO(wangwei) create the initializers from factory like that for Layer.
Definition: initializer.h:109
void Uniform(const SType low, const SType high, Tensor *out)
Fill in Tensor &#39;t&#39; following uniform distribution.
virtual void Setup(const InitializerConf &conf)
Set meta fields from user configurations.
Definition: initializer.h:38
Definition: initializer.h:55
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: common.h:48
Definition: initializer.h:28
void Setup(const InitializerConf &conf) override
Set meta fields from user configurations.
Definition: initializer.h:48