-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathCTransform.cpp
198 lines (189 loc) · 4.66 KB
/
CTransform.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
#include "CTransform.h"
const double CTransform::eps = 1e-16;
CTransform* CTransform::defaultPositive()
{
return new CExpTransform();
}
CTransform* CTransform::defaultZeroOne()
{
return new CSigmoidTransform();
}
CTransform* CTransform::getNewTransformPointer(const string transformType)
{
if(transformType=="negLogLogit")
return new CNegLogLogitTransform();
else if(transformType=="sigmoid")
return new CSigmoidTransform();
else if(transformType=="exp")
return new CExpTransform();
else if(transformType=="linear")
return new CLinearTransform();
else
throw ndlexceptions::Error("Transform type " + transformType + " is currently unknown.");
}
CExpTransform::CExpTransform()
{
transform = true;
setType("exp");
}
double CExpTransform::atox(double a) const
{
double x=0;
if(a<-limVal)
x = exp(-limVal);
else if(a<limVal)
x=exp(a);
else
x = exp(limVal);
SANITYCHECK(!isnan(x));
return x;
}
double CExpTransform::xtoa(double x) const
{
SANITYCHECK(x>0);
double a=log(x);
SANITYCHECK(!isnan(a));
return a;
}
double CExpTransform::gradfact(double x) const
{
return x;
}
CNegLogLogitTransform::CNegLogLogitTransform()
{
transform = 1;
setType("negLogLogit");
}
double CNegLogLogitTransform::atox(double a) const
{
double x=0;
if(a<-limVal)
x = exp(-limVal);
else if(a<limVal)
x=log(1+exp(a));
SANITYCHECK(!isnan(x));
return x;
}
double CNegLogLogitTransform::xtoa(double x) const
{
double a=x;
SANITYCHECK(a>-limVal);
if(a<limVal)
a=log(exp(x)-1);
SANITYCHECK(!isnan(a));
return a;
}
double CNegLogLogitTransform::gradfact(double x) const
{
double g;
if(x<limVal)
g=(exp(x)-1)/exp(x);
else
g=1.0;
return g;
}
CSigmoidTransform::CSigmoidTransform()
{
transform = 1;
setType("sigmoid");
}
double CSigmoidTransform::atox(double a) const
{
if(a<-limVal)
return ndlutil::EPS;
if(a<limVal)
return ndlutil::sigmoid(a);
else
return 1-ndlutil::EPS;
}
double CSigmoidTransform::xtoa(double x) const
{
return ndlutil::invSigmoid(x);
}
double CSigmoidTransform::gradfact(double x) const
{
return x*(1-x);
}
void CParamTransforms::writeParamsToStream(ostream& out) const
{
writeToStream(out, "numTransforms", getNumTransforms());
for(unsigned int i=0; i<getNumTransforms(); i++)
{
writeToStream(out, "type", getTransformType(i));
writeToStream(out, "index", getTransformIndex(i));
}
}
void CParamTransforms::readParamsFromStream(istream& in)
{
transforms.clear();
transIndex.clear();
int numTrans = readIntFromStream(in, "numTransforms");
for(int i=0; i<numTrans; i++)
{
string trtype = readStringFromStream(in, "type");
int trIndex = readIntFromStream(in, "index");
addTransform(CTransform::getNewTransformPointer(trtype), trIndex);
}
}
void CParamTransforms::display(ostream& out) const
{
out << "Parameter Transforms:" << endl;
for(unsigned int i=0; i<getNumTransforms(); i++)
{
out << "Transform type: " << getTransformType(i) << endl;
out << "Transform index: " << getTransformIndex(i) << endl;
}
}
bool CParamTransforms::equals(CParamTransforms transforms) const
{
if(getNumTransforms()!=transforms.getNumTransforms())
return false;
for(unsigned int i=0; i<getNumTransforms(); i++)
{
if(getTransformType(i)!=transforms.getTransformType(i))
return false;
if(getTransformIndex(i)!=transforms.getTransformIndex(i))
return false;
}
return true;
}
#ifdef _NDLMATLAB
mxArray* CParamTransforms::toMxArray() const
{
size_t dims[1];
// transforms field.
const char *transFieldNames[] = {"index", "type"};
dims[0]=getNumTransforms();
mxArray* transformsArray = mxCreateStructArray(1, dims, 2, transFieldNames);
CMatrix ind(1, 1);
const char *compType[1];
string trType;
for(int i=0; i<getNumTransforms(); i++)
{
ind.setVals((double)(getTransformIndex(i)+1));
mxSetField(transformsArray, i, "index", ind.toMxArray());
trType = getTransformType(i);
compType[0] = trType.c_str();
mxSetField(transformsArray, i, "type", mxCreateCharMatrixFromStrings(1, compType));
}
return transformsArray;
}
void CParamTransforms::fromMxArray(const mxArray* transformArray)
{
int numTransforms = mxGetNumberOfElements(transformArray);
string transformType;
vector<int> transformIndex;
int counter = 0;
for(int i=0; i<numTransforms; i++)
{
transformType=mxArrayExtractStringField(transformArray, "type", i);
transformIndex=mxArrayExtractVectorIntField(transformArray, "index", i);
for(int j=0; j<transformIndex.size(); j++)
{
counter++;
CTransform* trans = CTransform::getNewTransformPointer(transformType);
addTransform(trans, transformIndex[j]-1);
}
}
}
#endif