-
Notifications
You must be signed in to change notification settings - Fork 0
/
neunet.c
161 lines (141 loc) · 3.41 KB
/
neunet.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
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "utils.h"
#include "matrix.h"
#include "neunet.h"
#define PI 3.141592653
struct layer net[MAXLAYER];
int nlayer;
double (*lossf)(double *ov, double *tv, int n);
void (*dlossf)(double *dv, double *ov, double *tv, int n);
static double *vbuf; /* temporary vector buffer */
static void propback(double *tv);
static void setdi(struct layer *l, double *dout);
static void update(double lr, double l2, int n);
static double randlf(void);
static void check(struct layer *l);
void addlayer(int n, vfield act, dvfield dact, int xact)
{
struct layer *l;
l = &net[nlayer++];
l->n = n;
l->act = act;
l->dact = dact;
l->xact = xact;
}
void netinit(int train)
{
int i, j, k, max;
max = 0;
for (k = 0; k < nlayer; k++) {
if (net[k].n > max)
max = net[k].n;
CALLOC(net[k].ov, net[k].n);
if (k == 0)
continue;
ALLOC2(net[k].wm, net[k].n, net[k-1].n);
CALLOC(net[k].iv, net[k].n);
CALLOC(net[k].bv, net[k].n);
if (train) {
CALLOC(net[k].di, net[k].n);
CALLOC(net[k].db, net[k].n);
ALLOC2(net[k].dw, net[k].n, net[k-1].n);
if (net[k].xact)
ALLOC2(net[k].jact, net[k].n, net[k].n);
}
}
if (!train)
return;
CALLOC(vbuf, max);
for (k = 1; k < nlayer; k++)
for (i = 0; i < net[k].n; i++) {
for (j = 0; j < net[k-1].n; j++)
net[k].wm[i][j] = randlf();
net[k].bv[i] = randlf();
}
}
void feedfwd(double *iv)
{
int i;
net->act(net->ov, iv, net->n);
for (i = 1; i < nlayer; i++) {
MEMCPY(net[i].iv, net[i].bv, net[i].n);
mxv(net[i].iv, net[i-1].ov, net[i].wm, net[i-1].n, net[i].n);
net[i].act(net[i].ov, net[i].iv, net[i].n);
}
}
void learn(double **iv, double **tv, int n, double lr, double l2)
{
int i;
for (i = 1; i < nlayer; i++) {
MEMSET(net[i].dw[0], 0, net[i-1].n*net[i].n);
MEMSET(net[i].db, 0, net[i].n);
}
for (i = 0; i < n; i++) {
feedfwd(iv[i]);
propback(tv[i]);
}
update(lr, l2, n);
}
static void propback(double *tv)
{
struct layer *ol;
int i, j;
ol = &net[nlayer-1];
dlossf(vbuf, ol->ov, tv, ol->n);
setdi(ol, vbuf);
for (i = nlayer-2; i > 0; i--) {
MEMSET(vbuf, 0, net[i].n);
mtxv(vbuf, net[i+1].di, net[i+1].wm, net[i+1].n, net[i].n);
setdi(&net[i], vbuf);
}
for (i = 1; i < nlayer; i++) {
vxvt(net[i].dw, net[i].di, net[i-1].ov, net[i].n, net[i-1].n);
entadd(net[i].db, net[i].di, net[i].n);
}
}
static void setdi(struct layer *l, double *dout)
{
if (l->xact) {
MEMSET(l->di, 0, l->n);
l->dact(l->jact, l->ov, l->n);
mtxv(l->di, dout, l->jact, l->n, l->n);
} else {
l->dact(l->di, l->ov, l->n);
entmul(l->di, dout, l->n);
}
}
static void update(double lr, double l2, int n)
{
int i;
for (i = 1; i < nlayer; i++) {
nxm(net[i].wm, 1-lr*l2, net[i].n, net[i-1].n);
nxm(net[i].dw, -lr/n, net[i].n, net[i-1].n);
mam(net[i].wm, net[i].dw, net[i].n, net[i-1].n);
nummul(net[i].db, -lr/n, net[i].n);
entadd(net[i].bv, net[i].db, net[i].n);
check(&net[i]);
}
}
/* Generate standard Gaussian random number by Box–Muller transform */
static double randlf(void)
{
double u, v;
u = (rand()+1.0) / (RAND_MAX+2.0);
v = (rand()+1.0) / (RAND_MAX+2.0);
return sqrt(-2*log(u)) * cos(2*PI*v);
}
static void check(struct layer *l)
{
struct layer *p;
int i, j;
p = l - 1;
for (i = 0; i < l->n; i++)
for (j = 0; j < p->n; j++)
if (!isfinite(l->wm[i][j]))
err("Weight float overflow\n");
for (i = 0; i < l->n; i++)
if (!isfinite(l->bv[i]))
err("Bias float overflow\n");
}