-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBPNetwork.c
614 lines (553 loc) · 13 KB
/
BPNetwork.c
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#include"BPNetwork.h"
#include<stdlib.h>
#include"rand.h"
#include<math.h>
#include<stdio.h>
//////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////优化日记/////////////////////////////////////
//1、增加动量项
//2、学习速率可调
struct netnode **network=NULL;//神经网络的节点参数二维动态数组
struct netnode **network0 = NULL;//神经网络的节点参数二维动态数组
int *nodes_all=NULL;
int layer_all=0;
long maxepoch;
float xminmax[ROW_X][2];
float yminmax[ROW_Y][2];
float momentum_w=0;//增加动量项
float momentum_b = 0;//增加动量项
float mseerror = 0;//误差
float error0 = 100000;//上一个误差0
float speed = 0.001;//学习速率
float rate_w = 0.0;//动量项系数
float rate_b = 0.0;//动量项系数
float rate_w0 = 0.7;//动量项系数
float rate_b0 = 0.7;//动量项系数
float rate_up = 0.00001;//学习速率上升速率
float rate_down =0.00001;//学习速率下降速率
void networkTrain(float x[ROW_X][COL_X], float y[ROW_X][COL_X], int layer, int nodes[], int mode1)
{
//首先,建立神经网络节点
if (netSetup(ROW_X, layer, nodes, ROW_Y))
{
// error("网络节点建立失败");
}
//然后,初始化参数
if (netInit())
{
// error("网络参数初始化失败");
}
//数据预处理,映射
minmax(x, y,xminmax,yminmax, mode1);
//然后进入迭代求解
maxepoch = 10000;
netSolve(x, y, maxepoch, 0.01, mode1);
//输出参数
if (OutPut())
{
printf("输出参数失败\n");
}
else
{
printf("输出参数成功\n");
}
//释放内存
if (freeNet())
{
printf("内存释放失败\n");
}
else
{
printf("内存释放成功\n");
}
}
//网络参数初始化
int netInit(void)
{
int i = 0, j = 0,k=0;
for (i = 0; i < layer_all; i++)
{
for (j = 0; j < nodes_all[i]; j++)
{
network[i][j].b = random() / 65535.0 - 0.5;
network[i][j].b_change = random() / 65535.0 - 0.5;
network0[i][j].b_change = 0;
if (i != layer_all - 1)
{
network[i][j].w = (double*)malloc(sizeof(double)*nodes_all[i + 1]);
network0[i][j].w = (double*)malloc(sizeof(double)*nodes_all[i + 1]);
network[i][j].w_change = (double*)malloc(sizeof(double)*nodes_all[i + 1]);
network0[i][j].w_change = (double*)malloc(sizeof(double)*nodes_all[i + 1]);
}
}
}
for (i = 0; i < layer_all; i++)
{
for (j = 0; j < nodes_all[i]; j++)
{
if (i != layer_all - 1)
{
for (k = 0; k < nodes_all[i + 1]; k++)
{
network[i][j].w[k] = random() / 65535.0 - 0.5;;
network[i][j].w_change[k] = random() / 65535.0 - 0.5;
network0[i][j].w_change[k] = 0;
}
}
}
}
return 0;
}
//神经网络建立结构节点函数
//输入参数,输入层节点个数num_x,隐含层层数layer,隐含层每层节点数nodes,以及输出层节点数num_y
int netSetup(int num_x, int layer, int nodes[], int num_y)
{
int i = 0;
network = (struct netnode**)malloc(sizeof(struct netnode*)*(layer + 2));//申请行数
network0 = (struct netnode**)malloc(sizeof(struct netnode*)*(layer + 2));//申请行数
for (i = 0; i < layer + 2; i++)
{
if (i == 0)
{
network[i] = (struct netnode*)malloc(sizeof(struct netnode)*num_x);//申请每行列数
network0[i] = (struct netnode*)malloc(sizeof(struct netnode)*num_x);//申请每行列数
}
else if (i == layer + 1)
{
network[i] = (struct netnode*)malloc(sizeof(struct netnode)*num_y);//申请每行列数
network0[i] = (struct netnode*)malloc(sizeof(struct netnode)*num_y);//申请每行列数
}
else
{
network[i] = (struct netnode*)malloc(sizeof(struct netnode)*nodes[i - 1]);//申请每行列数
network0[i] = (struct netnode*)malloc(sizeof(struct netnode)*nodes[i - 1]);//申请每行列数
}
}
int j = 0;
for (i = 0; i < layer + 2; i++)
{
if (network[i]== NULL)
return 1;
}
nodes_all = (int*)malloc(sizeof(int)*(layer + 2));
for (i = 0; i < layer + 2; i++)
{
if (i == 0)
nodes_all[i] = ROW_X;
else if (i==layer+1)
nodes_all[i] = ROW_Y;
else
nodes_all[i] = nodes[i-1];
}
layer_all = layer + 2;
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
//神经网络求解函数
//步骤如下:
//第一,清零累积更新
//第二步,循环所有样本
void netSolve(float x[ROW_X][COL_X], float y[ROW_X][COL_X], long epoch_max, float error_min, int mode1)
{
int epoch = 0;
float error = 0;
while (epoch < epoch_max)
{
if (clearChange())
{
;
}
error=calAll(x, y,mode1,epoch);
printf("误差 %f\n",error/COL_X*2);
epoch++;
}
}
//清零累积更新
int clearChange(void)
{
int i = 0,j=0,k=0;
for (i = 0; i < layer_all; i++)
{
for (j = 0; j < nodes_all[i]; j++)
{
network[i][j].b_change = 0;
if (i != layer_all - 1)
{
for (k = 0; k < nodes_all[i + 1]; k++)
{
network[i][j].w_change[k] = 0;
}
}
}
}
return 0;
}
//循环所有样本
float calAll(float x[ROW_X][COL_X], float y[ROW_X][COL_X], int mode1, long epoch)
{
int i = 0,j=0,k=0;
float error = 0;
for (k = 0; k < COL_X; k++)
{
//计算输入输出
calInputAndOutPut(x,y,k,mode1);
//计算误差
error += calError(y, k,mode1,epoch);
//计算反向过程
bachward(y,k,mode1);
}
mseerror = error;
updata();
error0 = mseerror;
return error;
}
//计算输入输出
int calInputAndOutPut(float x[ROW_X][COL_X], float y[ROW_X][COL_X],int k,int mode1)
{
int i = 0, j = 0,m=0;
double input = 0;
for (i = 0; i < layer_all; i++)
{
//如果是输入层
if (i == 0)
{
for (j = 0; j < nodes_all[i]; j++)
{
network[i][j].input = x[j][k];
network[i][j].output = calInspirit(network[i][j].input, network[i][j].b,mode1);
}
}
//如果是其他层
else
{
for (j = 0; j < nodes_all[i]; j++)
{
input = 0;
for (m = 0; m < nodes_all[i - 1]; m++)
{
input += network[i - 1][m].output*network[i - 1][m].w[j];
}
network[i][j].input = input;
//如果不是输出层
if (i != layer_all - 1)
network[i][j].output = calInspirit(network[i][j].input, network[i][j].b,mode1);
else
network[i][j].output = network[i][j].input;
}
}
}
return 0;
}
//mode1==0,激励函数(sig)
//y=1/(1+exp(x))
//mode1==1,激励函数(正切tansig)
//y=2/(1+e(-2x))-1
double calInspirit(double x, double b, int mode1)
{
double y = 0;
x -= b;
if (mode1 == 0)
{
y = 1 + exp(-x);
y = 1 / y;
}
else if (mode1 == 1)
{
y = 1 + exp(-2 * x);
y = 2 / y - 1;
}
return y;
}
//计算误差函数
float calError(float y[ROW_X][COL_X], int k, int mode1, long epoch)
{
int i = 0;
float error1 = 0;
for (i = 0; i < ROW_Y; i++)
{
error1 += pow((recoverOutput(network[layer_all - 1][i].output, i, mode1) - recoverOutput(y[i][k], i, mode1)), 2);
if (epoch>maxepoch-10)
printf("理想输出: %f , 实际输出: %f ", recoverOutput(y[i][k], i, mode1), recoverOutput(network[layer_all - 1][i].output, i, mode1));
}
if (epoch>maxepoch - 10)
printf("\n");
error1 /= 2;
return error1;
}
//计算反向更新
int bachward(float y[ROW_X][COL_X], int k,int mode1)
{
int i = 0, j = 0,m=0;
double s1 = 0, dy = 0;
//计算累积分量
for (i = layer_all-1; i >=0; i--)
{
for (j = 0; j < nodes_all[i]; j++)
{
//如果是输出层
if (i == layer_all - 1)
{
s1 = network[i][j].output - y[j][k];
network[i][j].s1 = s1;
}
//如果是其他层
else
{
s1 = 0;
for (m = 0; m < nodes_all[i + 1];m++)
{
if (mode1 == 0)
dy = network[i][j].output*(1 - network[i][j].output);
else
dy = 1 - network[i][j].output*network[i][j].output;
s1 += network[i+1][m].s1*network[i][j].w[m]*dy;
}
network[i][j].s1 = s1;
}
}
}
//计算w累积更新
//从输出层的前一层开始
double w1 = 0, d1 = 0, b1 = 0;
for (i = layer_all - 2; i >= 0; i--)
{
for (j = 0; j < nodes_all[i]; j++)
{
for (m = 0; m < nodes_all[i + 1]; m++)
{
w1 = network[i + 1][m].s1*network[i][j].output;
network[i][j].w_change[m] += w1;
}
network[i][j].b_change += network[i][j].s1;
}
}
return 0;
}
int updata()
{
int i = 0, j = 0,m=0;
float s = 0;
for (i = 0; i < layer_all - 1; i++)
{
for (j = 0; j < nodes_all[i]; j++)
{
s = (speed*network[i][j].b_change / COL_X);
network[i][j].b += s + rate_b*network0[i][j].b_change;
network0[i][j].b_change = s + rate_b*network0[i][j].b_change;
for (m = 0; m < nodes_all[i + 1]; m++)
{
s = (speed*network[i][j].w_change[m] / COL_X);
network[i][j].w[m] -= s + rate_w*network0[i][j].w_change[m];
network0[i][j].w_change[m] = s + rate_w*network0[i][j].w_change[m];
}
}
}
//更新学习速率
if (mseerror<error0)
{
speed *= (1+rate_up);
savedata(0);
rate_b = rate_b0;
rate_w = rate_w0;
}
else if (mseerror>error0)
{
speed *= (1-rate_down);
savedata(1);
rate_b = 0;
rate_w = 0;
}
return 0;
}
//保存参数
int savedata(int mode)
{
int i = 0, j = 0, k = 0;
for (i = 0; i < layer_all; i++)
{
if (i != layer_all - 1)
{
for (j = 0; j < nodes_all[i]; j++)
{
if (mode == 0)
{
network0[i][j].b = network[i][j].b;
for (k = 0; k < nodes_all[i + 1]; k++)
{
network0[i][j].w[k] = network[i][j].w[k];
}
}
else if (mode == 1)
{
network[i][j].b = network0[i][j].b;
for (k = 0; k < nodes_all[i + 1]; k++)
{
network[i][j].w[k] = network0[i][j].w[k];
}
}
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
void minmax(float x[ROW_X][COL_X], float y[ROW_Y][COL_Y], float xminmax[ROW_Y][2],float yminmax[ROW_Y][2], int mode1)
{
int i = 0, j = 0;
float min = 10000, max = -10000,dis=0;
//处理x
for (i = 0; i < ROW_X; i++)
{
for (j = 0; j < COL_X; j++)
{
if (min>x[i][j])
min = x[i][j];
if (max<x[i][j])
max = x[i][j];
}
dis = max - min;
xminmax[i][0] = min;
xminmax[i][1] = max;
for (j = 0; j < COL_X; j++)
{
x[i][j] -= min;
x[i][j] /= dis;
if (mode1 == 1)
//压缩到-1到1
{
x[i][j] *= 2;
x[i][j] -= 1;
}
}
}
//处理y
min = 10000;
max = -10000;
for (i = 0; i < ROW_Y; i++)
{
for (j = 0; j < COL_Y; j++)
{
if (min>y[i][j])
min = y[i][j];
if (max < y[i][j])
max = y[i][j];
}
dis = max - min;
yminmax[i][0] = min;
yminmax[i][1] = max;
for (j = 0; j < COL_Y; j++)
{
y[i][j] -= min;
y[i][j] /= dis;
if (mode1 == 1)
{
y[i][j] *= 2;
y[i][j] -= 1;
}
}
}
}
float recoverOutput(float output, int j,int mode1)
{
float dis = yminmax[j][1] - yminmax[j][0];
if (mode1 == 1)
//压缩到0-1区间
{
output += 1;
output /= 2;
}
output *= dis;
output += yminmax[j][0];
return output;
}
int freeNet(void)
{
int i = 0, j = 0;
for (i = 0; i < layer_all; i++)
{
for (j = 0; j < nodes_all[i]; j++)
{
if (i != layer_all - 1)
{
free(network[i][j].w);
free(network[i][j].w_change);
network[i][j].w = NULL;
free(network0[i][j].w_change);
network[i][j].w_change = NULL;
free(network0[i][j].w);
network0[i][j].w = NULL;
if (network[i][j].w != NULL)
return 1;
}
}
free(network[i]);
free(network0[i]);
network[i] = NULL;
network0[i] = NULL;
if (!(network[i] == NULL))
return 1;
}
free(network);
network = NULL;
free(network0);
network0 = NULL;
if (!(network==NULL))
return 1;
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
//参数输出
//创建一个txt文件,向里面写参数
int OutPut(void)
{
//打开data.txt,如果没有就创建
FILE *fpwrite = fopen("data.txt","w");
if (fpwrite == NULL)
{
printf("打开文件失败\n");
return 1;
}
//开始写文件(每写一个数,空一小格)
//写入神经网络层数
fprintf(fpwrite,"%.8f\n",layer_all*1.0);
//写入神经网络每层的网络节点数
int i = 0;
for (i = 0; i < layer_all; i++)
{
fprintf(fpwrite,"%.8f\n",nodes_all[i]*1.0);
}
int j = 0, k = 0;
//写入数据映射的数据
for (i = 0; i < ROW_X; i++)
{
fprintf(fpwrite, "%.8f\n", xminmax[i][0] * 1.0);//min
fprintf(fpwrite, "%.8f\n", xminmax[i][1] * 1.0);//max
}
for (i = 0; i < ROW_Y; i++)
{
fprintf(fpwrite, "%.8f\n", yminmax[i][0] * 1.0);//min
fprintf(fpwrite, "%.8f\n", yminmax[i][1] * 1.0);//max
}
//写入每个节点的参数
//遍历神经网络每层每个节点,输出层除外
for (i = 0; i < layer_all - 1; i++)
{
for (j = 0; j < nodes_all[i]; j++)
{
//写入w
for (k = 0; k < nodes_all[i + 1]; k++)
{
fprintf(fpwrite, "%.8f\n", network[i][j].w[k]);
}
//写入b
fprintf(fpwrite, "%.8f\n", network[i][j].b);
}
}
//关闭文件
fclose(fpwrite);
return 0;
}