-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
optimizer.cc
121 lines (109 loc) · 4.33 KB
/
optimizer.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
/**
* 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/model/optimizer.h"
#include "singa/utils/logging.h"
namespace singa {
Optimizer::~Optimizer() {
for (auto entry : regularizers_) delete entry.second;
for (auto entry : constraints_) delete entry.second;
if (constraint_ != nullptr) delete constraint_;
if (regularizer_ != nullptr) delete regularizer_;
}
void Optimizer::Setup(const OptimizerConf& conf) {
if (conf.has_regularizer())
regularizer_ = new Regularizer(conf.regularizer());
if (conf.has_constraint()) constraint_ = new Constraint(conf.constraint());
conf_ = conf;
}
void Optimizer::Register(const string& name, const ParamSpec& specs) {
if (specs.has_constraint()) {
CHECK(constraints_.find(name) == constraints_.end())
<< "Parameter with name = " << name << " has already registered";
constraints_[name] = new Constraint(specs.constraint());
}
if (specs.has_regularizer()) {
CHECK(regularizers_.find(name) == regularizers_.end())
<< "Parameter with name = " << name << " has already registered";
regularizers_[name] = new Regularizer(specs.regularizer());
}
if (specs.has_decay_mult()) {
auto reg = specs.regularizer();
reg.set_coefficient(reg.coefficient() * conf_.regularizer().coefficient());
regularizers_[name] = new Regularizer(reg);
}
if (specs.has_lr_mult()) {
CHECK(learning_rate_multplier_.find(name) == learning_rate_multplier_.end())
<< "Parameter with name = " << name << " has already registered";
learning_rate_multplier_[name] = specs.lr_mult();
}
/*
if (specs.has_lr_generator()) {
LOG(FATAL) << "Not implemented yet";
}
*/
}
void Optimizer::ApplyRegularizerConstraint(int epoch, const string& name,
const Tensor& value, Tensor& grad, int step) {
// TODO(wangwei) need to consider the order of constraint and regularizer
if (regularizers_.find(name) != regularizers_.end()) {
regularizers_.at(name)->Apply(epoch, value, grad, step);
} else if (regularizer_ != nullptr) {
regularizer_->Apply(epoch, value, grad, step);
}
if (constraints_.find(name) != constraints_.end())
constraints_.at(name)->Apply(epoch, value, grad, step);
else if (constraint_ != nullptr)
constraint_->Apply(epoch, value, grad, step);
}
void Optimizer::Apply(int epoch, const string& name, Tensor& grad,
Tensor& value, int step) {
float lr = learning_rate_generator_(step);
Apply(epoch, lr, name, grad, value, step);
}
void Regularizer::Setup(const RegularizerConf& conf) {
type_ = conf.type();
coefficient_ = conf.coefficient();
if (type_ != "L2" && type_ != "l2") {
CHECK(type_ == "NotSet") << "Unknown regularizer type = " << type_;
}
}
void Regularizer::Apply(int epoch, const Tensor& value, Tensor& grad, int step)
{
if (type_ == "L2" || type_ == "l2") {
Axpy(coefficient_, value, &grad);
} else {
CHECK(type_ == "NotSet") << "Unknown regularizer type = " << type_;
}
}
void Regularizer::Apply(int epoch, const vector<Tensor>& values,
const vector<Tensor>& grads, int step) {
LOG(FATAL) << "Not implemented yet";
}
void Constraint::Setup(const ConstraintConf& conf) {
type_ = conf.type();
threshold_ = conf.threshold();
}
void Constraint::Apply(int epoch, const Tensor& value, Tensor& grad, int step) {
// TODO(wangwei) implement L2 and hard constraint
CHECK(type_ == "NotSet") << "Unknown regularizer type = " << type_;
}
void Constraint::Apply(int epoch, const vector<Tensor>& values,
const vector<Tensor>& grads, int step) {
LOG(FATAL) << "Not implemented yet";
}
} // namespace singa