-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValue.h
168 lines (137 loc) · 5.83 KB
/
Value.h
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
#ifndef VALUE_H
#define VALUE_H
#include <iostream>
#include <memory>
#include <set>
#include <functional>
#include <cassert>
#include <cmath>
class Value : public std::enable_shared_from_this<Value> {
public:
double data;
double grad;
std::function<void()> _backward;
std::set<std::shared_ptr<Value>> _prev;
std::string _op;
Value(double data, std::set<std::shared_ptr<Value>> _children = {}, std::string _op = "")
: data(data), grad(0), _backward([]{}), _prev(_children), _op(_op) {}
std::shared_ptr<Value> operator+(const std::shared_ptr<Value>& other) {
auto out = std::make_shared<Value>(data + other->data, std::set<std::shared_ptr<Value>>{shared_from_this(), other}, "+");
out->_backward = [this, other, out] {
this->grad += out->grad;
other->grad += out->grad;
};
return out;
}
std::shared_ptr<Value> operator*(const std::shared_ptr<Value>& other) {
auto out = std::make_shared<Value>(data * other->data, std::set<std::shared_ptr<Value>>{shared_from_this(), other}, "*");
out->_backward = [this, other, out] {
this->grad += other->data * out->grad;
other->grad += this->data * out->grad;
};
return out;
}
std::shared_ptr<Value> pow(double exponent) {
assert(exponent == static_cast<int>(exponent) || exponent == static_cast<float>(exponent) || exponent == static_cast<double>(exponent));
auto out = std::make_shared<Value>(std::pow(data, exponent), std::set<std::shared_ptr<Value>>{shared_from_this()}, "**" + std::to_string(exponent));
out->_backward = [this, exponent, out] {
this->grad += (exponent * std::pow(data, exponent - 1)) * out->grad;
};
return out;
}
std::shared_ptr<Value> relu() {
auto out = std::make_shared<Value>(data < 0 ? 0 : data, std::set<std::shared_ptr<Value>>{shared_from_this()}, "ReLU");
out->_backward = [this, out] {
this->grad += (out->data > 0) * out->grad;
};
return out;
}
void backward() {
std::vector<std::shared_ptr<Value>> topo;
std::set<std::shared_ptr<Value>> visited;
auto build_topo = [&](auto&& self, const std::shared_ptr<Value>& v) -> void {
if (visited.find(v) == visited.end()) {
visited.insert(v);
for (const auto& child : v->_prev) {
self(self, child);
}
topo.push_back(v);
}
};
build_topo(build_topo, shared_from_this());
grad = 1;
for (auto it = topo.rbegin(); it != topo.rend(); ++it) {
(*it)->_backward();
}
}
std::shared_ptr<Value> operator-() {
return (*this) * std::make_shared<Value>(-1);
}
std::shared_ptr<Value> operator+(double other) {
return (*this) + std::make_shared<Value>(other);
}
std::shared_ptr<Value> operator-(const std::shared_ptr<Value>& other) {
return (*this) + (-other);
}
std::shared_ptr<Value> operator-(double other) {
return (*this) + std::make_shared<Value>(-other);
}
std::shared_ptr<Value> operator*(double other) {
return (*this) * std::make_shared<Value>(other);
}
std::shared_ptr<Value> operator/(const std::shared_ptr<Value>& other) {
return (*this) * other->pow(-1);
}
std::shared_ptr<Value> operator/(double other) {
return (*this) * std::make_shared<Value>(1.0 / other);
}
// Overload the operators for shared_ptr<Value>
friend std::shared_ptr<Value> operator-(const std::shared_ptr<Value>& l) {
return l->operator-();
}
friend std::shared_ptr<Value> operator+(const std::shared_ptr<Value>& lhs, const std::shared_ptr<Value>& rhs) {
return lhs->operator+(rhs);
}
friend std::shared_ptr<Value> operator-(const std::shared_ptr<Value>& lhs, const std::shared_ptr<Value>& rhs) {
return lhs->operator-(rhs);
}
friend std::shared_ptr<Value> operator*(const std::shared_ptr<Value>& lhs, const std::shared_ptr<Value>& rhs) {
return lhs->operator*(rhs);
}
friend std::shared_ptr<Value> operator/(const std::shared_ptr<Value>& lhs, const std::shared_ptr<Value>& rhs) {
return lhs->operator/(rhs);
}
friend std::shared_ptr<Value> operator+(double lhs, const std::shared_ptr<Value>& rhs) {
return std::make_shared<Value>(lhs) + rhs;
}
friend std::shared_ptr<Value> operator-(double lhs, const std::shared_ptr<Value>& rhs) {
return std::make_shared<Value>(lhs) - rhs;
}
friend std::shared_ptr<Value> operator*(double lhs, const std::shared_ptr<Value>& rhs) {
return std::make_shared<Value>(lhs) * rhs;
}
friend std::shared_ptr<Value> operator/(double lhs, const std::shared_ptr<Value>& rhs) {
return std::make_shared<Value>(lhs) / rhs;
}
friend std::shared_ptr<Value> operator+(const std::shared_ptr<Value>& lhs, double rhs) {
return lhs + std::make_shared<Value>(rhs);
}
friend std::shared_ptr<Value> operator-(const std::shared_ptr<Value>& lhs, double rhs) {
return lhs - std::make_shared<Value>(rhs);
}
friend std::shared_ptr<Value> operator*(const std::shared_ptr<Value>& lhs, double rhs) {
return lhs * std::make_shared<Value>(rhs);
}
friend std::shared_ptr<Value> operator/(const std::shared_ptr<Value>& lhs, double rhs) {
return lhs / std::make_shared<Value>(rhs);
}
friend std::ostream& operator<<(std::ostream& os, const Value& v) {
os << "Value(data=" << v.data << ", grad=" << v.grad << ")" << std::endl;
return os;
}
friend std::ostream& operator<<(std::ostream& os, const std::shared_ptr<Value>& v) {
os << "Value(data=" << v->data << ", grad=" << v->grad << ")" << std::endl;
return os;
}
};
#endif // VALUE_H