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
|
#include "util.h"
/*
Read the data from the data_file. The data matrix X is augmented
with a column of ones, to get the matrix Z.
*/
void read_data(struct Data *dataset, struct Model *model, char *data_file)
{
FILE *fid;
long i, j;
long n, m; // dimensions of data
long nr = 0; // used to check consistency of data
double value;
long K = 0;
long min_y = 1000;
char buf[MAX_LINE_LENGTH];
if ((fid = fopen(data_file, "r")) == NULL) {
printf("\nERROR: datafile %s could not be opened.\n",
data_file);
exit(0);
}
// Read data dimensions
nr += fscanf(fid, "%ld", &n);
nr += fscanf(fid, "%ld", &m);
// Allocate memory
dataset->Z = Malloc(double, n*(m+1));
// Read first line of data
for (j=1; j<m+1; j++) {
nr += fscanf(fid, "%lf", &value);
matrix_set(dataset->Z, n, 0, j, value);
}
// Check if there is a label at the end of the line
if (fgets(buf, MAX_LINE_LENGTH, fid) == NULL) {
printf("ERROR: No label found on first line.\n");
exit(1);
}
if (sscanf(buf, "%lf", &value) > 0) {
dataset->y = Malloc(long, n);
dataset->y[0] = value;
} else if (dataset->y != NULL) {
free(dataset->y);
dataset->y = NULL;
}
// Read the rest of the file
for (i=1; i<n; i++) {
for (j=1; j<m+1; j++) {
nr += fscanf(fid, "%lf", &value);
matrix_set(dataset->Z, m+1, i, j, value);
}
if (dataset->y != NULL) {
nr += fscanf(fid, "%lf", &value);
dataset->y[i] = (long) value;
K = maximum(K, value);
min_y = minimum(min_y, value);
}
}
fclose(fid);
// Correct labels: must be in [1, K]
if (min_y == 0) {
for (i=0; i<n; i++)
dataset->y[i]++;
} else if (min_y < 0 ) {
printf("ERROR: wrong class labels in %s, minimum value is: %ld\n",
data_file, min_y);
exit(0);
}
if (nr < n * m) {
printf("ERROR: not enough data found in %s\n", data_file);
exit(0);
}
// Set the column of ones
for (i=0; i<n; i++)
matrix_set(dataset->Z, m+1, i, 0, 1.0);
dataset->n = n;
dataset->m = m;
dataset->K = K;
model->n = n;
model->m = m;
model->K = K;
info("Succesfully read data file: %s\n", data_file);
}
int check_argv(int argc, char **argv, char *str)
{
int i;
int arg_str = 0;
for (i=1; i<argc; i++)
if (strstr(argv[i], str) != NULL) {
arg_str = i;
break;
}
return arg_str;
}
int check_argv_eq(int argc, char **argv, char *str)
{
int i;
int arg_str = 0;
for (i=1; i<argc; i++)
if (strcmp(argv[i], str) == 0) {
arg_str = i;
break;
}
return arg_str;
}
static void print_string_stdout(const char *s)
{
fputs(s, stdout);
fflush(stdout);
}
static void (*print_string) (const char *) = &print_string_stdout;
void set_print_string_function(void (*print_func)(const char *))
{
if (print_func == NULL)
print_string = &print_string_stdout;
else
print_string = print_func;
}
void info(const char *fmt,...)
{
char buf[BUFSIZ];
va_list ap;
va_start(ap,fmt);
vsprintf(buf,fmt,ap);
va_end(ap);
(*print_string)(buf);
}
double rnd()
{
return (double) rand()/0x7FFFFFFF;
}
/*
Set a matrix element using ROW Major order. i denotes row,
j denotes column.
*/
void matrix_set(double *M, long cols, long i, long j, double val)
{
M[i*cols+j] = val;
}
/*
Get a matrix element using ROW Major order. i denotes row,
j denotes column.
*/
double matrix_get(double *M, long cols, long i, long j)
{
return M[i*cols+j];
}
/*
Add to an existing matrix element. Row-Major order is used.
*/
void matrix_add(double *M, long cols, long i, long j, double val)
{
M[i*cols+j] += val;
}
/*
Multiply existing matrix element. Row-Major order is used.
*/
void matrix_mult(double *M, long cols, long i, long j, double val)
{
M[i*cols+j] *= val;
}
/*
Set a matrix element of a 3D matrix in ROW major order.
N2 and N3 are the second and third dimension respectively
and i, j, k are the indices of the first, second and third
dimensions respectively.
*/
void matrix3_set(double *M, long N2, long N3, long i, long j, long k, double val)
{
M[k+N3*(j+N2*i)] = val;
}
/*
Get a matrix element of a 3D matrix in ROW major order.
N2 and N3 are the second and third dimension respectively, and
i, j and k are the indices of the first, second and third
dimension of the requested element respectively.
*/
double matrix3_get(double *M, long N2, long N3, long i, long j, long k)
{
return M[k+N3*(j+N2*i)];
}
void allocate_model(struct Model *model)
{
long n = model->n;
long m = model->m;
long K = model->K;
model->W = Calloc(double, m*(K-1));
if (model->W == NULL) {
fprintf(stderr, "Failed to allocate memory for W.\n");
exit(1);
}
model->t = Calloc(double, K-1);
if (model->t == NULL) {
fprintf(stderr, "Failed to allocate memory for t.\n");
exit(1);
}
model->V = Calloc(double, (m+1)*(K-1));
if (model->V == NULL) {
fprintf(stderr, "Failed to allocate memory for V.\n");
exit(1);
}
model->Vbar = Calloc(double, (m+1)*(K-1));
if (model->Vbar == NULL) {
fprintf(stderr, "Failed to allocate memory for Vbar.\n");
exit(1);
}
model->U = Calloc(double, K*(K-1));
if (model->U == NULL) {
fprintf(stderr, "Failed to allocate memory for U.\n");
exit(1);
}
model->UU = Calloc(double, n*K*(K-1));
if (model->UU == NULL) {
fprintf(stderr, "Failed to allocate memory for UU.\n");
exit(1);
}
model->Q = Calloc(double, n*K);
if (model->Q == NULL) {
fprintf(stderr, "Failed to allocate memory for Q.\n");
exit(1);
}
model->H = Calloc(double, n*K);
if (model->H == NULL) {
fprintf(stderr, "Failed to allocate memory for H.\n");
exit(1);
}
model->R = Calloc(double, n*K);
if (model->R == NULL) {
fprintf(stderr, "Failed to allocate memory for R.\n");
exit(1);
}
model->rho = Calloc(double, n);
if (model->rho == NULL) {
fprintf(stderr, "Failed to allocate memory for rho.\n");
exit(1);
}
}
void free_model(struct Model *model)
{
free(model->W);
free(model->t);
free(model->V);
free(model->Vbar);
free(model->U);
free(model->UU);
free(model->Q);
free(model->H);
free(model->rho);
free(model->R);
free(model);
}
void free_data(struct Data *data)
{
free(data->Z);
free(data->y);
free(data);
}
void print_matrix(double *M, long rows, long cols)
{
long i, j;
for (i=0; i<rows; i++) {
for (j=0; j<cols; j++) {
info("%8.8f ", matrix_get(M, cols, i, j));
}
info("\n");
}
info("\n");
}
|