-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtestGp.cpp
186 lines (169 loc) · 5 KB
/
testGp.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
#include "CKern.h"
#include "CMatrix.h"
#include "CGp.h"
#include "CClctrl.h"
int testGaussian(string type);
class CClgptest : public CClctrl
{
public:
CClgptest(int arc, char** arv) : CClctrl(arc, arv){}
void helpInfo(){}
void helpHeader(){}
};
int main(int argc, char* argv[])
{
CClgptest command(argc, argv);
int fail = 0;
try
{
//fail += testGaussian("ftc");
fail += testGaussian("dtc");
//fail += testGaussian("fitc");
//fail += testGaussian("pitc");
command.exitNormal();
}
catch(ndlexceptions::Error& err)
{
command.exitError(err.getMessage());
}
catch(std::bad_alloc&)
{
command.exitError("Out of memory.");
}
catch(std::exception& err)
{
std::string what(err.what());
command.exitError("Unhandled exception: " + what);
}
}
int testGaussian(string type)
{
string fileName = "matfiles" + ndlstrutil::dirSep() + "testGp" + type + ".mat";
int fail = 0;
CMatrix X;
X.readMatlabFile(fileName.c_str(), "X");
CMatrix y;
y.readMatlabFile(fileName.c_str(), "y");
CMatrix actSetMat;
actSetMat.readMatlabFile(fileName.c_str(), "numActive");
int numActive = (int)actSetMat.getVal(0);
CMatrix apprxTypeMat;
apprxTypeMat.readMatlabFile(fileName.c_str(), "approxInt");
int approxType = (int)apprxTypeMat.getVal(0);
CMatrix scale;
scale.readMatlabFile(fileName.c_str(), "scale");
CMatrix bias;
bias.readMatlabFile(fileName.c_str(), "bias");
CCmpndKern kernInit(X);
kernInit.addKern(new CRbfKern(X));
kernInit.addKern(new CLinKern(X));
kernInit.addKern(new CBiasKern(X));
kernInit.addKern(new CWhiteKern(X));
CGaussianNoise noiseInit(&y);
for(int i=0; i<noiseInit.getOutputDim(); i++)
{
noiseInit.setBiasVal(0.0, i);
}
// Initialise a model from read in MATLAB
CGp modelInit(&kernInit, &noiseInit, &X, approxType, numActive, 2);
modelInit.setScale(scale);
modelInit.setBias(bias);
modelInit.updateM();
CCmpndKern kern(X);
kern.readMatlabFile(fileName.c_str(), "kernInit");
//CGaussianNoise noise(y);
//noise.readMatlabFile("testGp.mat", "noiseInit");
// Initialise a model from gpInfo.
CGp model(&X, &y, &kern, &noiseInit, fileName.c_str(), "gpInfoInit", 0);
if(model.equals(modelInit))
{
cout << "GP initial model passed." << endl;
}
else
{
cout << "FAILURE: GP." << endl;
cout << "Matlab loaded model " << endl;
model.display(cout);
cout << "C++ Initialised Model " << endl;
modelInit.display(cout);
fail++;
}
// Compare C++ parameters with MATLAB provided parameters.
CMatrix fileParams;
fileParams.readMatlabFile(fileName.c_str(), "params");
CMatrix params(1, model.getOptNumParams());
model.getOptParams(params);
if(params.equals(fileParams))
{
cout << "Parameter match passed." << endl;
}
else
{
cout << "FAILURE: GP parameter match." << endl;
cout << "Matlab loaded params: " << fileParams << endl;
cout << "C++ Initialised params: " << params << endl;
cout << "Maximum difference: " << params.maxAbsDiff(fileParams) << endl;
fail++;
}
// Compare C++ gradients with MATLAB provided gradients.
CMatrix fileGrads;
fileGrads.readMatlabFile(fileName.c_str(), "grads");
CMatrix grads(1, model.getOptNumParams());
model.logLikelihoodGradient(grads);
if(grads.equals(fileGrads))
{
cout << "GP Gradient match passed." << endl;
}
else {
cout << "FAILURE: GP gradient match." << endl;
cout << "Matlab Gradient " << fileGrads << endl;
cout << "C++ Gradient " << grads << endl;
cout << "Maximum difference: " << grads.maxAbsDiff(fileGrads) << endl;
fail++;
}
// Compare C++ log likelihood with MATLAB provided log likelihood.
CMatrix fileLl;
fileLl.readMatlabFile(fileName.c_str(), "ll");
CMatrix ll(1, 1);
ll.setVal(model.logLikelihood(), 0, 0);
if(ll.equals(fileLl))
{
cout << "GP Log Likelihood match passed." << endl;
}
else
{
cout << "FAILURE: GP gradient match." << endl;
cout << "Matlab Log Likelihood " << fileLl << endl;
cout << "C++ Log Likelihood " << ll << endl;
fail++;
}
// Read and write tests
// Matlab read/write
model.writeMatlabFile("crap.mat", "writtenModel");
modelInit.readMatlabFile("crap.mat", "writtenModel");
if(model.equals(modelInit))
cout << "MATLAB written " << model.getName() << " matches read in model. Read and write to MATLAB passes." << endl;
else
{
cout << "FAILURE: MATLAB read in " << model.getName() << " does not match written out model." << endl;
fail++;
}
// Write to stream.
model.toFile("crap_gp");
modelInit.fromFile("crap_gp");
if(model.equals(modelInit))
{
cout << "Text written " << model.getName() << " matches read in model. Read and write to stream passes." << endl;
}
else
{
cout << "FAILURE: Stream read in " << model.getName() << " does not match written model." << endl;
cout << "Matlab loaded model " << endl;
model.display(cout);
cout << "Text Read in Model " << endl;
modelInit.display(cout);
fail++;
}
// model.checkGradients();
return fail;
}