forked from tsoding/nn.h
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gym.c
240 lines (208 loc) · 7.57 KB
/
gym.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
// Gym is a GUI app that trains your NN on the data you give it.
// The idea is that it will spit out a binary file that can be
// then loaded up with nn.h and used in your application.
#include <assert.h>
#include <stdio.h>
#include <limits.h>
#include <float.h>
#include "raylib.h"
#define SV_IMPLEMENTATION
#include "sv.h"
#define NN_IMPLEMENTATION
#include "nn.h"
typedef int Errno;
#define WINDOW_FACTOR 80
#define WINDOW_WIDTH (16*WINDOW_FACTOR)
#define WINDOW_HEIGHT (9*WINDOW_FACTOR)
typedef struct {
size_t *items;
size_t count;
size_t capacity;
} Arch;
typedef struct {
float *items;
size_t count;
size_t capacity;
} Cost_Plot;
#define DA_INIT_CAP 256
#define da_append(da, item) \
do { \
if ((da)->count >= (da)->capacity) { \
(da)->capacity = (da)->capacity == 0 ? DA_INIT_CAP : (da)->capacity*2; \
(da)->items = realloc((da)->items, (da)->capacity*sizeof(*(da)->items)); \
assert((da)->items != NULL && "Buy more RAM lol"); \
} \
\
(da)->items[(da)->count++] = (item); \
} while (0)
char *args_shift(int *argc, char ***argv)
{
assert(*argc > 0);
char *result = **argv;
(*argc) -= 1;
(*argv) += 1;
return result;
}
void nn_render_raylib(NN nn, float rx, float ry, float rw, float rh)
{
Color low_color = {0xFF, 0x00, 0xFF, 0xFF};
Color high_color = {0x00, 0xFF, 0x00, 0xFF};
float neuron_radius = rh*0.03;
float layer_border_vpad = rh*0.08;
float layer_border_hpad = rw*0.06;
float nn_width = rw - 2*layer_border_hpad;
float nn_height = rh - 2*layer_border_vpad;
float nn_x = rx + rw/2 - nn_width/2;
float nn_y = ry + rh/2 - nn_height/2;
size_t arch_count = nn.count + 1;
float layer_hpad = nn_width / arch_count;
for (size_t l = 0; l < arch_count; ++l) {
float layer_vpad1 = nn_height / nn.as[l].cols;
for (size_t i = 0; i < nn.as[l].cols; ++i) {
float cx1 = nn_x + l*layer_hpad + layer_hpad/2;
float cy1 = nn_y + i*layer_vpad1 + layer_vpad1/2;
if (l+1 < arch_count) {
float layer_vpad2 = nn_height / nn.as[l+1].cols;
for (size_t j = 0; j < nn.as[l+1].cols; ++j) {
// i - rows of ws
// j - cols of ws
float cx2 = nn_x + (l+1)*layer_hpad + layer_hpad/2;
float cy2 = nn_y + j*layer_vpad2 + layer_vpad2/2;
float value = sigmoidf(MAT_AT(nn.ws[l], i, j));
high_color.a = floorf(255.f*value);
float thick = rh*0.004f;
Vector2 start = {cx1, cy1};
Vector2 end = {cx2, cy2};
DrawLineEx(start, end, thick, ColorAlphaBlend(low_color, high_color, WHITE));
}
}
if (l > 0) {
high_color.a = floorf(255.f*sigmoidf(MAT_AT(nn.bs[l-1], 0, i)));
DrawCircle(cx1, cy1, neuron_radius, ColorAlphaBlend(low_color, high_color, WHITE));
} else {
DrawCircle(cx1, cy1, neuron_radius, GRAY);
}
}
}
}
void plot_cost(Cost_Plot plot, int rx, int ry, int rw, int rh)
{
float min = FLT_MAX, max = FLT_MIN;
for (size_t i = 0; i < plot.count; ++i) {
if (max < plot.items[i]) max = plot.items[i];
if (min > plot.items[i]) min = plot.items[i];
}
if (min > 0) min = 0;
size_t n = plot.count;
if (n < 1000) n = 1000;
for (size_t i = 0; i+1 < plot.count; ++i) {
float x1 = rx + (float)rw/n*i;
float y1 = ry + (1 - (plot.items[i] - min)/(max - min))*rh;
float x2 = rx + (float)rw/n*(i+1);
float y2 = ry + (1 - (plot.items[i+1] - min)/(max - min))*rh;
DrawLineEx((Vector2){x1, y1}, (Vector2){x2, y2}, rh*0.005, RED);
}
float y0 = ry + (1 - (0 - min)/(max - min))*rh;
DrawLineEx((Vector2){rx + 0, y0}, (Vector2){rx + rw - 1, y0}, rh*0.005, WHITE);
DrawText("0", rx + 0, y0 - rh*0.04, rh*0.04, WHITE);
}
int main(int argc, char **argv)
{
const char *program = args_shift(&argc, &argv);
if (argc <= 0) {
fprintf(stderr, "Usage: %s <model.arch> <model.mat>\n", program);
fprintf(stderr, "ERROR: no architecture file was provided\n");
return 1;
}
const char *arch_file_path = args_shift(&argc, &argv);
if (argc <= 0) {
fprintf(stderr, "Usage: %s <model.arch> <model.mat>\n", program);
fprintf(stderr, "ERROR: no data file was provided\n");
return 1;
}
const char *data_file_path = args_shift(&argc, &argv);
unsigned int buffer_len = 0;
unsigned char *buffer = LoadFileData(arch_file_path, &buffer_len);
if (buffer == NULL) {
return 1;
}
String_View content = sv_from_parts((const char*)buffer, buffer_len);
Arch arch = {0};
content = sv_trim_left(content);
while (content.count > 0 && isdigit(content.data[0])) {
size_t x = sv_chop_u64(&content);
da_append(&arch, x);
content = sv_trim_left(content);
}
FILE *in = fopen(data_file_path, "rb");
if (in == NULL) {
fprintf(stderr, "ERROR: could not read file %s\n", data_file_path);
return 1;
}
Mat t = mat_load(in);
fclose(in);
NN_ASSERT(arch.count > 1);
size_t ins_sz = arch.items[0];
size_t outs_sz = arch.items[arch.count-1];
NN_ASSERT(t.cols == ins_sz + outs_sz);
Mat ti = {
.rows = t.rows,
.cols = ins_sz,
.stride = t.stride,
.es = &MAT_AT(t, 0, 0),
};
Mat to = {
.rows = t.rows,
.cols = outs_sz,
.stride = t.stride,
.es = &MAT_AT(t, 0, ins_sz),
};
NN nn = nn_alloc(arch.items, arch.count);
NN g = nn_alloc(arch.items, arch.count);
nn_rand(nn, -1, 1);
NN_PRINT(nn);
float rate = 1;
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "gym");
SetTargetFPS(60);
Cost_Plot plot = {0};
size_t epoch = 0;
size_t max_epoch = 10000;
size_t epochs_per_frame = 103;
bool paused = true;
while (!WindowShouldClose()) {
if (IsKeyPressed(KEY_SPACE)) {
paused = !paused;
}
if (IsKeyPressed(KEY_R)) {
epoch = 0;
nn_rand(nn, -1, 1);
plot.count = 0;
}
for (size_t i = 0; i < epochs_per_frame && !paused && epoch < max_epoch; ++i) {
nn_backprop(nn, g, ti, to);
nn_learn(nn, g, rate);
epoch += 1;
da_append(&plot, nn_cost(nn, ti, to));
}
BeginDrawing();
Color background_color = {0x18, 0x18, 0x18, 0xFF};
ClearBackground(background_color);
{
int w = GetRenderWidth();
int h = GetRenderHeight();
int rw = w/2;
int rh = h*2/3;
int rx = 0;
int ry = h/2 - rh/2;
plot_cost(plot, rx, ry, rw, rh);
rx += rw;
nn_render_raylib(nn, rx, ry, rw, rh);
char buffer[256];
snprintf(buffer, sizeof(buffer), "Epoch: %zu/%zu, Rate: %f, Cost: %f", epoch, max_epoch, rate, nn_cost(nn, ti, to));
DrawText(buffer, 0, 0, h*0.04, WHITE);
}
EndDrawing();
}
return 0;
}