-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmlp.cpp
More file actions
442 lines (391 loc) · 11.1 KB
/
Copy pathmlp.cpp
File metadata and controls
442 lines (391 loc) · 11.1 KB
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#include <cstring>
#include <cstdlib>
#include <math.h>
#include <string>
#include <iomanip>
#include "utils.h"
#include <valarray>
#include <iostream>
#include <fstream>
#include "mlp.h"
#ifdef _WIN32
#include <mkl.h>
#define F77_FUNC(func) (func)
#else
#include "fortran.h"
#define F77_FUNC(func) func##_
#endif
#include "matops.h"
#ifndef MYDEFS_H
#define MYDEFS_H
#include "mydefs.h"
#endif
double h(double x)
{
return(1./(1+exp(-x)));
}
using namespace std;
extern double slete(double, double);
extern double slete1(double, double );
void MLP::init_weights()
{
for (int i=0;i<Nh;i++)
{
W[i][N]=slete(0,1);
for (int j=0;j<N;j++)
W[i][j]=slete(0,1)/Xstd[j];
}
}
void MLP::set_W(const matrix &X)
{
if ((X.getRow() != W.getRow())|| (X.getCol() != W.getCol()))
{
cout << "set_W: wrong dimensions of matrix" << endl;
exit(0);
}
W=X;
}
void MLP::set_Woh(const matrix &X)
{
if ((X.getRow() != Woh.getRow())|| (X.getCol() != Woh.getCol()))
{
cout << "set_W: wrong dimensions of matrix" << endl;
exit(0);
}
Woh=X;
}
void MLP::set_Woi(const matrix &X)
{
if ((X.getRow() != Woi.getRow())|| (X.getCol() != Woi.getCol()))
{
cout << "set_Woi: wrong dimensions of matrix" << endl;
exit(0);
}
Woi=X;
}
void MLP::write_weights(string output)
{
ofstream outfile(output.c_str());
outfile << "500" << endl;
outfile << trnFile << endl;
outfile << N << endl;
outfile << M << endl;
outfile << Nh << endl;
outfile << "The_training_algorithm_is_OWO-Newton" << endl;
outfile << transpose(Woi);
outfile << transpose(Woh);
outfile << W;
outfile.close();
}
double MLP::J2(const matrix& Wa)
//The objective function is computed by making one pass through the data with
//the current weights.
//ASSUMPTIONS: The input mean has been incorporated into the thresholds
{
ifstream trnfile,infile;
double ZERO=0.0,ONE=1.0;
valarray<double> e(M);
int i,NPlusOne,num_patterns=0;
int INTONE=1;
valarray<double> x(N+1),t(M),y(M),net(Nh),temp(M);
NPlusOne = N + 1;
trnfile.open(trnFile.c_str());
while (!trnfile.eof())
{
for (i=0;i<N;i++)
trnfile >> x[i];
x[N]=1.0;
if (!trnfile.eof())
for (i=0;i<M;i++)
trnfile >> t[i];
else
break;
num_patterns++;
F77_FUNC(dgemv)("T",&NPlusOne,&Nh,&ONE,&Wa[0][0],&NPlusOne,&x[0],&INTONE,&ZERO,&net[0],&INTONE);
net=net.apply(h);
F77_FUNC(dgemv)("T",&Nh,&M,&ONE,&Woh[0][0],&Nh,&net[0],&INTONE,&ZERO,&y[0],&INTONE);
F77_FUNC(dgemv)("T",&NPlusOne,&M,&ONE,&Woi[0][0],&NPlusOne,&x[0],&INTONE,&ONE,&y[0],&INTONE);
temp = t-y;
e += (temp*temp);
}
trnfile.close();
return(e.sum()/num_patterns);
}
double MLP::J(const string fname)
//The objective function is computed by making one pass through the data with
//the current weights.
//ASSUMPTIONS: The input mean has been incorporated into the thresholds
{
ifstream trnfile,infile;
double ZERO=0.0,ONE=1.0;
valarray<double> e(M);
int i,NPlusOne,num_patterns=0;
int INTONE=1;
valarray<double> x(N+1),t(M),y(M),net(Nh),temp(M);
NPlusOne = N + 1;
infile.open(fname.c_str());
while (!infile.eof())
{
for (i=0;i<N;i++)
infile >> x[i];
x[N]=1.0;
if (!infile.eof())
for (i=0;i<M;i++)
infile >> t[i];
else
break;
num_patterns++;
F77_FUNC(dgemv)("T",&NPlusOne,&Nh,&ONE,&W[0][0],&NPlusOne,&x[0],&INTONE,&ZERO,&net[0],&INTONE);
net=net.apply(h);
F77_FUNC(dgemv)("T",&Nh,&M,&ONE,&Woh[0][0],&Nh,&net[0],&INTONE,&ZERO,&y[0],&INTONE);
F77_FUNC(dgemv)("T",&NPlusOne,&M,&ONE,&Woi[0][0],&NPlusOne,&x[0],&INTONE,&ONE,&y[0],&INTONE);
temp = t-y;
e += (temp*temp);
}
infile.close();
return(e.sum()/num_patterns);
}
double MLP::J2(const matrix& Wa,const matrix& W1,const matrix& W2)
//The objective function is computed by making one pass through the data with
//the current weights.
//ASSUMPTIONS: The input mean has been incorporated into the thresholds
{
ifstream trnfile,infile;
double ZERO=0.0,ONE=1.0;
valarray<double> e(M);
int i,NPlusOne,num_patterns=0;
int INTONE=1;
valarray<double> x(N+1),t(M),y(M),net(Nh),temp(M);
NPlusOne = N + 1;
trnfile.open(trnFile.c_str());
while (!trnfile.eof())
{
for (i=0;i<N;i++)
trnfile >> x[i];
x[N]=1.0;
if (!trnfile.eof())
for (i=0;i<M;i++)
trnfile >> t[i];
else
break;
num_patterns++;
F77_FUNC(dgemv)("T",&NPlusOne,&Nh,&ONE,&Wa[0][0],&NPlusOne,&x[0],&INTONE,&ZERO,&net[0],&INTONE);
net=net.apply(h);
F77_FUNC(dgemv)("T",&Nh,&M,&ONE,&W2[0][0],&Nh,&net[0],&INTONE,&ZERO,&y[0],&INTONE);
F77_FUNC(dgemv)("T",&NPlusOne,&M,&ONE,&W1[0][0],&NPlusOne,&x[0],&INTONE,&ONE,&y[0],&INTONE);
temp = t-y;
e += (temp*temp);
}
trnfile.close();
return(e.sum()/num_patterns);
}
double MLP::J2(const matrix& W1,const matrix& W2)
//The objective function is computed by making one pass through the data with
//the current weights.
//ASSUMPTIONS: The input mean has been incorporated into the thresholds
{
ifstream trnfile,infile;
double ZERO=0.0,ONE=1.0;
valarray<double> e(M);
int i,NPlusOne,num_patterns=0;
int INTONE=1;
valarray<double> x(N+1),t(M),y(M),net(Nh),temp(M);
NPlusOne = N + 1;
trnfile.open(trnFile.c_str());
while (!trnfile.eof())
{
for (i=0;i<N;i++)
trnfile >> x[i];
x[N]=1.0;
if (!trnfile.eof())
for (i=0;i<M;i++)
trnfile >> t[i];
else
break;
num_patterns++;
F77_FUNC(dgemv)("T",&NPlusOne,&Nh,&ONE,&W[0][0],&NPlusOne,&x[0],&INTONE,&ZERO,&net[0],&INTONE);
net=net.apply(h);
F77_FUNC(dgemv)("T",&Nh,&M,&ONE,&W2[0][0],&Nh,&net[0],&INTONE,&ZERO,&y[0],&INTONE);
F77_FUNC(dgemv)("T",&NPlusOne,&M,&ONE,&W1[0][0],&NPlusOne,&x[0],&INTONE,&ONE,&y[0],&INTONE);
temp = t-y;
e += (temp*temp);
}
trnfile.close();
return(e.sum()/num_patterns);
}
void MLP::spit_info()
{
cout << "Class MethodTraining file: " << trnFile.c_str() << endl;
cout << "Number of patterns: " << Nv << endl;
cout << "Number of hidden units: " << Nh << endl;
cout << "Number of inputs: " << N << endl;
cout << "Number of outputs: " << M << endl << endl;
}
void MLP::update_weights(double alpha,const matrix& D)
{
W += alpha*D;
}
void MLP::save_weights()
{
// memcpy(&W_old[0][0],&W[0][0],(N+1)*Nh*sizeof(double));
}
void MLP::restore_weights()
{
// memcpy(&W[0][0],&W_old[0][0],(N+1)*Nh*sizeof(double));
}
int MLP::getNumOutputs(void)
{
return (N);
}
MLP::MLP(int N1,int N2,int N3,string fname):net_std(N3),Et(N2)
{
N=N1;
Nh=N3;
M=N2;
Nv=0;
Nu = N + Nh + 1;
Xstd=valarray<double>(N1+1);
net_mean=valarray<double>(N3);
//H=matrix(Nh*(N+1),Nh*(N+1));
H_OIT=matrix((N+1)*(N+1),(N+1)*(N+1));
//H_ONT=matrix(Nh*Nh,Nh*Nh);
G=matrix(Nh,N+1);
G1=matrix(Nh,N+1);
R=matrix(Nu,Nu);
Ri=matrix(N+1,N+1);
C=matrix(M,Nu);
W=matrix(Nh,N+1);
Woi=matrix(M,N+1);
Woh=matrix(M,Nh);
H_ig=matrix(N+1,N+1);
g_ig=valarray<double>(N+1);
Gt=valarray<double>((N+1)*(Nh+M)+M*Nh);
G_oit=matrix(N+1,N+1);
Ht=matrix((N+1)*(Nh+M)+M*Nh,(N+1)*(Nh+M)+M*Nh);
trnFile=fname;
}
MLP::~MLP()
{
}
size_t MLP::init_mlp(string trnFile)
/* Calculates the mean, variance and total energy in the datafile */
//Inputs:
//N Number of inputs
//Nh Number of hidden units
//Nc Number of outputs
//OUTPUT:
//Nv: number of patterns that are read
//Xmean: The mean vector for the training file
//Xstd: The standard deviation vector for the training file
{
int i;
ifstream infile;
valarray<double> X(N+1),Xmean(N+1),ty(M);
infile.open(trnFile.c_str());
this->trnFile=trnFile;
if (!infile.is_open())
{
cout << "Error reading file in stats routine" << endl;
exit(0);
}
Nv=0;
while (!infile.eof())
{
for (i = 0; i < N; i++)
infile >> X[i];
X[N]=1.0;
if (infile.eof())
break;
else
for (i=0;i<M;i++)
infile >> ty[i];
Nv++;
Xmean += X;
Xstd += X*X;
Et += ty*ty;
}
Xmean /= Nv;
Xstd = Xstd / (double)(Nv) - Xmean*Xmean;
Xstd = sqrt(Xstd);
Et /= double(Nv);
valarray<double> sub1(Nh),sub2(M);
sub1=W*Xmean;
sub2=Woi*Xmean;
for (i=0;i<Nh;i++)
W[i][N] -= sub1[i];
for (i=0;i<M;i++)
Woi[i][N] -= sub2[i];
infile.close();
return(Nv);
}
matrix MLP::get_gradient(void)
{
size_t M=G.getRow(),N=G.getCol();
matrix temp(M,N);
temp = G;
return(temp);
}
matrix MLP::get_output_weights(void)
{
matrix temp;
temp=vertcat(true,true,Woi,Woh);
return(temp);
}
matrix MLP::get_input_weights(void)
{
matrix temp(Nh,N+1);
temp=W;
return(temp);
}
double MLP::J(const matrix &Wa)
{
matrix weights(Nh,Nu);
matrix Wb;
valarray<double> E(M),temp1(M),temp2(M);
Wb=vertcat(true,true,Woi,Woh);
temp1=Wb.extract_row(N);
for (int i=N+1;i<Nu;i++)
{
temp2=Wb.extract_row(i);
Wb.insert_row(i-1,temp2);
}
Wb.insert_row(N+Nh,temp1);
E=Et+diag(transpose(Wb)*R*Wb-2.*C*Wb);
return(E.sum());
}
double act(double x)
{
return(1./(1+exp(-x)));
}
valarray<double> MLP::process_pattern(valarray<double>& x,valarray<double>& net,valarray<double>& O)//,const matrix &D)
{
valarray<double> y(M);//net(Nh),y(M),O(Nh);
int INTONE=1;
double ZERO = 0.0,ONE=1.0;
int NPlusOne=N+1;
matrix tempW(Nh,N+1);
tempW=W;
// for (int i=0;i<Nh;i++)
// net[i]=O[i]=0;
dgemv_("T",&NPlusOne,&Nh,&ONE,&tempW[0][0],&NPlusOne,&x[0],&INTONE,&ZERO,&net[0],&INTONE);
O=net.apply(act);
//Part 1 of compute the output variable y
dgemv_("T",&Nh,&M,&ONE,&Woh[0][0],&Nh,&O[0],&INTONE,&ZERO,&y[0],&INTONE);
//Part 2 which adds the bypass weights
dgemv_("T",&NPlusOne,&M,&ONE,&Woi[0][0],&NPlusOne,&x[0],&INTONE,&ONE,&y[0], &INTONE);
return y;
}
valarray<double> MLP::process_pattern(valarray<double>& x)
{
valarray<double> net(Nh),y(M),O(Nh);
int INTONE=1;
double ZERO = 0.0,ONE=1.0;
int NPlusOne=N+1;
dgemv_("T",&NPlusOne,&Nh,&ONE,&W[0][0],&NPlusOne,&x[0],&INTONE,&ZERO,&net[0],&INTONE);
O=net.apply(act);
//Part 1 of compute the output variable y
dgemv_("T",&Nh,&M,&ONE,&Woh[0][0],&Nh,&O[0],&INTONE,&ZERO,&y[0],&INTONE);
//Part 2 which adds the bypass weights
dgemv_("T",&NPlusOne,&M,&ONE,&Woi[0][0],&NPlusOne,&x[0],&INTONE,&ONE,&y[0], &INTONE);
return y;
}