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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
/**
* @file gensvm_base.c
* @author G.J.J. van den Burg
* @date 2016-05-01
* @brief Functions for initializing GenModel, GenData, and GenWork structures
*
* @details
* This file contains functions for initializing, freeing, allocating, and
* reallocating a GenModel instance. It also contains functions for
* initializing and freeing a GenData structure. In addition, default values
* for these structures are defined here (and only here).
*
* @copyright
Copyright 2016, G.J.J. van den Burg.
This file is part of GenSVM.
GenSVM 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.
GenSVM 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 GenSVM. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gensvm_base.h"
/**
* @brief Initialize a GenData structure
*
* @details
* A GenData structure is initialized and default values are set.
* A pointer to the initialized data is returned.
*
* @returns initialized GenData
*
*/
struct GenData *gensvm_init_data(void)
{
struct GenData *data = Malloc(struct GenData, 1);
data->Sigma = NULL;
data->y = NULL;
data->Z = NULL;
data->spZ = NULL;
data->RAW = NULL;
// set default values
data->kerneltype = K_LINEAR;
data->gamma = -1;
data->coef = -1;
data->degree = -1;
return data;
}
/**
* @brief Free allocated GenData struct
*
* @details
* Simply free a previously allocated GenData struct by freeing all its
* components. Note that the data struct itself is also freed here.
*
* @param[in] data GenData struct to free
*
*/
void gensvm_free_data(struct GenData *data)
{
if (data == NULL)
return;
if (data->spZ != NULL)
gensvm_free_sparse(data->spZ);
if (data->Z == data->RAW) {
free(data->Z);
} else {
free(data->Z);
free(data->RAW);
}
free(data->y);
free(data->Sigma);
free(data);
data = NULL;
}
/**
* @brief Initialize a GenModel structure
*
* @details
* A GenModel structure is initialized and the default value for the
* parameters are set. A pointer to the initialized model is returned.
*
* @returns initialized GenModel
*/
struct GenModel *gensvm_init_model(void)
{
struct GenModel *model = Malloc(struct GenModel, 1);
model->n = 0;
model->m = 0;
model->K = 0;
// set default values
model->p = 1.0;
model->lambda = pow(2, -8.0);
model->epsilon = 1e-6;
model->kappa = 0.0;
model->weight_idx = 1;
model->gamma = 1.0;
model->coef = 0.0;
model->degree = 2.0;
model->kerneltype = K_LINEAR;
model->kernel_eigen_cutoff = 1e-8;
model->max_iter = 1000000000;
model->training_error = -1;
model->elapsed_iter = -1;
model->status = -1;
model->seed = -1;
model->V = NULL;
model->Vbar = NULL;
model->U = NULL;
model->UU = NULL;
model->Q = NULL;
model->H = NULL;
model->rho = NULL;
model->data_file = NULL;
return model;
}
/**
* @brief Allocate memory for a GenModel
*
* @details
* This function can be used to allocate the memory needed for a GenModel. All
* arrays in the model are specified and initialized to 0.
*
* @param[in] model GenModel to allocate
*
*/
void gensvm_allocate_model(struct GenModel *model)
{
long n = model->n;
long m = model->m;
long K = model->K;
model->V = Calloc(double, (m+1)*(K-1));
model->Vbar = Calloc(double, (m+1)*(K-1));
model->U = Calloc(double, K*(K-1));
model->UU = Calloc(double, K*K*(K-1));
model->Q = Calloc(double, n*K);
model->H = Calloc(double, n*K);
model->rho = Calloc(double, n);
}
/**
* @brief Reallocate memory for GenModel
*
* @details
* This function can be used to reallocate existing memory for a GenModel,
* upon a change in the model dimensions. This is used in combination with
* kernels.
*
* @param[in] model GenModel to reallocate
* @param[in] n new value of GenModel->n
* @param[in] m new value of GenModel->m
*
*/
void gensvm_reallocate_model(struct GenModel *model, long n, long m)
{
long K = model->K;
if (model->n == n && model->m == m)
return;
if (model->n != n) {
model->Q = Realloc(model->Q, double, n*K);
Memset(model->Q, double, n*K);
model->H = Realloc(model->H, double, n*K);
Memset(model->H, double, n*K);
model->rho = Realloc(model->rho, double, n);
Memset(model->rho, double, n);
model->n = n;
}
if (model->m != m) {
model->V = Realloc(model->V, double, (m+1)*(K-1));
Memset(model->V, double, (m+1)*(K-1));
model->Vbar = Realloc(model->Vbar, double, (m+1)*(K-1));
Memset(model->Vbar, double, (m+1)*(K-1));
model->m = m;
}
}
/**
* @brief Free allocated GenModel struct
*
* @details
* Simply free a previously allocated GenModel by freeing all its component
* arrays. Note that the model struct itself is also freed here.
*
* @param[in] model GenModel to free
*
*/
void gensvm_free_model(struct GenModel *model)
{
if (model == NULL)
return;
free(model->V);
free(model->Vbar);
free(model->U);
free(model->UU);
free(model->Q);
free(model->H);
free(model->rho);
free(model->data_file);
free(model);
model = NULL;
}
/**
* @brief Initialize the workspace structure
*
* @details
* During the computations in gensvm_get_update(), a number of matrices need
* to be allocated which are used to store intermediate results. To avoid
* reallocating these matrices at every call to gensvm_get_update(), we create
* here a workspace with these matrices. This workspace is created once by
* gensvm_optimize(), and is passed to gensvm_get_update() and
* gensvm_get_loss(). See the documentation of the GenWork structure for
* information on each allocated field.
*
* @param[in] model a GenModel with the dimensionality of the problem
* @returns an allocated GenWork instance
*
*/
struct GenWork *gensvm_init_work(struct GenModel *model)
{
long n = model->n;
long m = model->m;
long K = model->K;
struct GenWork *work = Malloc(struct GenWork, 1);
work->n = n;
work->m = m;
work->K = K;
work->LZ = Calloc(double, n*(m+1));
work->ZB = Calloc(double, (m+1)*(K-1)),
work->ZBc = Calloc(double, (m+1)*(K-1)),
work->ZAZ = Calloc(double, (m+1)*(m+1)),
work->tmpZAZ = Calloc(double, (m+1)*(m+1)),
work->ZV = Calloc(double, n*(K-1));
work->beta = Calloc(double, K-1);
return work;
}
/**
* @brief Free an allocated GenWork instance
*
* @details
* This function simply frees every matrix allocated for in the GenWork
* workspace.
*
* @param[in] work a pointer to an allocated GenWork instance
*
*/
void gensvm_free_work(struct GenWork *work)
{
free(work->LZ);
free(work->ZB);
free(work->ZBc);
free(work->ZAZ);
free(work->tmpZAZ);
free(work->ZV);
free(work->beta);
free(work);
work = NULL;
}
/**
* @brief Reset all matrices of a GenWork instance
*
* @details
* In the gensvm_get_update() function, it is expected for some matrices that
* all their entries are initialized to 0. This function sets the memory for
* each of the matrices in the GenWork workspace to 0 to facilitate this.
*
* @param[in,out] work an initialized GenWork instance. On exit, the
* matrices of the GenWork instance are all
* initialized to 0.
*/
void gensvm_reset_work(struct GenWork *work)
{
long n = work->n;
long m = work->m;
long K = work->K;
Memset(work->LZ, double, n*(m+1));
Memset(work->ZB, double, (m+1)*(K-1)),
Memset(work->ZBc, double, (m+1)*(K-1)),
Memset(work->ZAZ, double, (m+1)*(m+1)),
Memset(work->tmpZAZ, double, (m+1)*(m+1)),
Memset(work->ZV, double, n*(K-1));
Memset(work->beta, double, K-1);
}
|