-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue.py
180 lines (118 loc) · 4.9 KB
/
value.py
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
from math import exp
class Value(object):
def __init__(self, data, first_parent=None, second_parent=None, operation=''):
self.data = data
self.operation = operation
self.first_parent = first_parent
self.second_parent = second_parent
self.grad = 0
self.label = 'un'
def backprop(self):
visited = set()
topsorted_list = []
def topsort(v):
if v not in visited:
visited.add(v)
if v.first_parent:
topsort(v.first_parent)
if v.second_parent:
topsort(v.second_parent)
topsorted_list.append(v)
topsort(self)
self.grad = 1
for v in reversed(topsorted_list):
v._backprop()
def _backprop(self):
if self.operation == '+':
self.first_parent.grad += self.grad
self.second_parent.grad += self.grad
elif self.operation == '-':
self.first_parent.grad += self.grad
self.second_parent.grad += -1 * self.grad
elif self.operation == '*':
self.first_parent.grad += self.grad * self.second_parent.data
self.second_parent.grad += self.grad * self.first_parent.data
elif self.operation == '/':
self.first_parent.grad += self.grad / self.second_parent.data
self.second_parent.grad += self.grad * self.first_parent.data * -1 / (self.second_parent.data)**2
elif self.operation == 'relu':
self.first_parent.grad += self.grad * int(self.data > 0)
elif self.operation == 'sigmoid':
self.first_parent.grad += self.grad * self.data * (1 - self.data) # data contains value of sigmoid(x)
elif self.operation == 'tanh':
self.first_parent.grad += self.grad * (1 - (self.data)**2) # data contains value of tanh(x)
elif self.operation == 'pow':
self.first_parent.grad += self.grad * self.second_parent.data * (self.first_parent.data)**(self.second_parent.data - 1)
elif self.operation == 'neg':
self.first_parent.grad += self.grad * (-1)
def __repr__(self):
label = f"node: data = {self.data}"
if self.operation != '':
label += f', operation = {self.operation}'
return label
@staticmethod
def relu(v):
if not isinstance(v, Value):
v = Value(v)
new = Value(max(v.data, 0), first_parent=v, operation='relu')
return new
@staticmethod
def sigmoid(v):
if not isinstance(v, Value):
v = Value(v)
data = 1 / (1 + exp(-v.data))
new = Value(data, first_parent=v, operation='sigmoid')
return new
@staticmethod
def tanh(v):
if not isinstance(v, Value):
v = Value(v)
e_2x = exp(2 * v.data)
data = (e_2x - 1) / (e_2x + 1)
new = Value(data, first_parent=v, operation='tanh')
return new
def __neg__(self):
new = Value(-self.data, first_parent=self, operation='neg')
return new
def __pow__(self, other):
other = Value(other)
new = Value(self.data ** other.data, first_parent=self, second_parent=other, operation='pow')
return new
def __add__(self, other):
if not isinstance(other, Value):
other = Value(other)
new = Value(self.data + other.data, first_parent=self, second_parent=other, operation='+')
return new
def __sub__(self, other):
if not isinstance(other, Value):
other = Value(other)
new = Value(self.data - other.data, first_parent=self, second_parent=other, operation='-')
return new
def __rsub__(self, other):
if not isinstance(other, Value):
other = Value(other)
new = Value(other.data - self.data, first_parent=other, second_parent=self, operation='-')
return new
def __mul__(self, other):
if not isinstance(other, Value):
other = Value(other)
new = Value(self.data * other.data, first_parent=self, second_parent=other, operation='*')
return new
def __truediv__(self, other):
if not isinstance(other, Value):
other = Value(other)
new = Value(self.data / other.data, first_parent=self, second_parent=other, operation='/')
return new
def __rtruediv__(self, other):
if not isinstance(other, Value):
other = Value(other)
new = Value(other.data / self.data, first_parent=other, second_parent=self, operation='/')
return new
def __radd__(self, other):
return self + other
def __rmul__(self, other):
return self * other
def main():
a = Value(5)
b = Value(10)
main()