-
Notifications
You must be signed in to change notification settings - Fork 0
/
value_main.cpp
180 lines (159 loc) · 5.84 KB
/
value_main.cpp
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
#include "value.h"
int main() {
{
auto a = std::make_shared<Value>(1.0f);
auto b = std::make_shared<Value>(2.0f);
auto c = std::make_shared<Value>(3.0f);
auto d = std::make_shared<Value>(4.0f);
auto e = a + b;
auto f = c * d;
auto g = tanh(e + f);
g->backward();
std::cout << "a|data=" << a->data << "|grad=" << a->grad << std::endl;
std::cout << "b|data=" << b->data << "|grad=" << b->grad << std::endl;
std::cout << "c|data=" << c->data << "|grad=" << c->grad << std::endl;
std::cout << "d|data=" << d->data << "|grad=" << d->grad << std::endl;
std::cout << "e|data=" << e->data << "|grad=" << e->grad << std::endl;
std::cout << "f|data=" << f->data << "|grad=" << f->grad << std::endl;
std::cout << "g|data=" << g->data << "|grad=" << g->grad << std::endl;
}
{
auto x1 = std::make_shared<Value>(2.0f);
auto x2 = std::make_shared<Value>(0.0f);
auto w1 = std::make_shared<Value>(-3.0f);
auto w2 = std::make_shared<Value>(1.0f);
auto b = std::make_shared<Value>(6.881373587f);
auto x1w1 = x1 * w1;
auto x2w2 = x2 * w2;
auto x1w1x2w2 = x1w1 + x2w2;
auto n = x1w1x2w2 + b;
auto o = tanh(n);
o->backward();
std::cout << "x1|data=" << x1->data << "|grad=" << x1->grad << std::endl;
std::cout << "x2|data=" << x2->data << "|grad=" << x2->grad << std::endl;
std::cout << "w1|data=" << w1->data << "|grad=" << w1->grad << std::endl;
std::cout << "w2|data=" << w2->data << "|grad=" << w2->grad << std::endl;
std::cout << "b|data=" << b->data << "|grad=" << b->grad << std::endl;
std::cout << "x1w1|data=" << x1w1->data << "|grad=" << x1w1->grad << std::endl;
std::cout << "x2w2|data=" << x2w2->data << "|grad=" << x2w2->grad << std::endl;
std::cout << "x1w1x2w2|data=" << x1w1x2w2->data << "|grad=" << x1w1x2w2->grad << std::endl;
std::cout << "n|data=" << n->data << "|grad=" << n->grad << std::endl;
std::cout << "o|data=" << o->data << "|grad=" << o->grad << std::endl;
}
{
auto x1 = std::make_shared<Value>(2.0f);
auto x2 = std::make_shared<Value>(0.0f);
auto w1 = std::make_shared<Value>(-3.0f);
auto w2 = std::make_shared<Value>(1.0f);
auto b = std::make_shared<Value>(6.881373587f);
auto x1w1 = x1 * w1;
auto x2w2 = x2 * w2;
auto x1w1x2w2 = x1w1 + x2w2;
auto n = x1w1x2w2 + b;
auto o = (exp(2*n) - 1) / (exp(2*n) + 1);
o->backward();
std::cout << "x1|data=" << x1->data << "|grad=" << x1->grad << std::endl;
std::cout << "x2|data=" << x2->data << "|grad=" << x2->grad << std::endl;
std::cout << "w1|data=" << w1->data << "|grad=" << w1->grad << std::endl;
std::cout << "w2|data=" << w2->data << "|grad=" << w2->grad << std::endl;
std::cout << "b|data=" << b->data << "|grad=" << b->grad << std::endl;
std::cout << "x1w1|data=" << x1w1->data << "|grad=" << x1w1->grad << std::endl;
std::cout << "x2w2|data=" << x2w2->data << "|grad=" << x2w2->grad << std::endl;
std::cout << "x1w1x2w2|data=" << x1w1x2w2->data << "|grad=" << x1w1x2w2->grad << std::endl;
std::cout << "n|data=" << n->data << "|grad=" << n->grad << std::endl;
std::cout << "o|data=" << o->data << "|grad=" << o->grad << std::endl;
}
{
auto a = std::make_shared<Value>(3.0f);
auto b = a + a;
b->backward();
std::cout << "a|data=" << a->data << "|grad=" << a->grad << std::endl;
std::cout << "b|data=" << b->data << "|grad=" << b->grad << std::endl;
}
{
auto a = std::make_shared<Value>(3.0f);
auto b = a + 5;
b->backward();
std::cout << "a|data=" << a->data << "|grad=" << a->grad << std::endl;
std::cout << "b|data=" << b->data << "|grad=" << b->grad << std::endl;
b->print();
}
{
auto x = std::vector<std::shared_ptr<Value>> {
std::make_shared<Value>(2.0f),
std::make_shared<Value>(2.0f),
std::make_shared<Value>(-1.0f),
};
auto n = MLP(3, {4, 4, 1});
auto out = n(x);
std::cout << n(x)[0]->data << std::endl;
}
{
auto n = MLP(3, {4, 4, 1});
auto xs = std::vector<std::vector<std::shared_ptr<Value>>> {
{
std::make_shared<Value>(2.0f),
std::make_shared<Value>(3.0f),
std::make_shared<Value>(-1.0f),
},
{
std::make_shared<Value>(3.0f),
std::make_shared<Value>(-1.0f),
std::make_shared<Value>(0.5f),
},
{
std::make_shared<Value>(5.0f),
std::make_shared<Value>(1.0f),
std::make_shared<Value>(1.0f),
},
{
std::make_shared<Value>(1.0f),
std::make_shared<Value>(1.0f),
std::make_shared<Value>(-1.0f),
},
};
auto ys = std::vector<std::shared_ptr<Value>> {
std::make_shared<Value>(1.0f),
std::make_shared<Value>(-1.0f),
std::make_shared<Value>(-1.0f),
std::make_shared<Value>(1.0f),
};
auto ypred = std::vector<std::shared_ptr<Value>>(4);
auto err = std::vector<std::shared_ptr<Value>>(4);
auto loss = std::make_shared<Value>(0.0f);
std::vector<std::shared_ptr<Value>> params;
for (int k = 0; k < 2000; k += 1) {
// Forward pass
ypred = std::vector<std::shared_ptr<Value>> {
n(xs[0])[0],
n(xs[1])[0],
n(xs[2])[0],
n(xs[3])[0],
};
err = std::vector<std::shared_ptr<Value>> {
pow(ypred[0] - ys[0], 2.0f),
pow(ypred[1] - ys[1], 2.0f),
pow(ypred[2] - ys[2], 2.0f),
pow(ypred[3] - ys[3], 2.0f),
};
loss = err[0] + err[1] + err[2] + err[3];
// Backward pass
params.clear();
n.parameters(params);
for (auto& p : params) {
p->grad = 0.0f;
}
loss->backward();
// Update
for (auto& p : params) {
p->data += -0.02f * p->grad;
}
std::cout << k << " " << loss->data << std::endl;
}
ypred[0]->print();
ypred[1]->print();
ypred[2]->print();
ypred[3]->print();
}
return 0;
}