-
Notifications
You must be signed in to change notification settings - Fork 0
/
xor.c
247 lines (207 loc) · 4.96 KB
/
xor.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
// this simple NN use 3 neurons, and 2 layers -> OR, NAND first layer,
// AND second layer
typedef struct{
/*FIRST LAYER*/
//OR neuron
float or_w1;
float or_w2;
float or_b;
//NAND neuron
float nand_w1;
float nand_w2;
float nand_b;
/*SECOND LAYER*/
//AND neuron
float and_w1;
float and_w2;
float and_b;
} Xor;
typedef float sample[3];
//XOR-gate
sample xor_train[] = {
{0,0,0},
{1,0,1},
{0,1,1},
{1,1,0},
};
//OR-gate
sample or_train[] = {
{0,0,0},
{1,0,1},
{0,1,1},
{1,1,1},
};
//NAND-gate
sample nand_train[] = {
{0,0,1},
{1,0,1},
{0,1,1},
{1,1,0},
};
//AND-gate
sample and_train[] = {
{0,0,0},
{1,0,0},
{0,1,0},
{1,1,1},
};
sample *train = xor_train;
size_t train_count = 4;
//we need to "squish" the results, so they are not unbounded
//so we use a sigmoid function
float sigmoidf(float x)
{
return 1.f / (1.f + expf(-x));
}
float forward(Xor m, float x1, float x2)
{
float a = sigmoidf(m.or_w1*x1 + m.or_w2*x2 + m.or_b);
float b = sigmoidf(m.nand_w1*x1 + m.nand_w2*x2 * m.nand_b);
return sigmoidf(a*m.and_w1 + b*m.and_w2 + m.and_b);
}
//cost function
float cost(Xor m){
float result = 0.0f;
for (size_t i = 0; i < train_count; ++i){
float x1 = train[i][0];
float x2 = train[i][1];
float y = forward(m, x1, x2);
float d = y - train[i][2];
result += d*d;
}
result /= train_count;
return result;
}
float rand_float(void)
{
return (float) rand()/ (float) RAND_MAX;
}
// x1, x2 are the inputs
/*first layer*/
Xor rand_xor(void)
{
Xor m;
m.or_w1 = rand_float();
m.or_w2 = rand_float();
m.or_b = rand_float();
m.nand_w1 = rand_float();
m.nand_w2 = rand_float();
m.nand_b = rand_float();
m.and_w1 = rand_float();
m.and_w2 = rand_float();
m.and_b = rand_float();
return m;
}
void print_xor(Xor m)
{
printf("or_w1 = %f\n", m.or_w1);
printf("or_w2 = %f\n", m.or_w2);
printf("or_b = %f\n", m.or_b);
printf("nand_w1 = %f\n", m.nand_w1);
printf("nand_w2 = %f\n", m.nand_w2);
printf("nand_b = %f\n", m.nand_b);
printf("and_w1 = %f\n", m.and_w1);
printf("and_w2 = %f\n", m.and_w2);
printf("and_b = %f\n", m.and_b);
}
Xor learn(Xor m, Xor g, float rate)
{
m.or_w1 -= rate*g.or_w1;
m.or_w2 -= rate*g.or_w2;
m.or_b -= rate*g.or_b;
m.nand_w1 -= rate*g.nand_w1;
m.nand_w2 -= rate*g.nand_w2;
m.nand_b -= rate*g.nand_b;
m.and_w1 -= rate*g.and_w1;
m.and_w2 -= rate*g.and_w2;
m.and_b -= rate*g.and_b;
return m;
}
Xor finite_diff(Xor m, float eps)
{
Xor g;
float c = cost(m);
float saved;
saved = m.or_w1;
m.or_w1 += eps;
g.or_w1 = (cost(m) - c)/eps;
m.or_w1 = saved;
saved = m.or_w2;
m.or_w2 += eps;
g.or_w2 = (cost(m) - c)/eps;
m.or_w2 = saved;
saved = m.or_b;
m.or_b += eps;
g.or_b = (cost(m) - c)/eps;
m.or_b = saved;
saved = m.nand_w1;
m.nand_w1 += eps;
g.nand_w1 = (cost(m) - c)/eps;
m.nand_w1 = saved;
saved = m.nand_w2;
m.nand_w2 += eps;
g.nand_w2 = (cost(m) - c)/eps;
m.nand_w2 = saved;
saved = m.nand_b;
m.nand_b += eps;
g.nand_b = (cost(m) - c)/eps;
m.nand_b = saved;
saved = m.and_w1;
m.and_w1 += eps;
g.and_w1 = (cost(m) - c)/eps;
m.and_w1 = saved;
saved = m.and_w2;
m.and_w2 += eps;
g.and_w2 = (cost(m) - c)/eps;
m.and_w2 = saved;
saved = m.and_b;
m.and_b += eps;
g.and_b = (cost(m) - c)/eps;
m.and_b = saved;
return g;
}
int main(void)
{
srand(time(0));
Xor m = rand_xor();
float eps = 1e-1;
float rate = 1e-1;
for (size_t i = 0; i < 1000*1000; ++i){
Xor g = finite_diff(m, eps);
m = learn(m, g, rate);
//printf("cost = %f\n", cost(m));
}
printf("cost = %f\n", cost(m));
printf("------------------------------\n");
for (size_t i = 0; i < 2; ++i) {
for (size_t j = 0; j < 2; ++j) {
printf("%zu ^ %zu = %f\n", i, j, forward(m, i, j));
}
}
printf("------------------------------\n");
printf("\"OR\" neuron:\n");
for (size_t i = 0; i < 2; ++i) {
for (size_t j = 0; j < 2; ++j) {
printf("%zu | %zu = %f\n", i, j, sigmoidf(m.or_w1*i + m.or_w2*j + m.or_b));
}
}
printf("------------------------------\n");
printf("\"NAND\" neuron:\n");
for (size_t i = 0; i < 2; ++i) {
for (size_t j = 0; j < 2; ++j) {
printf("~(%zu & %zu) = %f\n", i, j, sigmoidf(m.nand_w1*i + m.nand_w2*j + m.nand_b));
}
}
printf("------------------------------\n");
printf("\"AND\" neuron:\n");
for (size_t i = 0; i < 2; ++i) {
for (size_t j = 0; j < 2; ++j) {
printf("%zu & %zu = %f\n", i, j, sigmoidf(m.and_w1*i + m.and_w2*j + m.and_b));
}
}
return 0;
}