19 #ifndef SINGA_MODEL_OPTIMIZER_H_ 20 #define SINGA_MODEL_OPTIMIZER_H_ 23 #include <unordered_map> 26 #include "singa/core/tensor.h" 27 #include "singa/proto/model.pb.h" 31 using std::unordered_map;
49 conf.ParseFromString(str);
54 virtual void Setup(
const OptimizerConf& conf);
58 virtual void Register(
const string& name,
const ParamSpec& specs);
61 virtual void ApplyRegularizerConstraint(
int epoch,
const string& name,
69 virtual void Apply(
int epoch,
float lr,
const string& name,
82 learning_rate_generator_ = func;
84 float GetLearningRate(
int step) {
85 if (learning_rate_generator_)
86 return learning_rate_generator_(step);
92 function<float(int)> learning_rate_generator_;
93 std::unordered_map<std::string, float> learning_rate_multplier_;
94 std::unordered_map<std::string, Constraint*> constraints_;
95 std::unordered_map<std::string, Regularizer*> regularizers_;
110 Constraint(
const string& type,
float threshold)
111 : type_(type), threshold_(threshold) {}
112 void Setup(
const ConstraintConf& conf);
113 void Setup(
const string& conf_str) {
115 conf.ParseFromString(conf_str);
125 void Apply(
int epoch,
const vector<Tensor>& values,
126 const vector<Tensor>& grads,
int step = -1);
133 string type_ =
"Unknown";
137 inline std::shared_ptr<Constraint> CreateConstraint(std::string type) {
138 return std::make_shared<Constraint>();
147 : type_(type), coefficient_(coefficient) {}
148 void Setup(
const RegularizerConf& conf);
149 void Setup(
const string& conf_str) {
150 RegularizerConf conf;
151 conf.ParseFromString(conf_str);
162 void Apply(
int epoch,
const vector<Tensor>& values,
163 const vector<Tensor>& grads,
int step = -1);
168 string type_ =
"NotSet";
171 inline std::shared_ptr<Regularizer> CreateRegularizer(std::string type) {
172 return std::make_shared<Regularizer>();
180 void Setup(
const OptimizerConf& conf);
182 void Apply(
int epoch,
float lr,
const string& name,
Tensor& grad,
183 Tensor& value,
int step = -1)
override;
188 momentum_generator_ = func;
192 std::unordered_map<string, Tensor> history_gradient_;
193 std::function<float(int)> momentum_generator_;
199 void Setup(
const OptimizerConf& conf);
201 void Apply(
int epoch,
float lr,
const string& name,
Tensor& grad,
202 Tensor& value,
int step = -1)
override;
207 momentum_generator_ = func;
211 std::unordered_map<string, Tensor> history_gradient_;
212 std::function<float(int)> momentum_generator_;
218 void Setup(
const OptimizerConf& conf);
220 void Apply(
int epoch,
float lr,
const string& name,
Tensor& grad,
221 Tensor& value,
int step = -1)
override;
224 std::unordered_map<string, Tensor> history_gradient_;
230 void Setup(
const OptimizerConf& conf);
232 void Apply(
int epoch,
float lr,
const string& name,
Tensor& grad,
233 Tensor& value,
int step = -1)
override;
237 std::unordered_map<string, Tensor> history_gradient_;
242 inline std::shared_ptr<Optimizer> CreateOptimizer(
const string& type) {
243 std::shared_ptr<Optimizer> opt;
245 opt = std::shared_ptr<Optimizer>(
new SGD());
246 else if (type ==
"RMSProp")
247 opt = std::shared_ptr<Optimizer>(
new RMSProp());
248 else if (type ==
"AdaGrad")
249 opt = std::shared_ptr<Optimizer>(
new AdaGrad());
250 else if (type ==
"Nesterov")
251 opt = std::shared_ptr<Optimizer>(
new Nesterov());
253 LOG(FATAL) <<
"Unknown optimizer type : " << type;
308 #endif // SINGA_MODEL_OPTIMIZER_H_ virtual void Apply(int epoch, float lr, const string &name, Tensor &grad, Tensor &value, int step=-1)=0
Apply the updating algorithm if the gradient is not empty.
A Tensor instance is a multi-dimensional array resident on a Device (default device is the host CPU)...
Definition: tensor.h:56
Apply constraints for parameters (gradient).
Definition: optimizer.h:106
Apply regularization for parameters (gradient), e.g., L1 norm and L2 norm.
Definition: optimizer.h:142
Definition: optimizer.h:197
void SetMomentumGenerator(std::function< float(int)> func)
The argument function returns the momentum value given the current running step (i.e., iterations/mini-batches).
Definition: optimizer.h:206
virtual void Register(const string &name, const ParamSpec &specs)
Register the parameter, e.g., create Constraint and Regularizers.
Definition: optimizer.h:228
void SetLearningRateGenerator(function< float(int)> func)
The argument is a function that returns the learning rate given the current step (i.e., curren running iteration).
Definition: optimizer.h:81
void SetMomentumGenerator(std::function< float(int)> func)
The argument function returns the momentum value given the current running step (i.e., iterations/mini-batches).
Definition: optimizer.h:187
void Setup(const string &str)
Setup the optimzier using configurations from serialized string (for binding languages).
Definition: optimizer.h:47
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: common.h:48
Definition: optimizer.h:178
The base class for gradient descent algorithms used to update the model parameters in order to optimi...
Definition: optimizer.h:41
Definition: optimizer.h:216