19 #ifndef SINGA_MODEL_INITIALIZER_H_ 20 #define SINGA_MODEL_INITIALIZER_H_ 22 #include "singa/core/tensor.h" 23 #include "singa/proto/model.pb.h" 24 #include "singa/utils/string.h" 31 void Setup(
const std::string& str) {
33 conf.ParseFromString(str);
40 virtual void Fill(
Tensor& t) = 0;
58 Uniform(
const float low,
const float high) : min_(low), max_(high) {}
66 float min_ = 0, max_ = 1;
72 Gaussian(
const float m,
const float s): mean_(m), std_(s) {}
80 float mean_ = 0, std_ = 1;
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;
99 void Fill(
Tensor& t)
override {
100 CHECK_EQ(t.nDim(), 2u);
101 float std = sqrt(2.0f / t.shape(0));
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>();
122 LOG(FATAL) <<
"Unknown initialization type: " << conf.type();
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 Gaussian(const SType mean, const SType std, Tensor *out)
Fill in Tensor 't' 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 't' following uniform distribution.
virtual void Setup(const InitializerConf &conf)
Set meta fields from user configurations.
Definition: initializer.h:38
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