-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathoptimizer.hpp
69 lines (58 loc) · 1.56 KB
/
optimizer.hpp
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
#pragma once
#include "utils.hpp"
#include "serialization.hpp"
#include <signal.h>
class Optimizer {
private:
ExampleIds all_examples;
Serializable<arma::mat>* w;
Serializable<arma::mat> G, V, Tau;
string algo;
double rho;
double tau;
public:
Optimizer(const pt::ptree& options, Serializable<arma::mat>* w) :
w( w ),
G(w->n_rows, w->n_cols, arma::fill::zeros),
V(w->n_rows, w->n_cols, arma::fill::zeros),
Tau(w->n_rows, w->n_cols, arma::fill::ones),
algo(options.get<string>("algo")),
rho(options.get<double>("rho")),
tau(options.get<double>("tau"))
{
setup();
}
void setup() {
all_examples.clear();
for (arma::uword i=0; i<w->n_cols; ++i)
all_examples.push_back(i);
}
void update(const arma::mat& g) {
update(g, all_examples);
}
void update(const arma::mat& g, const ExampleIds& example_ids) {
if(algo == "ada")
ada_ascent(g, example_ids);
else if (algo == "rmsprop")
rmsprop_ascent(g, example_ids);
else if (algo == "vsgd")
vsgd_ascent(g, example_ids);
else
throw runtime_error("unknown optimization algorithm");
}
void ada_ascent(const arma::mat& g, const ExampleIds& example_ids);
void rmsprop_ascent(const arma::mat& g, const ExampleIds& example_ids);
void vsgd_ascent(const arma::mat& g, const ExampleIds& example_ids);
template<class Archive>
void serialize(Archive& ar, const unsigned int) {
ar & algo;
ar & rho;
ar & tau;
ar & G;
ar & V;
ar & Tau;
ar & w;
setup();
}
Optimizer() : w(NULL), algo("") {}
};