forked from yusugomori/DeepLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dA.c
239 lines (192 loc) · 5.38 KB
/
dA.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "dA.h"
#include "utils.h"
void test_dbn(void);
double uniform(double min, double max) {
return rand() / (RAND_MAX + 1.0) * (max - min) + min;
}
int binomial(int n, double p) {
if(p < 0 || p > 1) return 0;
int i;
int c = 0;
double r;
for(i=0; i<n; i++) {
r = rand() / (RAND_MAX + 1.0);
if (r < p) c++;
}
return c;
}
double sigmoid(double x) {
return 1.0 / (1.0 + exp(-x));
}
void dA__construct(dA* this, int N, int n_visible, int n_hidden, \
double **W, double *hbias, double *vbias) {
int i, j;
double a = 1.0 / n_visible;
this->N = N;
this->n_visible = n_visible;
this->n_hidden = n_hidden;
if(W == NULL) {
this->W = (double **)malloc(sizeof(double*) * n_hidden);
this->W[0] = (double *)malloc(sizeof(double) * n_visible * n_hidden);
for(i=0; i<n_hidden; i++) this->W[i] = this->W[0] + i * n_visible;
for(i=0; i<n_hidden; i++) {
for(j=0; j<n_visible; j++) {
this->W[i][j] = uniform(-a, a);
}
}
} else {
this->W = W;
}
if(hbias == NULL) {
this->hbias = (double *)malloc(sizeof(double) * n_hidden);
for(i=0; i<n_hidden; i++) this->hbias[i] = 0;
} else {
this->hbias = hbias;
}
if(vbias == NULL) {
this->vbias = (double *)malloc(sizeof(double) * n_visible);
for(i=0; i<n_visible; i++) this->vbias[i] = 0;
} else {
this->vbias = vbias;
}
}
void dA__destruct(dA* this) {
free(this->W[0]);
free(this->W);
free(this->hbias);
free(this->vbias);
}
void dA_get_corrupted_input(dA* this, int *x, int *tilde_x, double p) {
int i;
for(i=0; i<this->n_visible; i++) {
if(x[i] == 0) {
tilde_x[i] = 0;
} else {
tilde_x[i] = binomial(1, p);
}
}
}
// Encode
void dA_get_hidden_values(dA* this, int *x, double *y) {
int i,j;
for(i=0; i<this->n_hidden; i++) {
y[i] = 0;
for(j=0; j<this->n_visible; j++) {
y[i] += this->W[i][j] * x[j];
}
y[i] += this->hbias[i];
y[i] = sigmoid(y[i]);
}
}
// Decode
void dA_get_reconstructed_input(dA* this, double *y, double *z) {
int i, j;
for(i=0; i<this->n_visible; i++) {
z[i] = 0;
for(j=0; j<this->n_hidden; j++) {
z[i] += this->W[j][i] * y[j];
}
z[i] += this->vbias[i];
z[i] = sigmoid(z[i]);
}
}
void dA_train(dA* this, int *x, double lr, double corruption_level) {
int i, j;
int *tilde_x = (int *)malloc(sizeof(int) * this->n_visible);
double *y = (double *)malloc(sizeof(double) * this->n_hidden);
double *z = (double *)malloc(sizeof(double) * this->n_visible);
double *L_vbias = (double *)malloc(sizeof(double) * this->n_visible);
double *L_hbias = (double *)malloc(sizeof(double) * this->n_hidden);
double p = 1 - corruption_level;
dA_get_corrupted_input(this, x, tilde_x, p);
dA_get_hidden_values(this, tilde_x, y);
dA_get_reconstructed_input(this, y, z);
// vbias
for(i=0; i<this->n_visible; i++) {
L_vbias[i] = x[i] - z[i];
this->vbias[i] += lr * L_vbias[i] / this->N;
}
// hbias
for(i=0; i<this->n_hidden; i++) {
L_hbias[i] = 0;
for(j=0; j<this->n_visible; j++) {
L_hbias[i] += this->W[i][j] * L_vbias[j];
}
L_hbias[i] *= y[i] * (1 - y[i]);
this->hbias[i] += lr * L_hbias[i] / this->N;
}
// W
for(i=0; i<this->n_hidden; i++) {
for(j=0; j<this->n_visible; j++) {
this->W[i][j] += lr * (L_hbias[i] * tilde_x[j] + L_vbias[j] * y[i]) / this->N;
}
}
free(L_hbias);
free(L_vbias);
free(z);
free(y);
free(tilde_x);
}
void dA_reconstruct(dA* this, int *x, double *z) {
int i;
double *y = (double *)malloc(sizeof(double) * this->n_hidden);
dA_get_hidden_values(this, x, y);
dA_get_reconstructed_input(this, y, z);
free(y);
}
void test_dbn(void) {
srand(0);
int i, j, epoch;
double learning_rate = 0.1;
double corruption_level = 0.3;
int training_epochs = 100;
int train_N = 10;
int test_N = 2;
int n_visible = 20;
int n_hidden = 5;
// training data
int train_X[10][20] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0}
};
// construct dA
dA da;
dA__construct(&da, train_N, n_visible, n_hidden, NULL, NULL, NULL);
// train
for(epoch=0; epoch<training_epochs; epoch++) {
for(i=0; i<train_N; i++) {
dA_train(&da, train_X[i], learning_rate, corruption_level);
}
}
// test data
int test_X[2][20] = {
{1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0}
};
double reconstructed_X[2][20];
// test
for(i=0; i<test_N; i++) {
dA_reconstruct(&da, test_X[i], reconstructed_X[i]);
for(j=0; j<n_visible; j++) {
printf("%.5f ", reconstructed_X[i][j]);
}
printf("\n");
}
// destruct dA
dA__destruct(&da);
}
int main(void) {
test_dbn();
return 0;
}