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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <unistd.h>
// #define DEBUG
#define CHECK_SEED 69
#define BIAS 20
#define PASSES 99999
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define WIDTH 60
#define HEIGHT 60
float input[HEIGHT][WIDTH];
float weights[HEIGHT][WIDTH];
int rand_range(int low, int high)
{
assert(low < high);
return rand() % (high - low) + low;
}
void print_weight();
void fill_rect(int x0, int y0, int x1, int x2, float val);
void fill_circle(int cx, int cy, int r, float val);
void gen_rect();
void gen_circle();
void gen_shape(int opt)
{
if(opt == 0) gen_rect();
else if(opt == 1) gen_circle();
else assert(0);
}
void sub_weights();
void add_weights();
void manip_weights(int opt)
{
if(opt == 0) sub_weights();
else if(opt == 1) add_weights();
else assert(0);
}
float get_out()
{
float out_node = 0.0f;
for(int i = 0; i < HEIGHT; i++)
for(int j = 0; j < WIDTH; j++)
{
out_node += (weights[i][j] * input[i][j]);
}
return out_node;
}
void train()
{
memset(input, 0, HEIGHT*WIDTH*4);
int opt = rand_range(0, 2);
gen_shape(opt);
float out = get_out();
if((out > BIAS) != opt)
manip_weights(opt);
#ifdef DEBUG
printf("OUT %f", out);
print_all();
#endif
}
int check()
{
memset(input, 0, HEIGHT*WIDTH*4);
int opt = rand_range(0, 2);
gen_shape(opt);
float out = get_out();
if((out > BIAS) != opt)
return 0;
return 1;
}
int main()
{
memset(input, 0, HEIGHT*WIDTH*4);
memset(weights, 0, HEIGHT*WIDTH*4);
// ----- Train -----//
time_t cur_time = time(0);
puts("[INFO] Training model");
srand(time(0));
for(size_t i = 0; i < PASSES; i++) {
train();
#ifdef DEBUG
sleep(5);
#endif
}
for(size_t i = 0; i < PASSES; i++) {
train();
#ifdef DEBUG
sleep(5);
#endif
}
puts("[INFO] Training completed");
printf("[INFO] Operation took %ld seconds\n", time(0) - cur_time);
// ----- Check ----- //
cur_time = time(0);
puts("[INFO] Checking model success rate");
srand(CHECK_SEED);
int correct = 0;
for(size_t i = 0; i < PASSES; i++) {
correct += check();
}
printf("[INFO] Model success rate is %f%%\n", ((float)correct/PASSES)*100);
printf("[INFO] Operation took %ld seconds\n", time(0) - cur_time);
#ifdef DEBUG
print_weight();
#endif
return 0;
}
void print_weight()
{
puts("------------------------------------------------------------");
for(int i = 0; i < HEIGHT; i++)
{
for(int j = 0; j < WIDTH; j++)
{
if(weights[i][j] == 0.0f)
printf(".");
else if(weights[i][j] < 0.0f)
printf("@");
else
printf("#");
}
puts("");
}
puts("------------------------------------------------------------");
}
void fill_rect(int x0, int y0, int x1, int y1, float val)
{
assert(x0 >= 0); assert(x1 >= 0);
assert(x0 < WIDTH); assert(x1 < WIDTH);
assert(y0 >= 0); assert(y1 >= 0);
assert(y0 < HEIGHT); assert(y1 < HEIGHT);
assert(x0 <= x1); assert(y0 <= y1);
for(int y = y0; y <= y1; y++)
{
for(int x = x0; x <= x1; x++)
{
input[y][x] = val;
}
}
}
void fill_circle(int cx, int cy, int r, float val)
{
assert(r > 0);
int x0 = cx - r; assert(x0 >= 0); assert(x0 < WIDTH);
int y0 = cy - r; assert(y0 >= 0); assert(y0 < HEIGHT);
int x1 = cx + r; assert(x1 >= 0); assert(x1 < WIDTH);
int y1 = cy + r; assert(y1 >= 0); assert(y1 < HEIGHT);
for (int y = y0; y <= y1; ++y)
for (int x = x0; x <= x1; ++x)
{
int dx = x - cx;
int dy = y - cy;
if (dx*dx + dy*dy <= r*r)
input[y][x] = val;
}
}
void gen_rect()
{
int x0 = rand_range(0, WIDTH);
int y0 = rand_range(0, HEIGHT);
int x1 = rand_range(x0, WIDTH);
int y1 = rand_range(y0, HEIGHT);
fill_rect(x0, y0, x1, y1, 1.0f);
}
void gen_circle()
{
int cx = rand_range(2, WIDTH-2);
int cy = rand_range(2, HEIGHT-2);
int r = MIN(
MIN(cx, WIDTH - cx),
MIN(cy, HEIGHT - cy));
r = rand_range(1, r);
fill_circle(cx, cy, r, 1.0f);
}
void sub_weights()
{
for(int i = 0; i < HEIGHT; i++)
for(int j = 0; j < WIDTH; j++)
{
weights[i][j] -= input[i][j];
}
}
void add_weights()
{
for(int i = 0; i < HEIGHT; i++)
for(int j = 0; j < WIDTH; j++)
{
weights[i][j] += input[i][j];
}
}
|