-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
374 lines (286 loc) · 8.63 KB
/
main.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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "SparseBayes.h"
#include "matrix.h"
#include <iostream>
#include <fstream>
typedef std::vector<double> DOUBLE;
typedef std::vector<string> LINE;
static mach_timebase_info_data_t sTimebaseInfo;
double
timeInMilliseconds_main(uint64_t time)
{
if ( sTimebaseInfo.denom == 0 ) {
(void) mach_timebase_info(&sTimebaseInfo);
}
return (time * sTimebaseInfo.numer / sTimebaseInfo.denom)/1000000;
}
void readfile(string filename, std::vector<DOUBLE> &data, std::vector<int> &dataclass){
string deliminator="\t";
std::string line;
int pos;
ifstream myfile (filename.c_str());
if (myfile.is_open()){
while(getline(myfile,line)) /* read a record */
{
LINE ln;
std::vector<double> ln2;
while( (pos = line.find(deliminator)) > 0)
{
string field = line.substr(0,pos);
line = line.substr(pos+1);
double test= strtod(field.c_str(),NULL);
ln2.push_back(test);
}
data.push_back(ln2);
string field = line.substr(0,pos);
line = line.substr(pos+1);
dataclass.push_back(atoi(field.c_str()));
}
myfile.close();
}
else {
std::cout << "Unable to open file";
std::cout << " " << filename;
exit(1);
}
}
void Sigmoid(matrix &A,bool yout){
ofstream myfile_y;
if(yout){
string output2="output_y.txt";
myfile_y.open(output2.c_str(),ios::out);
}
for (int i=0; i<A.rows; i++){
if(yout)
myfile_y << (1.0/(1.0+exp(-A.data[i]))) << endl;
if( (1.0/(1.0+exp(-A.data[i])))>0.5)
A.data[i]=1.0;
else
A.data[i]=0.0;
}
if (yout)
myfile_y.close();
}
void kernelfunction_cauch_test(matrix &BASIS,const std::vector<DOUBLE> &data,std::vector<DOUBLE> &datatest,double basisWidth,std::vector<int> PARAMETERrev){
//Calculate Basis
matrix X2(datatest.size(),PARAMETERrev.size());
matrix Y2(datatest.size(),PARAMETERrev.size());
matrix X(datatest.size(),data[0].size());
matrix Y(PARAMETERrev.size(),data[0].size());
for (int i=0; i<datatest.size(); i++) {
double sumofsquares=0.0;
for(int k=0; k<datatest[i].size(); k++){
sumofsquares+=pow(datatest[i][k],2);
X.data[(i*datatest[i].size())+k]=datatest[i][k];
}
for(int p=0; p<X2.cols; p++){
X2.data[(i*X2.cols)+p]=sumofsquares;
}
}
for (int i=0; i<PARAMETERrev.size(); i++) {
double sumofsquares=0.0;
for (int k=0; k<data[0].size(); k++) {
sumofsquares+=(data[PARAMETERrev[i]][k]*data[PARAMETERrev[i]][k]);
Y.data[i*Y.cols+k]=data[PARAMETERrev[i]][k];
}
for(int p=0; p<X2.rows; p++){
Y2.data[p*X2.cols+i]=sumofsquares;
}
}
matrixprod(X, Y,BASIS, 2,-2.0);
for (int i=0; i<BASIS.rows; i++) {
for (int k=0; k<BASIS.cols; k++) {
BASIS.data[(i*BASIS.cols)+k]=1.0/(1.0+((X2.data[(i*BASIS.cols)+k]+Y2.data[(i*BASIS.cols)+k]+BASIS.data[(i*BASIS.cols)+k])*basisWidth));
}
}
}
void kernelfunction_gauss_test(matrix &BASIS,const std::vector<DOUBLE> &data,std::vector<DOUBLE> &datatest,double basisWidth,std::vector<int> PARAMETERrev){
//Calculate Basis
matrix X2(datatest.size(),PARAMETERrev.size());
matrix Y2(datatest.size(),PARAMETERrev.size());
matrix X(datatest.size(),data[0].size());
matrix Y(PARAMETERrev.size(),data[0].size());
for (int i=0; i<datatest.size(); i++) {
double sumofsquares=0.0;
for(int k=0; k<datatest[i].size(); k++){
sumofsquares+=pow(datatest[i][k],2);
X.data[(i*datatest[i].size())+k]=datatest[i][k];
}
for(int p=0; p<X2.cols; p++){
X2.data[(i*X2.cols)+p]=sumofsquares;
}
}
for (int i=0; i<PARAMETERrev.size(); i++) {
double sumofsquares=0.0;
for (int k=0; k<data[0].size(); k++) {
sumofsquares+=(data[PARAMETERrev[i]][k]*data[PARAMETERrev[i]][k]);
Y.data[i*Y.cols+k]=data[PARAMETERrev[i]][k];
}
for(int p=0; p<X2.rows; p++){
Y2.data[p*X2.cols+i]=sumofsquares;
}
}
matrixprod(X, Y,BASIS, 2,-2.0);
for (int i=0; i<BASIS.rows; i++) {
for (int k=0; k<BASIS.cols; k++) {
BASIS.data[(i*BASIS.cols)+k]=exp((X2.data[(i*BASIS.cols)+k]+Y2.data[(i*BASIS.cols)+k]+BASIS.data[(i*BASIS.cols)+k])*-basisWidth);
}
}
}
void kernelfunction_bin_test(matrix &BASIS,const std::vector<DOUBLE> &data,std::vector<DOUBLE> &datatest,double basisWidth,std::vector<int> PARAMETERrev){
BASIS.reset(datatest.size(), PARAMETERrev.size());
for (int i=0; i<datatest.size(); i++) {
for (int j=0; j<PARAMETERrev.size(); j++) {
double sum=0.0;
for (int k=0; k<data[0].size(); k++) {
sum+=abs(datatest[i][k]-data[PARAMETERrev[j]][k]);
}
BASIS.data[i*BASIS.cols+j]=pow(basisWidth,(data[0].size()-sum))*pow(1-basisWidth,sum);
}
}
}
int main (int argc, char * const argv[]) {
int ItNum=1000;
double MinDeltaLogAlpha=1e-3,MinDeltaLogBeta = 1e-6,AlignmentMax=1-1e-3;
//Reporting on the iterations
int monitor_its=10;
bool PriorityAddition=0,PriorityDeletion=1,BasisAlignmentTest=1;
double basisWidth=0.015625;
//kernel 1- Gaus 2- Cauch 3- Binary
string kernel="Gaus";
string train="";
string test="";
string vals="";
int kern=0;
string runno="";
string output="output_rvm.txt";
bool yout=0;
char ch;
while ((ch = getopt(argc, argv, "k:b:t:v:s:i:r:o:y")) != -1) {
switch (ch) {
case 'k':
kernel=optarg;
cout << "Setting kernel to " << kernel << endl;
break;
case 'b':
basisWidth=atof(optarg);
break;
case 't':
train=optarg;
break;
case 'v':
vals=optarg;
break;
case 's':
test=optarg;
break;
case 'i':
ItNum=atoi(optarg);
break;
case 'o':
output=optarg;
break;
case 'r':
runno.append("c");
runno.append(optarg);
break;
case 'y':
yout=1;
break;
}
}
std::vector<int> PARAMETERrev;
matrix PARAMETERval;
if(kernel=="Gaus")
kern=1;
else if(kernel=="Cauch")
kern=2;
else if (kernel=="Binary")
kern=3;
else {
cout << "Kernel not known. Choose Gaus, Cauch or Binary" << endl;
exit(0);
}
//READ in data
std::vector<DOUBLE> data;
std::vector<int> dataclass;
string s=train;
cout << s << endl;
readfile(s,data,dataclass);
double timeran;
//First number is likelihood - 1) Bernoulli in this case, 0) Gaussian.
timeran=SparseBayes(1,ItNum,monitor_its,MinDeltaLogAlpha,AlignmentMax,data,dataclass, PriorityAddition, PriorityDeletion, BasisAlignmentTest,PARAMETERrev,PARAMETERval,kern,basisWidth);
//READ in data
std::vector<DOUBLE> datatest;
std::vector<int> datatestclass;
s=vals;
readfile(s,datatest,datatestclass);
matrix BASIS;
if (kern==1)
kernelfunction_gauss_test(BASIS, data, datatest, basisWidth, PARAMETERrev);
else if(kern==2)
kernelfunction_cauch_test(BASIS, data, datatest, basisWidth, PARAMETERrev);
else if(kern==3)
kernelfunction_bin_test(BASIS, data, datatest, basisWidth, PARAMETERrev);
matrix y;
matrixprod(BASIS, PARAMETERval, y, 0, 1.0);
Sigmoid(y,yout);
ofstream myfile (output.c_str(),ios::app);
if (myfile.is_open())
{
double TP=0,FP=0,TN=0,FN=0;
for (int i=0; i<datatestclass.size(); i++) {
if (datatestclass[i]==1 and y.data[i]==1)
TP+=1;
else if (datatestclass[i]==0 and y.data[i]==1)
FP+=1;
else if (datatestclass[i]==0 and y.data[i]==0)
TN+=1;
else if (datatestclass[i]==1 and y.data[i]==0)
FN+=1;
}
cout << TP << "\t" << FP << endl;
cout << FN << "\t" << TN << endl;
myfile << "Basiswidth: " << basisWidth << "\tNo Relevance Vectors: " << PARAMETERrev.size() << "\tTime: " << timeran <<endl;
myfile << "Vals:\t" << TP << "\t" << FP << "\t" << FN << "\t" << TN << endl;
datatest.clear();
datatestclass.clear();
s=test;
readfile(s,datatest,datatestclass);
if (kern==1)
kernelfunction_gauss_test(BASIS, data, datatest, basisWidth, PARAMETERrev);
else if(kern==2)
kernelfunction_cauch_test(BASIS, data, datatest, basisWidth, PARAMETERrev);
else if(kern==3)
kernelfunction_bin_test(BASIS, data, datatest, basisWidth, PARAMETERrev);
matrixprod(BASIS, PARAMETERval, y, 0, 1.0);
Sigmoid(y,yout);
TP=0,FP=0,TN=0,FN=0;
for (int i=0; i<datatestclass.size(); i++) {
if (datatestclass[i]==1 and y.data[i]==1)
TP+=1;
else if (datatestclass[i]==0 and y.data[i]==1)
FP+=1;
else if (datatestclass[i]==0 and y.data[i]==0)
TN+=1;
else if (datatestclass[i]==1 and y.data[i]==0)
FN+=1;
}
cout << TP << "\t" << FP << endl;
cout << FN << "\t" << TN << endl;
myfile << "Test:\t" << TP << "\t" << FP << "\t" << FN << "\t" << TN << endl;
myfile.close();
}
else cout << "Unable to open file";
string output3="model";
ofstream myfile_model(output3.c_str(),ios::out);
for(int i=0; i<PARAMETERrev.size(); i++){
myfile_model << PARAMETERrev[i] << "\t";
}
myfile_model << endl;
for(int i=0; i<PARAMETERval.rows; i++){
myfile_model << PARAMETERval.data[i] << "\t";
}
myfile_model <<endl;
myfile_model.close();
return 0;
}