-
Notifications
You must be signed in to change notification settings - Fork 62
/
DNN.cpp
568 lines (517 loc) · 18.1 KB
/
DNN.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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
#include "DNN.h"
namespace MLL{
/**
所有参数都定义为全局标量,结构体不需要在函数之间传递,
根据超参数初始化参数
**/
int DNN::init_parameters()
{
int k=0,i=0,j=0;
double radom = 0;
int L=_sup_par.layer_dims;//网络层数
parameters *p = &_par;//参数,结构体已定义并分配内存,结构体内矩阵未分配内存
grad *g = &_gra;//梯度,结构体已定义并分配内存,结构体内矩阵未分配内存
/**
随机初始化
**/
//p->A.initMatrix(sup_par.layer_n[k],X._col,0);
//p->AT.initMatrix(X._col,X._row,0);
for(k=0; k<L-1; k++)
{
p->A.initMatrix(_sup_par.layer_n[k],_x._col,0);
//用于dropout,这里初始化一次即可,后面当使用dropout时,D才会赋值,不使用则不赋值,且实际使用长度小于网络层数
p->D.initMatrix(_sup_par.layer_n[k],_x._col,0);
p->W.initMatrix(_sup_par.layer_n[k+1],_sup_par.layer_n[k],0);
p->b.initMatrix(_sup_par.layer_n[k+1],1,0);
p->Z.initMatrix(_sup_par.layer_n[k+1],_x._col,0);
for(i=0; i<p->W._row; i++)
{
for(j=0; j<p->W._col; j++)
{
if(_initialization=="he")
{
radom=(rand()%100)/100.0;
p->W._data[i][j]=radom * sqrt(2.0/_sup_par.layer_n[k]);//一种常用的参数初始化方法,参数初始化也有技巧
}
if(_initialization=="random")
{
radom=(rand()%100)/100.0;
p->W._data[i][j]=radom;//一种常用的参数初始化方法,参数初始化也有技巧
}
if(_initialization=="arxiv")
{
radom=(rand()%100)/100.0;
p->W._data[i][j]=radom/sqrt(_sup_par.layer_n[k]);//一种常用的参数初始化方法,参数初始化也有技巧
}
}
}
p->next=new parameters();//下一层网络参数
p->next->pre=p;
p=p->next;
g->grad_A.initMatrix(_sup_par.layer_n[L-k-1],_x._col,0);
g->grad_Z.initMatrix(_sup_par.layer_n[L-k-1],_x._col,0);
g->grad_W.initMatrix(_sup_par.layer_n[L-k-1],_sup_par.layer_n[L-k-2],0);
g->grad_b.initMatrix(_sup_par.layer_n[L-k-1],1,0);
//用于momentum 和adam优化中用于保存前n次加权平均值
g->V_dw.initMatrix(_sup_par.layer_n[L-k-1],_sup_par.layer_n[L-k-2],0);
g->V_db.initMatrix(_sup_par.layer_n[L-k-1],1,0);
g->S_dw.initMatrix(_sup_par.layer_n[L-k-1],_sup_par.layer_n[L-k-2],0);
g->S_db.initMatrix(_sup_par.layer_n[L-k-1],1,0);
//用于修正的momentum 和adam
g->V_dw_corrected.initMatrix(_sup_par.layer_n[L-k-1],_sup_par.layer_n[L-k-2],0);
g->V_db_corrected.initMatrix(_sup_par.layer_n[L-k-1],1,0);
g->S_dw_corrected.initMatrix(_sup_par.layer_n[L-k-1],_sup_par.layer_n[L-k-2],0);
g->S_db_corrected.initMatrix(_sup_par.layer_n[L-k-1],1,0);
g->pre=new grad();//上一层网络参数梯度
g->pre->next=g;
g=g->pre;
}
p->A.initMatrix(_sup_par.layer_n[k],_x._col,0);
g->grad_A.initMatrix(_sup_par.layer_n[L-k-1],_x._col,0);
return 0;
}
void DNN::line_forward(parameters *p,double keep_prob)
{
int i=0,j=0;
if(keep_prob!=1)
{
for(i=0; i<p->D._row; i++)
{
for(j=0; j<p->D._col; j++)
{
p->D._data[i][j]=(rand()%100)/100.0;
if(p->D._data[i][j]<keep_prob)
p->D._data[i][j]=1.0/keep_prob; //这里已经扩充了keep_prob
else
p->D._data[i][j]=0;
}
}
p->A = p->A * p->D;
}
std::cout<<"zzz"<<p->Z._row<<"&&"<<p->Z._col<<std::endl;
std::cout<<"www"<<p->W._row<<"&&"<<p->W._col<<std::endl;
std::cout<<"aaa"<<p->A._row<<"&&"<<p->A._col<<std::endl;
std::cout<<"bbb"<<p->b._row<<"&&"<<p->b._col<<std::endl;
p->Z.print();
p->b.print();
p->Z = p->W * p->A;
for(i=0; i<p->Z._row; i++) //矩阵与向量的相加,class中未写
{
for(j=0; j<p->Z._col; j++)
{
p->Z._data[i][j]+=p->b._data[i][0];//这里可以把b也定义为等大小的矩阵,每行一样
}
}
}
void DNN::sigmoid_forward(parameters *p)
{
int i,j;
for(i=0; i<p->Z._row; i++)
{
for(j=0; j<p->Z._col; j++)
{
p->next->A._data[i][j]=1.0/(1.0+exp(-p->Z._data[i][j]));//sigmoid(p->Z._data[i][j]);
}
}
}
void DNN::relu_forward(parameters *p)
{
int i,j;
for(i=0; i<p->Z._row; i++)
{
for(j=0; j<p->Z._col; j++)
{
if(p->Z._data[i][j]>0)
{
p->next->A._data[i][j] = p->Z._data[i][j];
}
else
{
p->next->A._data[i][j]=0;
}
}
}
}
void DNN::line_active_forward(parameters *p,std::string active, double keep_prob)
{
line_forward(p,keep_prob);
if(active=="relu")
{
relu_forward(p);
}
if(active=="sigmoid")
{
sigmoid_forward(p);
}
}
Matrix DNN::model_forward(double *keep_probs)
{
int i=0;
int L=_sup_par.layer_dims;
parameters *p = &_par;
p->A = _x;
for(i=0; i<L-1 && p->next!=NULL; i++)
{
line_active_forward(p,_sup_par.layer_active[i+1],keep_probs[i]);
p=p->next;
}
return p->A;
}
void DNN::sigmoid_backword(parameters *p,grad *g)
{
int i=0,j=0;
for(i=0; i<g->grad_A._row; i++)
{
for(j=0; j<g->grad_A._col; j++)
{
g->grad_Z._data[i][j]=g->grad_A._data[i][j]*p->A._data[i][j]*(1-p->A._data[i][j]);
}
}
}
void DNN::relu_backword(parameters *p,grad *g)
{
int i=0,j=0;
for(i=0; i<g->grad_Z._row; i++)
{
for(j=0; j<g->grad_Z._col; j++)
{
if(p->pre->Z._data[i][j]>0)
{
g->grad_Z._data[i][j]=g->grad_A._data[i][j];
}
else
{
g->grad_Z._data[i][j]=0;
}
}
}
}
void DNN::line_backword(parameters *p,grad *g, double keep_prob)
{
int i,j;
Matrix AT = p->A.transposeMatrix();
g->grad_W = g->grad_W.multsMatrix(g->grad_Z,AT);
if(_lambd!=0)
{
for(i=0; i<p->W._row; i++)
{
for(j=0; j<p->W._col; j++)
{
g->grad_W._data[i][j]+=(_lambd * p->W._data[i][j]);
}
}
}
for(i=0; i<g->grad_W._row; i++)
{
for(j=0; j<g->grad_W._col; j++)
{
g->grad_W._data[i][j]/=g->grad_Z._col;
}
}
for(i=0; i<g->grad_Z._row; i++)
{
g->grad_b._data[i][0]=0;
for(j=0; j<g->grad_Z._col; j++)
{
g->grad_b._data[i][0]+=g->grad_Z._data[i][j];
}
g->grad_b._data[i][0]/=g->grad_Z._col;
}
Matrix WT = p->W.transposeMatrix();
g->pre->grad_A = g->pre->grad_A.multsMatrix(WT,g->grad_Z);
if(keep_prob!=1)
{
//这里p指向的D与对应A的dropout层,而等于1的情况下,D是只有初始化,无关赋值,所以对应dropout关系是正确的
//std::cout<<p->D._col<<"&"<<p->D._row<<std::endl;
//std::cout<<g->pre->grad_A._col<<"&"<<g->pre->grad_A._row<<std::endl;
g->pre->grad_A = g->pre->grad_A.multsMatrix(g->pre->grad_A,p->D);//由于keep_prob扩充已经放到D上了
}
//AT.clear();
//WT.clear();
}
void DNN::line_active_backword(parameters *p,grad *g,std::string active, double keep_prob)
{
std::cout<<"active_backword_start"<<std::endl;
if(active=="sigmoid")
{
sigmoid_backword(p,g);
}
if(active=="relu")
{
relu_backword(p,g);
}
std::cout<<"line_backword_start"<<std::endl;
line_backword(p->pre,g,keep_prob);
}
void DNN::model_backword(Matrix AL,double *keep_probs)
{
int i=0,j=0;
int L=_sup_par.layer_dims;
parameters *p = &_par;
while(p->next!=NULL)
{
p=p->next;
}
grad *g = &_gra;
for(i=0; i< _y._col; i++)
{
_gra.grad_A._data[0][i]=-(_y._data[0][i]/AL._data[0][i]-(1 - _y._data[0][i])/(1-AL._data[0][i]));
}
for(i=L-1; i>0; i--)
{
line_active_backword(p,g,_sup_par.layer_active[i],keep_probs[i]);
g=g->pre;
p=p->pre;
}
}
double DNN::cost_cumpter(Matrix AL)
{
int i=0,j=0;
int m = _y._col;//样本数
double loss=0;
double loss_L2_regularization=0;
if(_lambd!=0)
{
parameters *p = &_par;
while(p!=NULL)
{
for(i=0;i<p->W._row;i++)
{
for(j=0;j<p->W._col;j++)
{
loss_L2_regularization+=(_lambd*p->W._data[i][j]*p->W._data[i][j]);
}
}
p=p->next;
}
loss_L2_regularization/=(2*m);
}
for(i=0; i<m; i++)
{
loss+=-(_y._data[0][i]*log(AL._data[0][i])+(1 - _y._data[0][i])*log(1-AL._data[0][i]));
}
loss/=m;
//loss+=loss_L2_regularization;
return loss;
}
int DNN::updata_parameters_with_gd(int t)
{
int k=0,i=0,j=0;
int L = _sup_par.layer_dims;
parameters *p = &_par;
grad *g = &_gra;
while(g->pre->pre!=NULL)//反向和前向组合参数的方式不同,前者A=sgn(WX+b),后者 AL=sgn(WA+b)
{
g=g->pre;
}
//learn_rateing *= pow(0.999, t/1000);//指数衰减
for(k=0; k<L-1&&p->next!=NULL&&g!=NULL; k++)
{
for(i=0; i<g->grad_W._row; i++)
{
g->grad_b._data[i][0] *= -_learn_rateing;
for(j=0; j<g->grad_W._col; j++)
{
g->grad_W._data[i][j] *= -_learn_rateing;
}
}
p->W = p->W + g->grad_W;
p->b = p->b + g->grad_b;
p=p->next;
g=g->next;
}
return 0;
}
int DNN::updata_parameters_with_momentum(int t)
{
int k=0,i=0,j=0;
int L = _sup_par.layer_dims;
parameters *p = &_par;
grad *g = &_gra;
while(g->pre->pre!=NULL)//反向和前向组合参数的方式不同,前者A=sgn(WX+b),后者 AL=sgn(WA+b)
{
g=g->pre;
}
//learn_rateing *= pow(0.999, t/1000);//指数衰减
for(k=0; k<L-1&&p->next!=NULL&&g!=NULL; k++)
{
for(i=0; i<g->grad_W._row; i++)
{
g->V_db._data[i][0]=(_beta1 * g->V_db._data[i][0] + (1 - _beta1) * g->grad_b._data[i][0]);
g->V_db_corrected._data[i][0] = g->V_db._data[i][0] / (1-pow(_beta1,t));//修正
g->grad_b._data[i][0]=(-_learn_rateing) * g->V_db_corrected._data[i][0];
for(j=0; j<g->grad_W._col; j++)
{
g->V_dw._data[i][j]=(_beta1 * g->V_dw._data[i][j] + (1 - _beta1) * g->grad_W._data[i][j]);
g->V_dw_corrected._data[i][j]=g->V_dw._data[i][j] / (1-pow(_beta1,t));//修正
g->grad_W._data[i][j]=(-_learn_rateing) * g->V_dw_corrected._data[i][j];
}
}
p->W = p->W + g->grad_W;
p->b = p->b + g->grad_b;
p=p->next;
g=g->next;
}
return 0;
}
int DNN::updata_parameters_with_adam(int t)
{
int k=0,i=0,j=0;
int L = _sup_par.layer_dims;
parameters *p = &_par;
grad *g= &_gra;
while(g->pre->pre!=NULL)//反向和前向组合参数的方式不同,前者A=sgn(WX+b),后者 AL=sgn(WA+b)
{
g=g->pre;
}
//learn_rateing *= pow(0.999, t/1000);//指数衰减
for(k=0; k<L-1&&p->next!=NULL&&g!=NULL; k++)
{
for(i=0; i<g->grad_W._row; i++)
{
g->V_db._data[i][0]=(_beta1 * g->V_db._data[i][0] + (1 - _beta1) * g->grad_b._data[i][0]);
g->V_db_corrected._data[i][0] = g->V_db._data[i][0] / (1-pow(_beta1,t));//修正
g->S_db._data[i][0]=(_beta2 * g->S_db._data[i][0] + (1 - _beta2) * (g->grad_b._data[i][0] * g->grad_b._data[i][0]));
g->S_db_corrected._data[i][0] = g->S_db._data[i][0] / (1-pow(_beta2,t));//修正
g->grad_b._data[i][0]= (-_learn_rateing) * g->V_db_corrected._data[i][0] / sqrt(g->S_db_corrected._data[i][0]);
for(j=0; j<g->grad_W._col; j++)
{
g->V_dw._data[i][j]=(_beta1 * g->V_dw._data[i][j] + (1 - _beta1) * g->grad_W._data[i][j]);
g->V_dw_corrected._data[i][j]=g->V_dw._data[i][j] / (1-pow(_beta1,t));//修正
g->S_dw._data[i][j]=(_beta2 * g->S_dw._data[i][j] + (1 - _beta2) * (g->grad_W._data[i][j] * g->grad_W._data[i][j]));
g->S_dw_corrected._data[i][j]=g->S_dw._data[i][j] / (1-pow(_beta2,t));//修正
g->grad_W._data[i][j]= (-_learn_rateing) * g->V_dw_corrected._data[i][j] / sqrt(g->S_dw_corrected._data[i][j] + _epsilon) ;
}
}
p->W = p->W + g->grad_W;
p->b = p->b + g->grad_b;
p=p->next;
g=g->next;
}
return 0;
}
int DNN::updata_parameters(int t)
{
if(_optimizer=="gd")
updata_parameters_with_gd(t);
else if(_optimizer == "momentum")
updata_parameters_with_momentum(t);
else if(_optimizer =="adam")
updata_parameters_with_adam(t);
return 0;
}
int DNN::predict()
{
int i,k;
int L = _sup_par.layer_dims;
//parameters *p;
//p = &_par;
//p->A = _x.copyMatrix();
//Matrix AL;
Matrix AL(_y._row,_y._col,0);
double *keep_probs=new double [L];
for(k=0;k<L;k++)
{
keep_probs[k]=1;
}
AL=model_forward(keep_probs);
for(i=0;i<_y._col;i++)
{
if(AL._data[0][i]>0.5)
AL._data[0][i]=1;
else
AL._data[0][i]=0;
}
double pre=0;
for(i=0;i<_y._col;i++)
{
if((AL._data[0][i]==1 && _y._data[0][i]==1)||(AL._data[0][i]==0 && _y._data[0][i]==0))
pre+=1;
}
pre/=_y._col;
std::cout<<"pre="<<pre<<std::endl;
return 0;
}
DNN::DNN(const std::string &file, const char *optimizer,double learn_rateing,const char *initialization, double lambd, double keep_prob, \
int mini_batch_size,double beta1, double beta2, double epsilon, int iter, bool print_cost)
{
/**
初始化参数
**/
_x.init_by_data(file);
_x = _x.transposeMatrix();
_x.print();
//_x = _x.transposeMatrix();
_y = _x.getOneRow(_x._row-1);
_y.print();
_x.deleteOneRow(_x._row-1);
//_y=one_hot(_y,2);
//_y.print();
std::cout<<"_x:row&col"<<_x._row << _x._col<<std::endl;
std::cout<<"_y:row&col"<<_y._row << _y._col<<std::endl;
_initialization = initialization;
_learn_rateing = learn_rateing;
_optimizer = optimizer;
_beta1 = beta1;
_beta2 = beta2;
_epsilon = epsilon;
_lambd = lambd;
_iter = iter;
_print_cost = print_cost;
_keep_prob = keep_prob;
int i=0,k=0;
int lay_dim=3;
int lay_n[3]= {500,3,1};
lay_n[0]=_x._row;
std::string lay_active[3]= {"relu","relu","sigmoid"};
_sup_par.layer_dims=lay_dim;
for(i=0; i<lay_dim; i++)
{
_sup_par.layer_n[i]=lay_n[i];
_sup_par.layer_active[i]=lay_active[i];
}
init_parameters();
double loss;
//Matrix AL(_y._row,_y._col,0);
Matrix AL(1,_y._col,0);
double *keep_probs;
keep_probs=new double [_sup_par.layer_dims];
if(keep_prob==1)
{
for(k=0;k < _sup_par.layer_dims;k++)
{
keep_probs[k]=1;
}
}
else if (keep_prob<1)
{
for(k=0;k<_sup_par.layer_dims;k++)
{
if(k==0 || k==_sup_par.layer_dims-1)
{
keep_probs[k]=1;
}
else
{
keep_probs[k]=1;
}
}
}
for(i=0; i<iter; i++)
{
std::cout<<"-----------forward------------"<<"i="<<i<<std::endl;
AL=model_forward(keep_probs);
std::cout<<"-----------loss--------------"<<std::endl;
loss=cost_cumpter(AL);
if(i%100==0)
std::cout<<"loss="<<loss<<std::endl;
std::cout<<"-----------backword-----------"<<std::endl;
model_backword(AL,keep_probs);
std::cout<<"-----------update--------------"<<std::endl;
updata_parameters(i+1);
}
AL=model_forward(keep_probs);
std::cout<<"train_end"<<std::endl;
predict();
}
}