-
Notifications
You must be signed in to change notification settings - Fork 28
/
CMltools.h
215 lines (172 loc) · 5.72 KB
/
CMltools.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
/* This file and CMltools.cpp contain classes for general machine learning models such as multi-layer perceptrons. */
#ifndef CMLTOOLS_H
#define CMLTOOLS_H
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
#include <algorithm>
#include "ndlassert.h"
#include "ndlexceptions.h"
#include "ndlstrutil.h"
#include "COptimisable.h"
#include "CNdlInterfaces.h"
#include "CMatrix.h"
#include "CKern.h"
#include "CNoise.h"
#include "CTransform.h"
#include "CMatrix.h"
#include "CDist.h"
#include "CDataModel.h"
#include "ndlstrutil.h"
#include "ndlutil.h"
using namespace std;
const string MLTOOLSVERSION="0.1";
// Multi-layer peceptron model.
class CLinearMapping: public CMapModel, public COptimisable, public CStreamInterface, public CMatInterface
{
public:
CLinearMapping();
CLinearMapping(CMatrix* pXin, CMatrix* pyOut, int verbos=2);
//CLinearMapping(const CMatrix* X);
~CLinearMapping(){};
//CLinearMapping(const CLinearMapping& model);
CLinearMapping * clone() const
{
return new CLinearMapping (*this);
}
#ifdef _NDLMATLAB
// Constructor using file containing linearMapping.
CLinearMapping(CMatrix* inData,
CMatrix* targetData,
const string linearMappingInfoFile,
const string linearMappingInfoVariable,
int verbos=2);
mxArray* toMxArray() const;
void fromMxArray(const mxArray* matlabArray);
#endif
void initStoreage();
void initVals();
//void setInitParam();
//void setParam(double val, unsigned int paramNum);
//double getParam(int paramNum) const;
//double getGradParam(int index, const CMatrix& X) const;
void out(CMatrix& yPred, const CMatrix& inData) const;
//void out(CMatrix& yPred, CMatrix& probPred, const CMatrix& inData) const;
double outGradParams(CMatrix& g, const CMatrix& inData, unsigned int i, unsigned int j) const;
double outGradX(CMatrix& g, const CMatrix& inData, unsigned int i, unsigned int j) const;
void setWeights(const CMatrix& W, unsigned int layer);
void setBias(const CMatrix& b, unsigned int layer);
double logLikelihood() const;
double logLikelihoodGradient(CMatrix& g) const;
void updateG() const;
double pointLogLikelihood(const CMatrix& yOut, const CMatrix& Xin) const;
void display(ostream& os) const;
void writeParamsToStream(ostream& os) const;
void readParamsFromStream(istream& is);
void optimise(unsigned int iters=1000);
// specify tests for equality between two models.
virtual bool equals(const CLinearMapping& model, double tol=ndlutil::MATCHTOL) const;
virtual unsigned int getOptNumParams() const;
virtual void getOptParams(CMatrix& param) const;
virtual void setOptParams(const CMatrix& param);
double computeObjectiveGradParams(CMatrix& g) const
{
double L = logLikelihoodGradient(g);
g.negate();
return -L;
}
double computeObjectiveVal() const
{
return -logLikelihood();
}
private:
CMatrix W;
CMatrix b;
mutable CMatrix outActive;
double variance;
CMatrix* pX;
CMatrix* py;
void _init(); // initialise the structure.
};
// Multi-layer peceptron model.
class CMlpMapping: public CMapModel, public COptimisable, public CStreamInterface, public CMatInterface
{
public:
CMlpMapping();
CMlpMapping(CMatrix* pXin, CMatrix* pyOut, unsigned int nhidden, int verbos=2);
//CMlpMapping(const CMatrix* X);
~CMlpMapping(){};
//CMlpMapping(const CMlpMapping& model);
CMlpMapping * clone() const
{
return new CMlpMapping (*this);
}
#ifdef _NDLMATLAB
// Constructor using file containing mlpMapping.
CMlpMapping(CMatrix* inData,
CMatrix* targetData,
const string mlpMappingInfoFile,
const string mlpMappingInfoVariable,
int verbos=2);
mxArray* toMxArray() const;
void fromMxArray(const mxArray* matlabArray);
#endif
void initStoreage();
void initVals();
//void setInitParam();
//void setParam(double val, unsigned int paramNum);
//double getParam(int paramNum) const;
//double getGradParam(int index, const CMatrix& X) const;
void out(CMatrix& yPred, const CMatrix& inData) const;
//void out(CMatrix& yPred, CMatrix& probPred, const CMatrix& inData) const;
double outGradParams(CMatrix& g, const CMatrix& inData, unsigned int i, unsigned int j) const;
double outGradX(CMatrix& g, const CMatrix& inData, unsigned int i, unsigned int j) const;
void setWeights(const CMatrix& W, unsigned int layer);
void setBias(const CMatrix& b, unsigned int layer);
double logLikelihood() const;
double logLikelihoodGradient(CMatrix& g) const;
void updateG() const;
double pointLogLikelihood(const CMatrix& yOut, const CMatrix& Xin) const;
void display(ostream& os) const;
void writeParamsToStream(ostream& os) const;
void readParamsFromStream(istream& is);
unsigned int getHiddenDim() const
{
return hiddenDim;
}
void optimise(unsigned int iters=1000);
// specify tests for equality between two models.
virtual bool equals(const CMlpMapping& model, double tol=ndlutil::MATCHTOL) const;
virtual unsigned int getOptNumParams() const;
virtual void getOptParams(CMatrix& param) const;
virtual void setOptParams(const CMatrix& param);
double computeObjectiveGradParams(CMatrix& g) const
{
double L = logLikelihoodGradient(g);
g.negate();
return -L;
}
double computeObjectiveVal() const
{
return -logLikelihood();
}
private:
CMatrix W1;
CMatrix b1;
CMatrix W2;
CMatrix b2;
mutable CMatrix hiddenActive;
mutable CMatrix tanhActive;
mutable CMatrix outActive;
double variance;
unsigned int hiddenDim;
CMatrix* pX;
CMatrix* py;
void _init(); // initialise the structure.
};
//void writeMapModelToStream(const CMapModel& model, ostream& out);
//CMapModel* readMapModelFromStream(istream& in);
#endif