-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.c
221 lines (179 loc) · 4.13 KB
/
tests.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <assert.h>
#include "main.h"
void
test_create_val()
{
struct Value a = create_value(-2, "a");
a.grad = 1.0;
assert(a.data == -2);
assert(strcmp(a.label, "a") == 0);
assert(a.grad == 1.0);
}
void
test_add()
{
struct Value a = create_value(-2, "a");
struct Value b = create_value(3, "b");
struct Value c = w_add(&a, &b, "c");
assert(c.data == 1);
}
void
test_sub()
{
struct Value a = create_value(-2, "a");
struct Value b = create_value(3, "b");
struct Value c = w_sub(&a, &b, "c");
assert(c.data == -5);
}
void
test_mul()
{
struct Value a = create_value(-2, "a");
struct Value b = create_value(3, "b");
struct Value c = w_mul(&a, &b, "c");
assert(c.data == -6);
}
void
test_div()
{
struct Value a = create_value(-4, "a");
struct Value b = create_value(2, "b");
struct Value c = w_div(&a, &b, "c");
assert(c.data == -2);
}
void
test_tanh()
{
struct Value a = create_value(2, "a");;
struct Value b = w_tanh(&a, "b");
assert(fabs(b.data - 0.964028) < 1e-6);
}
void
test_relu()
{
struct Value a = create_value(2, "a");
struct Value b = w_relu(&a, "b");
struct Value c = create_value(-2, "c");
struct Value d = w_relu(&c, "d");
assert(b.data == 2);
assert(d.data == 0);
}
void
test_exp()
{
struct Value a = create_value(2, "a");
struct Value b = create_value(-2, "b");
struct Value c = w_exp(&a, "c");
struct Value d = w_exp(&b, "d");
assert(fabs(c.data - 7.389056) < 1e-6);
assert(fabs(d.data - 0.135335) < 1e-6);
}
void
test_pow()
{
struct Value a = create_value(2, "a");
struct Value b = create_value(-2, "b");
struct Value c = w_pow(&a, &b, "c");
assert(fabs(c.data - 0.250000) < 1e-6);
}
void
test_add_backward()
{
struct Value a = create_value(4, "a");
struct Value b = create_value(-2, "b");
struct Value c = w_add(&a, &b, "c");
c.grad = 1.0;
backward(&c);
assert(a.grad == 1);
assert(b.grad == 1);
}
void
test_sub_backward()
{
struct Value a = create_value(4, "a");
struct Value b = create_value(-2, "b");
struct Value c = w_sub(&a, &b, "c");
c.grad = 1.0;
backward(&c);
assert(a.grad == -1);
assert(b.grad == -1);
}
void
test_mul_backward()
{
struct Value a = create_value(4, "a");
struct Value b = create_value(-2, "b");
struct Value c = w_mul(&a, &b, "c");
c.grad = 1.0;
backward(&c);
assert(a.grad == b.data);
assert(b.grad == a.data);
}
void
test_div_backward()
{
struct Value a = create_value(4, "a");
struct Value b = create_value(-2, "b");
struct Value c = w_div(&a, &b, "c");
c.grad = 1.0;
backward(&c);
assert(a.grad == (1.0 / b.data));
assert(b.grad == (-a.data / (b.data * b.data)));
}
void
test_tanh_backward()
{
struct Value a = create_value(4, "a");
struct Value b = w_tanh(&a, "b");
b.grad = 1.0;
backward(&b);
assert(fabs(a.grad - 0.001341) < 1e-6);
}
void
test_exp_backward()
{
struct Value a = create_value(-2, "a");
struct Value b = w_exp(&a, "b");
b.grad = 1.0;
backward(&b);
assert(fabs(a.grad - 0.135335) < 1e-6);
}
void
test_relu_backward()
{
struct Value a = create_value(2, "a");
struct Value b = w_relu(&a, "b");
struct Value c = create_value(-2, "c");
struct Value d = w_relu(&c, "d");
b.grad = 1.0;
d.grad = 1.0;
backward(&b);
backward(&d);
assert(a.grad == 2);
assert(c.grad == 0);
}
void
test_1()
{
struct Value a = create_value(2, "a");
struct Value b = create_value(-2, "b");
struct Value c = w_sub(&a, &b, "c");
struct Value d = w_tanh(&c, "d");
d.grad = 1.0;
size_t graph_size = 0;
get_graph_size(&d, &graph_size);
//printf("%d\n", graph_size);
/* struct Stack stack; */
/* size_t initial_capacity = graph_size; */
/* init_stack(&stack, initial_capacity); */
/* dfs_to_stack(&d, &stack); */
/* double h = 0.001; */
/* gradient_descent(&stack, h, 100); */
/* backward_stack(&stack); */
/* //print_stack(&stack); */
/* cleanup_stack(&stack); */
}