-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnsemble.cpp
77 lines (63 loc) · 2.41 KB
/
Ensemble.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "../headers/Ensemble.hpp"
#include "../headers/SymmetryExpander.hpp"
#include <numeric>
Ensemble::Ensemble(double lr) : learning_rate(lr) {
constexpr static int m = 15;
/* Prepare a shape:
std::tuple<int, int> shape[4] = {{0, 0},
{0, 1},
{1, 1},
{1, 0}};
* Add a standard tuple:
tuples.emplace_back(new NTuple<4>(m, shape));
* Or one with symmetry expansion:
for (auto &&t : SymmetryExpander::expand<4>(m, shape))
tuples.push_back(std::move(t));
*/
// Cyfra '6'
std::tuple<int, int> shape[6] = {{1, 1},
{1, 0},
{0, 0},
{0, 1},
{0, 2},
{0, 3}};
for (auto &&t : SymmetryExpander::expand<6>(m, shape))
tuples.push_back(std::move(t));
// Cyfra '6', przesunięta
std::tuple<int, int> other_shape[6] = {{2, 1},
{2, 0},
{1, 0},
{1, 1},
{1, 2},
{1, 3}};
for (auto &&t : SymmetryExpander::expand<6>(m, other_shape))
tuples.push_back(std::move(t));
}
double Ensemble::apply(const Board &board) const {
return std::accumulate(tuples.cbegin(), tuples.cend(), 0.0, [&board](double acc, const auto &tuple) {
return acc + tuple->apply(board);
});
}
void Ensemble::update(const Board &board, double error) {
const double delta = learning_rate * error;
std::for_each(tuples.begin(), tuples.end(), [&](auto &t) {
t->update(board, delta);
});
}
void Ensemble::save_model(const std::string &path) const {
for (size_t i = 0; i < tuples.size(); i++) {
tuples[i]->save_model(path + std::to_string(i));
}
}
void Ensemble::load_model(const std::string &path) {
for (size_t i = 0; i < tuples.size(); i++) {
tuples[i]->load_model(path + std::to_string(i));
}
}
void Ensemble::adapt_lr(int epoch) {
// DEFINE ADAPTIVE LR HERE
if (epoch == 400000) {
learning_rate = 0.001;
std::cout << "\nChanging learning rate to " << learning_rate << std::endl;
}
}