-
Notifications
You must be signed in to change notification settings - Fork 3
/
baselearner_track.cpp
201 lines (162 loc) · 7.51 KB
/
baselearner_track.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
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
// ========================================================================== //
// ___. __ //
// ____ ____ _____ ______\_ |__ ____ ____ _______/ |_ //
// _/ ___\/ _ \ / \\____ \| __ \ / _ \ / _ \/ ___/\ __\ //
// \ \__( <_> ) Y Y \ |_> > \_\ ( <_> | <_> )___ \ | | //
// \___ >____/|__|_| / __/|___ /\____/ \____/____ > |__| //
// \/ \/|__| \/ \/ //
// //
// ========================================================================== //
//
// Compboost is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Compboost is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Compboost. If not, see <http://www.gnu.org/licenses/>.
//
// This file contains:
// -------------------
//
// Implementation of "BaselearnerTrack".
//
// Written by:
// -----------
//
// Daniel Schalk
// Institut für Statistik
// Ludwig-Maximilians-Universität München
// Ludwigstraße 33
// D-80539 München
//
// https://www.compstat.statistik.uni-muenchen.de
//
// =========================================================================== #
#include "baselearner_track.h"
namespace blearnertrack
{
// Just an empty constructor:
BaselearnerTrack::BaselearnerTrack () {};
BaselearnerTrack::BaselearnerTrack (double learning_rate) : learning_rate ( learning_rate ) {};
// Insert a baselearner to the vector. We also want to add up the parameter
// in there to get an estimator in the end:
void BaselearnerTrack::InsertBaselearner (blearner::Baselearner* blearner)
{
// Insert new baselearner:
blearner_vector.push_back(blearner);
std::string insert_id = blearner->GetDataIdentifier() + ": " + blearner->GetBaselearnerType();
// Check if the baselearner is the first one. If so, the parameter
// has to be instantiated with a zero matrix:
std::map<std::string, arma::mat>::iterator it = my_parameter_map.find(insert_id);
// Prune parameter by multiplying it with the learning rate:
arma::mat parameter_temp = learning_rate * blearner->GetParameter();
// Check if this is the first parameter entry:
if (it == my_parameter_map.end()) {
// If this is the first entry, initialize it with zeros:
arma::mat init_parameter(parameter_temp.n_rows, parameter_temp.n_cols, arma::fill::zeros);
my_parameter_map.insert(std::pair<std::string, arma::mat>(insert_id, init_parameter));
}
// Accumulating parameter. If there is a nan, then this will be ignored and
// the non nan entries are added up:
// arma::mat parameter_insert = parameter_temp + my_parameter_map.find(blearner->GetBaselearnerType())->second;
// my_parameter_map.insert(std::pair<std::string, arma::mat>(blearner->GetBaselearnerType(), parameter_insert));
my_parameter_map[ insert_id ] = parameter_temp + my_parameter_map.find(insert_id)->second;
}
// Get the vector of baselearner:
std::vector<blearner::Baselearner*> BaselearnerTrack::GetBaselearnerVector ()
{
return blearner_vector;
}
// Get parameter map:
std::map<std::string, arma::mat> BaselearnerTrack::GetParameterMap ()
{
return my_parameter_map;
}
// Clear baselearner vector:
void BaselearnerTrack::ClearBaselearnerVector ()
{
// Basically the same as the destructor. But, without deleting the underlying
// BaselearnerTrack object.
for (unsigned int i = 0; i< blearner_vector.size(); i++)
{
delete blearner_vector[i];
}
blearner_vector.clear();
}
// Get estimated parameter for specific iteration:
std::map<std::string, arma::mat> BaselearnerTrack::GetEstimatedParameterForIteration (unsigned int k)
{
// Create new parameter map:
std::map<std::string, arma::mat> my_new_parameter_map;
if (k <= blearner_vector.size()) {
for (unsigned int i = 0; i < k; i++) {
std::string insert_id = blearner_vector[i]->GetDataIdentifier() + ": " + blearner_vector[i]->GetBaselearnerType();
// Check if the baselearner is the first one. If so, the parameter
// has to be instantiated with a zero matrix:
std::map<std::string, arma::mat>::iterator it = my_new_parameter_map.find(insert_id);
// Prune parameter by multiplying it with the learning rate:
arma::mat parameter_temp = learning_rate * blearner_vector[i]->GetParameter();
// Check if this is the first parameter entry:
if (it == my_new_parameter_map.end()) {
// If this is the first entry, initialize it with zeros:
arma::mat init_parameter(parameter_temp.n_rows, parameter_temp.n_cols, arma::fill::zeros);
my_new_parameter_map.insert(std::pair<std::string, arma::mat>(insert_id, init_parameter));
}
// Accumulating parameter. If there is a nan, then this will be ignored and
// the non nan entries are added up:
// arma::mat parameter_insert = parameter_temp + my_parameter_map.find(blearner->GetBaselearnerType())->second;
// my_parameter_map.insert(std::pair<std::string, arma::mat>(blearner->GetBaselearnerType(), parameter_insert));
my_new_parameter_map[ insert_id ] = parameter_temp + my_new_parameter_map.find(insert_id)->second;
}
}
return my_new_parameter_map;
}
// Create parameter matrix:
std::pair<std::vector<std::string>, arma::mat> BaselearnerTrack::GetParameterMatrix ()
{
// Instantiate list to iterate:
std::map<std::string, arma::mat> my_new_parameter_map = my_parameter_map;
unsigned int cols = 0;
// Set all parameter to zero in new map:
for (auto& it : my_new_parameter_map) {
arma::mat init_parameter (it.second.n_rows, it.second.n_cols, arma::fill::zeros);
my_new_parameter_map[ it.first ] = init_parameter;
cols += it.second.n_cols;
}
// Initialize matrix:
arma::mat parameters (blearner_vector.size(), cols, arma::fill::zeros);
for (unsigned int i = 0; i < blearner_vector.size(); i++) {
std::string insert_id = blearner_vector[i]->GetDataIdentifier() + ": " + blearner_vector[i]->GetBaselearnerType();
// Prune parameter by multiplying it with the learning rate:
arma::mat parameter_temp = learning_rate * blearner_vector[i]->GetParameter();
// Accumulating parameter. If there is a nan, then this will be ignored and
// the non nan entries are added up:
my_new_parameter_map[ insert_id ] = parameter_temp + my_new_parameter_map.find(insert_id)->second;
arma::mat param_insert;
for (auto& it : my_new_parameter_map) {
param_insert = arma::join_rows(param_insert, it.second);
}
parameters.row(i) = param_insert;
}
std::pair<std::vector<std::string>, arma::mat> out_pair;
for (auto& it : my_new_parameter_map) {
out_pair.first.push_back(it.first);
}
out_pair.second = parameters;
return out_pair;
}
// Destructor:
BaselearnerTrack::~BaselearnerTrack ()
{
// std::cout << "Call BaselearnerTrack Destructor" << std::endl;
for (unsigned int i = 0; i< blearner_vector.size(); i++)
{
delete blearner_vector[i];
}
blearner_vector.clear();
}
} // blearnertrack