[r80]: src / agf_precondition.cc Maximize Restore History

Download this file

agf_precondition.cc    344 lines (296 with data), 10.0 kB

  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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//Most common pre-processing operation--normalization, SVD, removing a feature
//--are 1. linear operations, 2. require the test data to be multiplied by
//a "pre-condtioning" matrix
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <gsl/gsl_linalg.h>
#include <assert.h>
#include "full_util.h"
#include "agf_lib.h"
#define WC_DEFAULT 20.
using namespace std;
using namespace libagf;
using namespace libpetey;
int main(int argc, char *argv[]) {
char *infile=NULL; //training data features only
char *resultfile=NULL; //transformed training data
FILE *fs;
FILE *diagfs; //print diagnostics to this file stream
nel_ta ntrain; //number of training data points
dim_ta nvar; //number of variables in original data
dim_ta nvar2; //number of variables after feature selection
dim_ta nvar3; //number of variables after SVD
real_a **train; //training data vectors
real_a **result; //the result
real_a **mat; //transformation "conditioning" matrix
cls_ta *cls=NULL;
int exit_value;
//components of the transformation matrix:
gsl_matrix *v;
gsl_vector *s;
real_a *std, *ave;
cls_ta *ind;
agf_command_opts opt_args;
exit_value=0;
exit_value=0;
exit_value=agf_parse_command_opts(argc, argv, "a:Anc:S:FNO", &opt_args);
if (exit_value==FATAL_COMMAND_OPTION_PARSE_ERROR) return exit_value;
//parse the command line arguments:
if (argc < 1) {
printf("\n");
printf("Syntax: agf_precondition -a normfile [-n] [-S nsv] [-F] [input output] \n");
printf(" [ind1 [ind2 [ind3...]]]\n");
printf("\n");
printf("arguments:\n");
printf(" normfile input/output transformation matrix\n");
printf(" input binary input file containing vector data\n");
printf(" output binary output file containing vector data\n");
printf(" indN feature selection index\n");
printf("\n");
printf("options (in order of execution):\n");
printf(" -A operate on ASCII files from/to stdin/stdout\n");
printf(" -F select features\n");
printf(" -n normalize with standard deviations\n");
printf(" -S svd singular value decomposition (SVD); keep nsv singular values\n");
//printf(" -N take data from stdin, write to stdout\n");
printf("\n");
return INSUFFICIENT_COMMAND_ARGS;
}
if (opt_args.normfile==NULL) {
fprintf(stderr, "agf_precondition: must specify normalization file with -a\n");
exit(INSUFFICIENT_COMMAND_ARGS);
}
diagfs=stderr;
//if there are no arguments, we read from stdin
//ascii data is always read from stdin and written to stdout
if (argc==0 || opt_args.asciiflag) {
fs=stdin;
} else {
infile=argv[0];
fs=fopen(infile, "r");
}
if (opt_args.asciiflag) {
int readflag=0;
ntrain=read_lvq(fs, train, cls, nvar, readflag);
if (ntrain<=0) {
fprintf(stderr, "agf_precondtion: Read error\n");
exit(FILE_READ_ERROR);
}
} else {
train=NULL;
cls=NULL;
nel_ta nv1;
//read in the training data:
train=read_matrix<real_a, nel_ta>(fs, ntrain, nv1);
nvar=nv1;
//really, why the f* have I duplicated this function??
//train=read_vecfile(infile, ntrain, nvar);
fprintf(diagfs, "%d %d-dimensional training vectors found in file: %s\n", ntrain, nvar, infile);
}
if (opt_args.Nflag==0) fclose(fs);
nvar2=nvar;
ind=new dim_ta[nvar];
if (opt_args.selectflag) {
real_a **result2;
if (opt_args.asciiflag) {
nvar2=argc;
} else if (opt_args.Oflag) {
nvar2=argc-1;
} else {
nvar2=argc-2;
}
if (nvar2 <= 0) {
fprintf(stderr, "agf_precondition: no selection terms in argument list\n");
exit(PARAMETER_OUT_OF_RANGE);
}
for (dim_ta i=0; i<nvar2; i++) {
if (sscanf(argv[i+argc-nvar2], "%d", ind+i)!=1) {
fprintf(stderr, "agf_precondition: unable to read seleciton index %d\n", i);
exit(FATAL_COMMAND_OPTION_PARSE_ERROR);
}
if (ind[i]>=nvar || ind[i]<0) {
fprintf(stderr, "agf_precondition: index out of range\n");
exit(PARAMETER_OUT_OF_RANGE);
}
}
delete_matrix(result);
result2=allocate_matrix<real_a, int32_t>(ntrain, nvar2);
for (nel_ta i=0; i<ntrain; i++) {
for (dim_ta j=0; j<nvar2; j++) {
result2[i][j]=train[i][ind[j]];
}
}
delete_matrix(result);
train=result2;
} else {
nvar2=nvar;
for (dim_ta i=0; i<nvar2; i++) ind[i]=i;
}
//calculate the averages and standard deviations:
std=new real_a[nvar];
ave=new real_a[nvar];
if (opt_args.normflag) {
calc_norm(train, nvar2, ntrain, ave, std);
for (nel_ta i=0; i<ntrain; i++) {
for (dim_ta j=0; j<nvar2; j++) {
train[i][j]=(train[i][j]-ave[j])/std[j];
}
}
//if (argc>=2) print_stats(diagfs, ave, std, nvar2);
print_stats(diagfs, ave, std, nvar2);
} else {
//(kind of a stupid way of doing it... oh well)
for (dim_ta i=0; i<nvar2; i++) {
std[i]=1;
ave[i]=0;
}
}
if (opt_args.svd>0) {
gsl_matrix *u;
gsl_vector *work;
nel_ta k;
//always remove averages (since they are wasted...)
if (opt_args.normflag==0) {
real_a dum[nvar2];
calc_norm(train, nvar2, ntrain, ave, dum);
for (nel_ta i=0; i<ntrain; i++) {
for (dim_ta j=0; j<nvar2; j++) {
train[i][j]=train[i][j]-ave[j];
}
}
}
if (ntrain>nvar2) {
u=gsl_matrix_alloc(ntrain, nvar2);
for (nel_ta i=0; i<ntrain; i++) {
for (dim_ta j=0; j<nvar2; j++) {
gsl_matrix_set(u, i, j, train[i][j]);
}
}
k=nvar2;
} else {
u=gsl_matrix_alloc(nvar2, ntrain);
for (nel_ta i=0; i<ntrain; i++) {
for (dim_ta j=0; j<nvar2; j++) {
gsl_matrix_set(u, j, i, train[i][j]);
}
}
k=ntrain;
}
v=gsl_matrix_alloc(k, k);
s=gsl_vector_alloc(k);
work=gsl_vector_alloc(k);
fprintf(diagfs, "agf_precondition: calling GSL SVD subroutine...\n");
//if (argc>=2) fprintf(diagfs, "agf_precondition: calling GSL SVD subroutine...\n");
//gsl_linalg_SV_decomp(u, v, s, work);
gsl_linalg_SV_decomp_jacobi (u, v, s);
if (opt_args.svd>0 && opt_args.svd<k) {
nvar3=opt_args.svd;
} else {
nvar3=k;
}
result=allocate_matrix<real_a, nel_ta>(ntrain, nvar3);
if (ntrain>nvar2) {
for (nel_ta i=0; i<ntrain; i++) {
for (dim_ta j=0; j<nvar3; j++) {
result[i][j]=gsl_matrix_get(u, i, j)*gsl_vector_get(s, j);
}
}
} else {
for (nel_ta i=0; i<ntrain; i++) {
for (nel_ta j=0; j<nvar3; j++) {
result[i][j]=gsl_matrix_get(v, j, i)*gsl_vector_get(s, j);
}
}
gsl_matrix_free(v);
v=gsl_matrix_alloc(ntrain, nvar2);
gsl_matrix_transpose_memcpy(v, u);
}
/*
if (argc>=2) {
for (nel_ta i=0; i<k; i++) printf("%g\n", gsl_vector_get(s, i));
for (nel_ta i=0; i<k; i++) {
for (nel_ta j=0; j<k; j++) {
printf("%12.6g ", gsl_matrix_get(v, i, j));
}
printf("\n");
}
}
*/
gsl_matrix_free(u);
gsl_vector_free(work);
} else {
v=gsl_matrix_alloc(nvar2, nvar2);
gsl_matrix_set_identity(v);
s=gsl_vector_alloc(nvar2);
gsl_vector_set_all(s, 1);
result=copy_matrix(train, ntrain, nvar2);
nvar3=nvar2;
}
if (opt_args.selectflag==0 && opt_args.normflag==0 && opt_args.svd<=0) {
if (opt_args.normfile!=NULL) {
//read in the normalization data and use it transform the data
//rather than printing it out:
mat=read_stats2(opt_args.normfile, ave, nvar2, nvar3);
//mat=read_matrix<real_a, nel_ta>(fs, nvar2, nvar3);
assert(nvar==nvar2);
for (nel_ta i=0; i<ntrain; i++)
for (dim_ta j=0; j<nvar; j++) train[i][j]-=ave[j];
result=matrix_mult(train, mat, ntrain, nvar, nvar3);
} else {
//congrats, you just wasted some compute cycles...
result=copy_matrix<real_a, int32_t>(train, ntrain, nvar);
nvar3=nvar;
}
} else {
mat=zero_matrix<real_a, nel_ta>(nvar, nvar3+1);
for (dim_ta i=0; i<nvar2; i++) {
for (dim_ta j=0; j<nvar3; j++) {
mat[ind[i]][j]=gsl_matrix_get(v, i, j)/std[i];
}
mat[ind[i]][nvar3]=ave[i]; //store averages to right of matrix
}
}
if (opt_args.asciiflag || opt_args.Oflag || argc<2) {
fs=stdout;
} else {
resultfile=argv[1];
fs=fopen(resultfile, "w");
if (fs==NULL) {
fprintf(stderr, "Unable to open file for writing: %s\n", resultfile);
return UNABLE_TO_OPEN_FILE_FOR_WRITING;
}
}
//write the results to a file:
if (opt_args.asciiflag) {
fprintf(fs, "%d\n", nvar3);
for (nel_ta i=0; i<ntrain; i++) {
for (dim_ta j=0; j<nvar3; j++) fprintf(fs, "%g ", result[i][j]);
fprintf(fs, "%d\n", cls[i]);
}
} else {
fwrite(&nvar3, sizeof(nvar3), 1, fs);
fwrite(result[0], sizeof(real_a), nvar3*ntrain, fs);
}
if (opt_args.Oflag==0 && argc>=2) fclose(fs);
if (opt_args.normfile!=NULL) {
fs=fopen(opt_args.normfile, "w");
if (fs==NULL) {
fprintf(stderr, "Unable to open file for writing: %s\n", opt_args.normfile);
return UNABLE_TO_OPEN_FILE_FOR_WRITING;
}
nvar3++;
fwrite(&nvar3, sizeof(nvar3), 1, fs);
fwrite(mat[0], sizeof(real_a), nvar3*nvar, fs);
fclose(fs);
}
//clean up:
delete [] train[0];
delete [] train;
if (cls!=NULL) delete [] cls;
delete_matrix(mat);
delete [] std;
delete [] ave;
if (opt_args.normfile!=NULL) delete [] opt_args.normfile;
return exit_value;
}