-
Notifications
You must be signed in to change notification settings - Fork 0
/
ad.py
133 lines (122 loc) · 3.41 KB
/
ad.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
class Expr:
def __init__(self, deps):
self.der = 0
self.deps = deps
self.height = max(map((lambda d: d.height), deps)) + 1
def __neg__(self):
return ExprNeg([self])
def __add__(self, b):
return ExprAdd([self, const(b)])
def __sub__(self, b):
return self + (-b)
def __mul__(self, b):
return ExprMul([self, const(b)])
def __truediv__(self, b):
return self * reciprocal(b)
def __radd__(self, a):
return ExprAdd([const(a), self])
def __rsub__(self, a):
return a + (-self)
def __rmul__(self, a):
return ExprMul([const(a), self])
def __rtruediv__(self, a):
return a * reciprocal(self)
def __lt__(self, b):
if isinstance(b,Expr):
return self.val < b.val
return self.val < b
def const(v):
if type(v) is int or type(v) is float:
return ExprConst(v)
else:
return v
def add(a,b):
if isinstance(b, Expr):
return b+a
return a+b
def mul(a,b):
if isinstance(b, Expr):
return b*a
return a*b
def sub(a,b):
if isinstance(b, Expr):
return (-b)+a
return a-b
def reciprocal(a):
if isinstance(a, Expr):
return ExprReciprocal([a])
if a == 0:
return float("inf")
return 1/a
def eq(a,b):
if isinstance(a, Expr):
a = a.val
if isinstance(b, Expr):
b = b.val
return a == b
class ExprConst(Expr):
def __init__(self, val):
self.der = 0
self.deps = []
self.height = 0
self.val = val
def backprop(self):
pass
class ExprNeg(Expr):
def __init__(self, deps):
super().__init__(deps)
self.val = -self.deps[0].val
def backprop(self):
self.deps[0].der = sub(self.deps[0].der, self.der)
class ExprAdd(Expr):
def __init__(self,deps):
super().__init__(deps)
self.val = add(self.deps[0].val, self.deps[1].val)
def backprop(self):
self.deps[0].der = add(self.deps[0].der, self.der)
self.deps[1].der = add(self.deps[1].der, self.der)
class ExprMul(Expr):
def __init__(self,deps):
super().__init__(deps)
self.val = mul(self.deps[0].val, self.deps[1].val)
def backprop(self):
self.deps[0].der = add(mul(self.deps[1].val, self.der), self.deps[0].der)
self.deps[1].der = add(mul(self.deps[0].val, self.der), self.deps[1].der)
class ExprReciprocal(Expr):
def __init__(self,deps):
super().__init__(deps)
self.val = reciprocal(self.deps[0].val)
def backprop(self):
self.deps[0].der = add(self.deps[0].der,
mul(self.der,
-reciprocal(mul(self.deps[0].val,self.deps[0].val))))
def backprop(root):
exprs = set()
todo = set([root])
while todo:
e = todo.pop()
for d in e.deps:
todo.add(d)
exprs.add(e)
root.der = 1
for e in sorted(exprs, key=(lambda e: e.height), reverse=True):
e.backprop()
# Differentiate a function.
# Input function can take any number of input numbers,
# and must return a single number.
# Returns a new function that
# accepts arguments in the same form,
# and returns an array of the derivatives of each argument
# with respect to the single output number.
def diff(f):
def derivative(*args):
input_exprs = list(map(ExprConst, args))
output_expr = const(f(*input_exprs))
backprop(output_expr)
return list(map((lambda e: e.der), input_exprs))
return derivative
# Special-case of the above, for functions that accept a single number.
# In this case, the derivative function returns the single derivative,
# instead of an array of one derivative.
def diff_single(f):
return lambda x: diff(f)(x)[0]