-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathCTransform.h
367 lines (328 loc) · 11.3 KB
/
CTransform.h
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
/* This file contains classes that enable non-linear transformation of parameters. This is so that in the space where these parameters are optimised they can be optimised in a unconstrained way, but in the real parameter space they are effectively constrained. The `right' way to do this is through constrained optimisation, but this approach allows for the use of simple, effective algorithms such as scaled conjugate gradient. One commonly used transformation is a log transformation of a positive parameter.
16/3/2007 Added CExpTransform for performing logarithmic transformations of kernel parameters in line with the new default in the MATLAB optimi toolbox.
21/10/2005 More minor changes to do with removing redundant const modifiers.
20/10/2005 Changes from William V. Baxter for compilation under MSVC. The psoition of a couple of declarations has been changed and redundant consts have been removed.*/
#ifndef CTRANSFORM_H
#define CTRANSFORM_H
#include <cmath>
#include "CMatrix.h"
#include "ndlutil.h"
using namespace std;
const double limVal=36;
// This is a base class for non-linear variable transformation.
class CTransform
{
public:
CTransform()
{
transform = 0;
}
virtual ~CTransform() {}
CTransform(const CTransform& rhs) : type(rhs.type) {}
virtual CTransform* clone() const=0;
virtual double atox(double x) const=0;
virtual double xtoa(double x) const=0;
virtual double gradfact(double x) const=0;
virtual void setType(const string name)
{
type = name;
}
virtual string getType() const
{
return type;
}
static CTransform* defaultPositive();
static CTransform* defaultZeroOne();
static CTransform* getNewTransformPointer(const string transformType);
private:
string type;
protected:
bool transform;
#ifndef SWIG
// swig gives an error here
const static double eps;
#else
static double eps;
#endif
};
// This transform transforms from real to positive numbers.
class CExpTransform : public CTransform
{
public:
CExpTransform();
CExpTransform(const CExpTransform& rhs) : CTransform(rhs), transform(rhs.transform) {}
CTransform* clone() const {return new CExpTransform(*this);}
double atox(double a) const;
double xtoa(double x) const;
double gradfact(double x) const;
private:
bool transform;
};
// This transform transforms from real to positive numbers.
class CNegLogLogitTransform : public CTransform
{
public:
CNegLogLogitTransform();
CNegLogLogitTransform(const CNegLogLogitTransform& rhs) : CTransform(rhs), transform(rhs.transform) {}
CTransform* clone() const {return new CNegLogLogitTransform(*this);}
double atox(double a) const;
double xtoa(double x) const;
double gradfact(double x) const;
private:
bool transform;
};
class CLinearTransform : public CTransform
{
public:
CLinearTransform()
{
transform = 1;
setType("linear");
m = 1.0;
c = 0.0;
}
CLinearTransform(const CLinearTransform& rhs) : CTransform(rhs), transform(rhs.transform), m(rhs.m), c(rhs.c) {}
CTransform* clone() const {return new CLinearTransform(*this);}
double atox(double a) const
{
return (a-c)/m;
}
double xtoa(double x) const
{
return m*x+c;
}
double gradfact(double x) const
{
return 1/m;
}
void setM(double val)
{
m = val;
}
double getM() const
{
return m;
}
void setC(double val)
{
c = val;
}
double getC() const
{
return c;
}
private:
bool transform;
double m;
double c;
};
// This transformation goes from real numbers to the range [0->1]
class CSigmoidTransform : public CTransform
{
public:
CSigmoidTransform();
CSigmoidTransform(const CSigmoidTransform& rhs) : CTransform(rhs), transform(rhs.transform) {}
CTransform* clone() const {return new CSigmoidTransform(*this);}
double atox(double a) const;
double xtoa(double x) const;
double gradfact(double x) const;
private:
bool transform;
};
// A class for storing the parameter transformations.
class CParamTransforms : public CMatInterface, public CStreamInterface
{
public:
string getType() const
{
return "transforms";
}
string getBaseType() const
{
return "transforms";
}
bool equals(CParamTransforms transforms) const;
void display(ostream& out) const;
void writeParamsToStream(ostream& out) const;
void readParamsFromStream(istream& in);
#ifdef _NDLMATLAB
mxArray* toMxArray() const;
void fromMxArray(const mxArray* transformArray);
#endif
void addTransform(const CTransform* trans, unsigned int index)
{
transIndex.push_back(index);
transforms.push_back(trans);
}
void clearTransforms()
{
transIndex.clear();
transforms.clear();
}
inline string getTransformType(unsigned int ind) const
{
BOUNDCHECK(ind<getNumTransforms());
return transforms[ind]->getType();
}
inline unsigned int getTransformIndex(unsigned int ind) const
{
BOUNDCHECK(ind<getNumTransforms());
return transIndex[ind];
}
inline unsigned int getNumTransforms() const
{
return transforms.size();
}
vector<const CTransform*> transforms;
vector<unsigned int> transIndex;
};
// This is an abstract base class for making the parameters of a class transformable.
class CTransformable
{
public:
// these are the pure virtual functions.
virtual ~CTransformable(){}
virtual unsigned int getNumParams() const=0;
virtual double getParam(unsigned int paramNo) const=0;
virtual void setParam(double val, unsigned int paramNo)=0;
virtual void getGradParams(CMatrix& g) const=0;
// these are default implementations.
virtual void getParams(CMatrix& params) const
{
if(params.getRows()!=1)
throw ndlexceptions::RuntimeError("getParams(): Dimension match check failed, numbers of rows should be 1, currently it is " + ndlstrutil::itoa(params.getRows()));
if(params.getCols()!=getNumParams())
throw ndlexceptions::RuntimeError("getParams(): Dimension match check failed, numbers of columns should be " + ndlstrutil::itoa(getNumParams()) + ", currently it is " + ndlstrutil::itoa(params.getRows()));
for(unsigned int i=0; i<params.getCols(); i++)
params.setVal(getParam(i), i);
}
virtual void setParams(const CMatrix& params)
{
if(params.getRows()!=1)
throw ndlexceptions::RuntimeError("setParams(): Dimension match check failed, numbers of rows should be 1, currently it is " + ndlstrutil::itoa(params.getRows()));
if(params.getCols()!=getNumParams())
throw ndlexceptions::RuntimeError("setParams(): Dimension match check failed, numbers of columns should be " + ndlstrutil::itoa(getNumParams()) + ", currently it is " + ndlstrutil::itoa(params.getRows()));
for(unsigned int i=0; i<params.getCols(); i++)
setParam(params.getVal(i), i);
}
virtual double getTransParam(unsigned int paramNo) const
{
BOUNDCHECK(paramNo<getNumParams());
double param = getParam(paramNo);
vector<unsigned int>::const_iterator pos = find(transArray.transIndex.begin(),
transArray.transIndex.end(),
paramNo);
if(pos == transArray.transIndex.end())
return param;
else
{
unsigned int ind = pos - transArray.transIndex.begin();
return transArray.transforms[ind]->xtoa(param);
}
}
virtual void getTransParams(CMatrix& transParam) const
{
if(transParam.getRows()!=1)
throw ndlexceptions::RuntimeError("getTransParams(): Dimension match check failed, numbers of rows should be 1, currently it is " + ndlstrutil::itoa(transParam.getRows()));
if(transParam.getCols()!=getNumParams())
throw ndlexceptions::RuntimeError("getTransParams(): Dimension match check failed, numbers of columns should be " + ndlstrutil::itoa(getNumParams()) + ", currently it is " + ndlstrutil::itoa(transParam.getRows()));
getParams(transParam);
double val;
for(unsigned int i=0; i<transArray.transIndex.size(); i++) {
val=transParam.getVal(transArray.transIndex[i]);
transParam.setVal(transArray.transforms[i]->xtoa(val), transArray.transIndex[i]);
}
}
virtual void setTransParam(double val, unsigned int paramNo)
{
BOUNDCHECK(paramNo<getNumParams());
// this casting is required under solaris for some reason
vector<unsigned int>::iterator pos=find(transArray.transIndex.begin(),
transArray.transIndex.end(),
paramNo);
if(pos==transArray.transIndex.end())
setParam(val, paramNo);
else {
unsigned int ind = pos - transArray.transIndex.begin();
setParam(transArray.transforms[ind]->atox(val), paramNo);
}
}
virtual void setTransParams(const CMatrix& transParam)
{
if(transParam.getRows()!=1)
throw ndlexceptions::RuntimeError("setTransParams(): Dimension match check failed, numbers of rows should be 1, currently it is " + ndlstrutil::itoa(transParam.getRows()));
if(transParam.getCols()!=getNumParams())
throw ndlexceptions::RuntimeError("setTransParams(): Dimension match check failed, numbers of columns should be " + ndlstrutil::itoa(getNumParams()) + ", currently it is " + ndlstrutil::itoa(transParam.getRows()));
CMatrix param(transParam);
double val = 0.0;
for(unsigned int i=0; i<transArray.transIndex.size(); i++)
{
val = param.getVal(transArray.transIndex[i]);
param.setVal(transArray.transforms[i]->atox(val), transArray.transIndex[i]);
}
setParams(param);
}
virtual void getGradTransParams(CMatrix& g) const
{
if(g.getRows()!=1)
throw ndlexceptions::RuntimeError("getGradTransParams(): Dimension match check failed, numbers of rows should be 1, currently it is " + ndlstrutil::itoa(g.getRows()));
if(g.getCols()!=getNumParams())
throw ndlexceptions::RuntimeError("getGradTransParams(): Dimension match check failed, numbers of columns should be " + ndlstrutil::itoa(getNumParams()) + ", currently it is " + ndlstrutil::itoa(g.getRows()));
getGradParams(g);
double val;
double param;
for(size_t i=0; i<transArray.transIndex.size(); i++)
{
val=g.getVal(transArray.transIndex[i]);
param=getParam(transArray.transIndex[i]);
g.setVal(val*transArray.transforms[i]->gradfact(param), transArray.transIndex[i]);
}
}
// These are non-modifiable methods.
inline unsigned int getNumTransforms() const
{
return transArray.getNumTransforms();
}
inline const CTransform* getTransform(unsigned int ind) const
{
BOUNDCHECK(ind<getNumTransforms());
return transArray.transforms[ind];
}
inline string getTransformType(unsigned int ind) const
{
return transArray.getTransformType(ind);
}
inline unsigned int getTransformIndex(unsigned int ind) const
{
return transArray.getTransformIndex(ind);
}
inline double getTransformGradFact(double val, unsigned int ind) const
{
return transArray.transforms[ind]->gradfact(val);
}
void addTransform(const CTransform* trans, unsigned int index)
{
BOUNDCHECK(index<getNumParams());
transArray.addTransform(trans, index);
}
void clearTransforms()
{
for(size_t i = 0; i<transArray.transforms.size(); i++)
delete transArray.transforms[i];
transArray.transIndex.clear();
transArray.transforms.clear();
}
#ifdef _NDLMATLAB
mxArray* transformsToMxArray() const
{
return transArray.toMxArray();
}
void transformsFromMxArray(const mxArray* matlabArray)
{
transArray.fromMxArray(matlabArray);
}
#endif
private:
CParamTransforms transArray;
};
#endif