forked from shogun-toolbox/shogun
/
CrossValidationStorage.cpp
243 lines (206 loc) · 6.1 KB
/
CrossValidationStorage.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* BSD 3-Clause License
*
* Copyright (c) 2017, Shogun-Toolbox e.V. <shogun-team@shogun-toolbox.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Written (W) 2017 Giovanni De Toni
*
*/
#include "CrossValidationStorage.h"
#include <shogun/labels/Labels.h>
#include <shogun/machine/Machine.h>
using namespace shogun;
CrossValidationFoldStorage::CrossValidationFoldStorage() : CSGObject()
{
m_current_run_index = 0;
m_current_fold_index = 0;
m_trained_machine = NULL;
m_test_result = NULL;
m_test_true_result = NULL;
}
CrossValidationFoldStorage::~CrossValidationFoldStorage()
{
SG_UNREF(m_test_result);
SG_UNREF(m_test_true_result);
SG_UNREF(m_trained_machine);
}
void CrossValidationFoldStorage::set_run_index(index_t run_index)
{
m_current_run_index = run_index;
}
void CrossValidationFoldStorage::set_fold_index(index_t fold_index)
{
m_current_fold_index = fold_index;
}
void CrossValidationFoldStorage::set_train_indices(SGVector<index_t> indices)
{
m_train_indices = indices;
}
void CrossValidationFoldStorage::set_test_indices(SGVector<index_t> indices)
{
m_test_indices = indices;
}
void CrossValidationFoldStorage::set_trained_machine(CMachine* machine)
{
SG_REF(machine)
SG_UNREF(m_trained_machine)
m_trained_machine = machine;
}
void CrossValidationFoldStorage::set_test_result(CLabels* results)
{
SG_REF(results)
SG_UNREF(m_test_result)
m_test_result = results;
}
void CrossValidationFoldStorage::set_test_true_result(CLabels* results)
{
SG_REF(results)
SG_UNREF(m_test_true_result)
m_test_true_result = results;
}
void CrossValidationFoldStorage::post_update_results()
{
}
void CrossValidationFoldStorage::set_evaluation_result(float64_t result)
{
m_evaluation_result = result;
}
index_t CrossValidationFoldStorage::get_current_run_index() const
{
return m_current_run_index;
}
index_t CrossValidationFoldStorage::get_current_fold_index() const
{
return m_current_fold_index;
}
const SGVector<index_t>& CrossValidationFoldStorage::get_train_indices() const
{
return m_train_indices;
}
const SGVector<index_t>& CrossValidationFoldStorage::get_test_indices() const
{
return m_test_indices;
}
CMachine* CrossValidationFoldStorage::get_trained_machine() const
{
return m_trained_machine;
}
CLabels* CrossValidationFoldStorage::get_test_result() const
{
return m_test_result;
}
CLabels* CrossValidationFoldStorage::get_test_true_result() const
{
return m_test_true_result;
}
float64_t CrossValidationFoldStorage::get_evaluation_result() const
{
return m_evaluation_result;
}
std::vector<CrossValidationFoldStorage*>
CrossValidationStorage::get_folds_results()
{
return m_folds_results;
}
bool CrossValidationFoldStorage::
operator==(const CrossValidationFoldStorage& rhs) const
{
return m_current_run_index == rhs.m_current_run_index &&
m_current_fold_index == rhs.m_current_fold_index &&
// m_train_indices.equals(rhs.m_train_indices) &&
// m_test_indices.equals(rhs.m_test_indices) &&
m_trained_machine->equals(rhs.m_trained_machine) &&
m_test_result->equals(rhs.m_test_result) &&
m_test_true_result->equals(rhs.m_test_true_result) &&
m_evaluation_result == rhs.m_evaluation_result;
}
/** CrossValidationStorage **/
CrossValidationStorage::CrossValidationStorage() : CSGObject()
{
m_num_runs = 0;
m_num_folds = 0;
m_expose_labels = NULL;
}
CrossValidationStorage::~CrossValidationStorage()
{
SG_UNREF(m_expose_labels);
for (auto i : m_folds_results)
SG_UNREF(i)
}
void CrossValidationStorage::set_num_runs(index_t num_runs)
{
m_num_runs = num_runs;
}
void CrossValidationStorage::set_num_folds(index_t num_folds)
{
m_num_folds = num_folds;
}
void CrossValidationStorage::set_expose_labels(CLabels* labels)
{
SG_REF(labels)
SG_UNREF(m_expose_labels)
m_expose_labels = labels;
}
void CrossValidationStorage::post_init()
{
}
index_t CrossValidationStorage::get_num_runs() const
{
return m_num_runs;
}
index_t CrossValidationStorage::get_num_folds() const
{
return m_num_folds;
}
CLabels* CrossValidationStorage::get_expose_labels() const
{
return m_expose_labels;
}
void CrossValidationStorage::append_fold_result(
CrossValidationFoldStorage* result)
{
SG_REF(result);
m_folds_results.push_back(result);
}
bool CrossValidationStorage::operator==(const CrossValidationStorage& rhs) const
{
auto member_vars = m_num_runs == rhs.m_num_runs &&
m_num_folds == rhs.m_num_folds &&
m_expose_labels->equals(rhs.m_expose_labels);
if (!member_vars)
return member_vars;
if (rhs.m_folds_results.size() != m_folds_results.size())
return false;
for (index_t i = 0; i < m_folds_results.size(); i++)
{
if (!(m_folds_results[i] == rhs.m_folds_results[i]))
return false;
}
return member_vars;
}