forked from blei-lab/deep-exponential-families
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimizer.cpp
63 lines (57 loc) · 1.59 KB
/
optimizer.cpp
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
#include "optimizer.hpp"
void Optimizer::ada_ascent(const arma::mat& g, const ExampleIds& example_ids) {
arma::uword j0 = 0;
for (auto j : example_ids) {
G.col(j) += g.col(j0) % g.col(j0);
++j0;
}
j0 = 0;
for (auto j : example_ids) {
for (arma::uword i=0; i<G.n_rows; ++i) {
if (G(i,j) > 0)
(*w)(i,j) += rho / sqrt(G(i,j)) * g(i,j0);
}
++j0;
}
}
void Optimizer::rmsprop_ascent(const arma::mat& g, const ExampleIds& example_ids) {
auto inv_tau = 1.0/tau;
arma::uword j0 = 0;
for (auto j : example_ids) {
for(arma::uword i=0; i<G.n_rows; ++i) {
auto G0 = G(i,j);
if (G0 == 0)
G(i, j) = g(i, j0) * g(i, j0);
else
G(i, j) = (1.0-inv_tau) * G0 + inv_tau * g(i, j0) * g(i, j0);
if (G(i,j) > 0)
(*w)(i,j) += rho / sqrt(G(i,j)) * g(i,j0);
}
++j0;
}
}
void Optimizer::vsgd_ascent(const arma::mat& g, const ExampleIds& example_ids) {
arma::uword j0 = 0;
for (auto j : example_ids) {
for(arma::uword i=0; i<G.n_rows; ++i) {
auto G0 = G(i,j);
auto V0 = V(i,j);
auto inv_tau = 1.0/Tau(i,j);
// update G & V
if (G0 == 0) {
G(i, j) = g(i, j0) * g(i, j0);
V(i, j) = g(i, j0);
}
else {
G(i, j) = (1.0-inv_tau) * G0 + inv_tau * g(i, j0) * g(i, j0);
V(i, j) = (1.0-inv_tau) * V0 + inv_tau * g(i, j0);
}
// update w and Tau
if (G(i,j) > 0) {
(*w)(i,j) += rho * abs(V(i,j)) / G(i,j) * g(i,j0);
Tau(i,j) = max((1.0-V(i,j)*V(i,j)/G(i,j)) * Tau(i,j) + 1.0, 3.0);
}
}
++j0;
}
}