-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
updater.cc
284 lines (256 loc) · 9.84 KB
/
updater.cc
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*************************************************************/
#include "singa/utils/updater.h"
#include "mshadow/cxxnet_op.h"
#include "mshadow/tensor.h"
#include "singa/utils/singleton.h"
#include "singa/utils/factory.h"
namespace singa {
using mshadow::cpu;
using mshadow::expr::F;
using mshadow::op::sqrtop;
using mshadow::op::square;
using mshadow::Shape;
using mshadow::Shape1;
using mshadow::Tensor;
using mshadow::TensorContainer;
LRGenerator* LRGenerator::Create(const LRGenProto& proto) {
auto factory = Singleton<Factory<LRGenerator>>::Instance();
LRGenerator* gen = nullptr;
if (proto.has_user_type())
gen = factory->Create(proto.user_type());
else
gen = factory->Create(proto.type());
gen->Init(proto);
return gen;
}
float FixedStepLRGen::Get(int step) {
if (last_idx_ < proto_.fixedstep_conf().step_size() - 1
&& step >= proto_.fixedstep_conf().step(last_idx_ + 1)) {
last_idx_++;
}
return proto_.fixedstep_conf().step_lr(last_idx_);
}
float StepLRGen::Get(int step) {
// do not cast int to float
int freq = proto_.step_conf().change_freq();
float lr = proto_.base_lr() * pow(proto_.step_conf().gamma(), step / freq);
// LOG_IF(INFO, step % freq == 0) << "Update learning rate to " << lr
// << " @ step " << step;
return lr;
}
float LinearLRGen::Get(int step) {
int freq = proto_.linear_conf().change_freq();
float r = step * 1.0 / freq;
return (1.0 - r) * proto_.base_lr() + r * proto_.linear_conf().final_lr();
}
float ExpLRGen::Get(int step) {
int freq = proto_.exponential_conf().change_freq();
return proto_.base_lr() / pow(2, step * 1. / freq);
}
float InvLRGen::Get(int step) {
return proto_.base_lr() * pow(1.f + proto_.inverse_conf().gamma() * step,
- proto_.inverse_conf().pow());
}
float InvTLRGen::Get(int step) {
return proto_.base_lr() / (1 + step * 1. / proto_.inverset_conf().final_lr());
}
Updater* Updater::Create(const UpdaterProto& proto) {
auto factory = Singleton<Factory<Updater>>::Instance();
Updater* updater = nullptr;
if (proto.has_user_type())
updater = factory->Create(proto.user_type());
else
updater = factory->Create(proto.type());
updater->Init(proto);
return updater;
}
/**************** added for Python Binding ***************************/
Updater* Updater::CreateUpdater(const string str) {
UpdaterProto conf;
conf.ParseFromString(str);
return Updater::Create(conf);
}
/***********************Python Binding end**************************/
/***********************SGD with momentum******************************/
void Updater::Init(const UpdaterProto& proto) {
momentum_ = proto.momentum();
weight_decay_ = proto.weight_decay();
lr_gen_ = LRGenerator::Create(proto.learning_rate());
clip_low_ = proto.clip_low();
clip_high_ = proto.clip_high();
}
void Updater::Clip(const float low, const float high, Param* param) {
Blob<float>* grad = param->mutable_grad();
float* ptr = grad->mutable_cpu_data();
for (int i = 0; i < grad->count(); i++) {
if (ptr[i] > high)
ptr[i] = high;
else if (ptr[i] < low)
ptr[i] = low;
}
}
void SGDUpdater::Update(int step, Param* param, float grad_scale) {
if (clip_high_ > clip_low_)
Clip(clip_low_, clip_high_, param);
Shape<1> s = Shape1(param->size());
Tensor<cpu, 1> data(param->mutable_cpu_data(), s);
Tensor<cpu, 1> grad(param->mutable_cpu_grad(), s);
float lr = lr_gen_->Get(step) * param->lr_scale();
float wd = weight_decay_ * param->wd_scale();
grad *= grad_scale;
if (wd > 0) // L2 regularization, should be done after timing grad_scale
grad += data * wd;
if (momentum_ > 0) {
Tensor<cpu, 1> history(param->mutable_cpu_history(), s);
history = history * momentum_ - lr * grad;
data += history;
} else {
grad *= -lr;
data += grad;
}
}
/***********************Nesterov******************************/
void NesterovUpdater::Update(int step, Param* param, float grad_scale) {
if (clip_high_ > clip_low_)
Clip(clip_low_, clip_high_, param);
Shape<1> s = Shape1(param->size());
Tensor<cpu, 1> data(param->mutable_cpu_data(), s);
Tensor<cpu, 1> grad(param->mutable_cpu_grad(), s);
Tensor<cpu, 1> history(param->mutable_cpu_history(), s);
TensorContainer<cpu, 1> tmp(s);
float lr = lr_gen_->Get(step)*param->lr_scale();
float wd = weight_decay_*param->wd_scale();
grad *= grad_scale;
if (wd > 0) // L2 regularization, should be done after timing grad_scale
grad += data * wd;
Copy(tmp, history);
history = history * momentum_ + lr * grad;
tmp = history * (1 + momentum_) - tmp * momentum_;
data -= tmp;
}
/***********************AdaGrad******************************/
void AdaGradUpdater::Update(int step, Param* param, float grad_scale) {
if (clip_high_ > clip_low_)
Clip(clip_low_, clip_high_, param);
Shape<1> s = Shape1(param->size());
Tensor<cpu, 1> data(param->mutable_cpu_data(), s);
Tensor<cpu, 1> grad(param->mutable_cpu_grad(), s);
Tensor<cpu, 1> history(param->mutable_cpu_history(), s);
float lr = lr_gen_->Get(step)*param->lr_scale();
float wd = weight_decay_*param->wd_scale();
grad *= grad_scale;
if (wd > 0) // L2 regularization, should be done after timing grad_scale
grad += data * wd;
history += F<square>(grad);
data -= lr * grad / (F<sqrtop>(history, proto_.delta()));
}
/***********************RMSProp******************************/
void RMSPropUpdater::Init(const UpdaterProto& proto) {
Updater::Init(proto);
rho_ = proto.rmsprop_conf().rho();
delta_ = proto.delta();
}
void RMSPropUpdater::Update(int step, Param* param, float grad_scale) {
if (clip_high_ > clip_low_)
Clip(clip_low_, clip_high_, param);
Shape<1> s=Shape1(param->size());
Tensor<cpu, 1> data(param->mutable_cpu_data(), s);
Tensor<cpu, 1> grad(param->mutable_cpu_grad(), s);
Tensor<cpu, 1> history(param->mutable_cpu_history(), s);
float lr = lr_gen_->Get(step) * param->lr_scale();
float wd = weight_decay_ * param->wd_scale();
grad *= grad_scale;
if (wd > 0) // L2 regularization, should be done after timing grad_scale
grad += data * wd;
history = history * rho_ + (1 - rho_) * F<square>(grad);
data -= lr * grad / F<sqrtop>(history, delta_);
}
/***********************AdaDelta******************************/
void AdaDeltaUpdater::Init(const UpdaterProto& proto){
Updater::Init(proto);
delta_ = proto.delta();
rho_=proto.adadelta_conf().rho();
}
void AdaDeltaUpdater::Update(int step, Param* param, float grad_scale){
Shape<1> s=Shape1(param->size());
Tensor<cpu, 1> data(param->mutable_cpu_data(), s);
Tensor<cpu, 1> grad(param->mutable_cpu_grad(), s);
Tensor<cpu, 1> history(param->mutable_cpu_history(), s);
Tensor<cpu, 1> update(param->mutable_cpu_update(), s);
TensorContainer<cpu, 1> tmp(s);
float wd = weight_decay_*param->wd_scale();
float lr = lr_gen_->Get(step) * param->lr_scale();
grad *= grad_scale;
if (wd > 0) // L2 regularization, should be done after timing grad_scale
grad += data * wd;
history = history * rho_ + (1 - rho_) * F<op::square>(grad);
tmp = grad * F<op::sqrtop>(update, delta_) / F<op::sqrtop>(history, delta_);
update = rho_ * update + (1 - rho_) * F<op::square>(tmp);
data -= lr * tmp;
}
/***********************Adam******************************/
void AdamUpdater::Init(const UpdaterProto &proto) {
Updater::Init(proto);
beta1_=proto.adam_conf().beta1();
beta2_=proto.adam_conf().beta2();
delta_ = proto.delta();
}
void AdamUpdater::Update(int step, Param* param, float grad_scale) {
Shape<1> s=Shape1(param->size());
Tensor<cpu, 1> data(param->mutable_cpu_data(), s);
Tensor<cpu, 1> grad(param->mutable_cpu_grad(), s);
Tensor<cpu, 1> history(param->mutable_cpu_history(), s);
Tensor<cpu, 1> update(param->mutable_cpu_update(), s);
float wd = weight_decay_*param->wd_scale();
float lr = lr_gen_->Get(step) * param->lr_scale();
grad *= grad_scale;
if (wd > 0) // L2 regularization, should be done after timing grad_scale
grad += data * wd;
history = history * beta1_ + (1 - beta1_) * grad;
update = update * beta2_ + (1 - beta2_) * F<op::square>(grad);
data -= lr * history / F<op::sqrtop>(update, delta_);
}
/***********************AdamMax******************************/
void AdamMaxUpdater::Init(const UpdaterProto &proto) {
Updater::Init(proto);
beta1_=proto.adammax_conf().beta1();
beta2_=proto.adammax_conf().beta2();
delta_=proto.delta();
}
void AdamMaxUpdater::Update(int step, Param* param, float grad_scale) {
Shape<1> s=Shape1(param->size());
Tensor<cpu, 1> data(param->mutable_cpu_data(), s);
Tensor<cpu, 1> grad(param->mutable_cpu_grad(), s);
Tensor<cpu, 1> history(param->mutable_cpu_history(), s);
Tensor<cpu, 1> update(param->mutable_cpu_update(), s);
float wd = weight_decay_*param->wd_scale();
float lr = lr_gen_->Get(step) * param->lr_scale();
grad *= grad_scale;
if (wd > 0) // L2 regularization, should be done after timing grad_scale
grad += data * wd;
history = history * beta1_ + (1 - beta1_) * grad;
update = update * beta2_;
grad = F<op::abs>(grad);
update = F<op::max>(update, grad) + delta_;
data -= lr * history / update;
}
} // namespace singa