-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_Value.cpp
More file actions
54 lines (48 loc) · 1.31 KB
/
Copy pathtest_Value.cpp
File metadata and controls
54 lines (48 loc) · 1.31 KB
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
#include "Value.h"
void test_sanity_check() {
auto x = std::make_shared<Value>(-4.0);
auto z = 2 * x + 2 + x;
auto q = z->relu() + z * x;
auto h = z->pow(2)->relu();
auto y = h + q + q * x;
y->backward();
auto xmg = x, ymg = y;
assert(ymg->data == -20.0);
assert(xmg->grad == 46.0);
}
void test_more_ops() {
auto a = std::make_shared<Value>(-4.0);
auto b = std::make_shared<Value>(2.0);
auto c = a + b;
auto d = a * b + b->pow(3);
c = c + c + 1;
c = c + 1 + c + (-a);
d = d + d * 2 + (b + a)->relu();
d = d + 3 * d + (b - a)->relu();
auto e = c - d;
auto f = e->pow(2);
auto g = f / 2.0;
g = g + 10.0 / f;
g->backward();
auto amg = a, bmg = b, gmg = g;
double tol = 1e-6;
assert(std::abs(gmg->data - 24.70408163265306) < tol);
assert(std::abs(amg->grad - 138.8338192419825) < tol);
assert(std::abs(bmg->grad - 645.5772594752186) < tol);
}
void test_duplicate_backprop() {
auto a = std::make_shared<Value>(1.0);
auto b = a + 4;
auto c = (b * 3) + (b * 5);
c->backward();
assert(a->grad == 8.0);
assert(b->grad == 8.0);
assert(c->grad == 1.0);
}
int main() {
test_sanity_check();
test_more_ops();
test_duplicate_backprop();
std::cout << "All tests passed!" << std::endl;
return 0;
}