-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_diff.py
198 lines (154 loc) · 4.74 KB
/
auto_diff.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import math
class Value:
def __init__(self, value, operator=None, dependencies=[], power = 0):
self.value = float(value)
self.operator = operator
self.dependencies = dependencies
self.power = power
self.grad = 0
self.vis = False
def zero(self):
self.grad = 0
for i in self.dependencies:
i.zero()
def backprop(self):
if self.operator == '+':
self.dependencies[0].grad += self.grad
self.dependencies[1].grad += self.grad
elif self.operator == '-':
self.dependencies[0].grad += self.grad
self.dependencies[1].grad += -self.grad
elif self.operator == '*':
self.dependencies[0].grad += self.dependencies[1].value * self.grad
self.dependencies[1].grad += self.dependencies[0].value * self.grad
elif self.operator == '/':
self.dependencies[0].grad += (1/self.dependencies[1].value) * self.grad
self.dependencies[1].grad += (-self.dependencies[0].value/(self.dependencies[1].value**2)) * self.grad
elif self.operator == '^':
self.dependencies[0].grad += self.power * (self.dependencies[0].value ** (self.power - 1)) * self.grad
elif self.operator == 'relu':
self.dependencies[0].grad += self.grad if self.dependencies[0].value > 0 else 0
elif self.operator == 'sigmoid':
# Derivative of the sigmoid function
# https://math.stackexchange.com/a/1225116
self.dependencies[0].grad += (1.0 / (1.0 + math.exp(-self.dependencies[0].value))) * (1.0 - 1.0 / (1.0 + math.exp(-self.dependencies[0].value))) * self.grad
elif self.operator == 'neg':
self.dependencies[0].grad += -self.grad
elif self.operator == 'log':
self.dependencies[0].grad += (1.0 / self.dependencies[0].value) * self.grad
def __add__(self, x):
return Value(self.value + x.value, '+', [self, x])
def __sub__(self, x):
return Value(self.value - x.value, '-', [self, x])
def __neg__(self):
return Value(-self.value, 'neg', [self])
def __mul__(self, x):
return Value(self.value * x.value, '*', [self, x])
def __truediv__(self, x):
return Value(self.value / x.value, '/', [self, x])
def __pow__(self ,x):
return Value(self.value**x, '^', [self], x)
def log(self):
return Value(math.log(self.value), 'log', [self])
def relu(self):
return Value(max(0, self.value), 'relu', [self])
def sigmoid(self):
return Value(1.0 / (1.0 + math.exp(-self.value)), 'sigmoid', [self])
def dfs(self, topsort):
self.vis = True
for i in self.dependencies:
if not i.vis:
i.dfs(topsort)
topsort.append(self)
def backward(self):
topsort = []
# Run a DFS from the result node, generate the topological order
# Run backprop from the topological order
self.dfs(topsort)
# A reverse topological order of a graph is equivalent to the topological order of the graph with reverse edges
# Proof: https://qr.ae/pKV2Zn
topsort = topsort[::-1]
self.grad = 1
for i in topsort:
i.backprop()
"""
# Simple equation 1
a = Value(2)
b = Value(3)
c = Value(4)
d = Value(1)
e = a * b
y = e * c + e * d
y.backward()
"""
"""
# Simple equation 2
a = Value(2)
b = Value(3)
y = a / b
y.backward()
"""
"""
# Simple equation 3
a = Value(2)
y = a**3
y.backward()
"""
"""
# Simple neural network
x = Value(2)
w = [
[
[Value(2)]
],
[
[Value(3)],
[Value(4)]
],
[
[Value(10), Value(11)]
]
]
b = [
[Value(10)],
[Value(-5), Value(-3)],
[Value(3)]
]
a = [
[None],
[None, None],
[None]
]
a[0][0] = w[0][0][0] * x + b[0][0]
a[1][0] = w[1][0][0] * a[0][0] + b[1][0]
a[1][1] = w[1][1][0] * a[0][0] + b[1][1]
a[2][0] = w[2][0][0] * a[1][0] + w[2][0][1] * a[1][1] + b[2][0]
a[2][0].backward()
"""
"""
import numpy as np
x = np.array([Value(2), Value(1)])
w = np.array([[Value(1), Value(1)], [Value(0.5), Value(0)]])
b = np.array([Value(0), Value(1)])
a1 = w @ x + b
a1 = np.array([i.relu() for i in a1])
cost = np.sum(a1)
x = np.array([Value(1.3), Value(1)])
a1 = w@x + b
a1 = np.array([i.relu() for i in a1])
cost += np.sum(a1)
cost.backward()
print(cost.value)
print(w[0][0].grad)
x = np.array([Value(2), Value(1)])
w = np.array([[Value(1+0.001), Value(1)], [Value(0.5), Value(0)]])
b = np.array([Value(0), Value(1)])
a1 = w @ x + b
a1 = np.array([i.relu() for i in a1])
cost = np.sum(a1)
x = np.array([Value(1.3), Value(1)])
a1 = w@x + b
a1 = np.array([i.relu() for i in a1])
cost += np.sum(a1)
print(cost.value)
"""