-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnodes.py
157 lines (117 loc) · 4.38 KB
/
nodes.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
from typing import List
class ScalarNodeCache:
value = None
local_partial_derivatives_wrt_arguments = None
global_derivative_wrt_self = None
class ScalarNode:
def __init__(self, arguments: List['ScalarNode']) -> None:
self._parents = []
self._arguments = arguments
for arg in self._arguments:
arg._parents.append(self)
# --- TASK_6 ---
self._cache = ScalarNodeCache()
# --- TASK_6 ---
def value(self) -> float:
raise NotImplementedError()
def local_partial_derivatives_wrt_arguments(self) -> List[float]:
raise NotImplementedError()
def find_self_position_in_parents_arguments(self, parent: 'ScalarNode') -> int:
for i, arg in enumerate(parent._arguments):
if self == arg:
return i
raise Exception("Self found not in parent's arguments")
def global_derivative_wrt_self(self) -> float:
# --- TASK_6 ---
if self._cache.global_derivative_wrt_self is not None:
return self._cache.global_derivative_wrt_self
# --- TASK_6 ---
if len(self._parents) == 0:
# no parent, this must be the output node, and d out/d out = 1.0
return 1.0
else:
result = 0.0
# --- TASK_5 ---
# multiply and add (generalized chain rule)
for p in self._parents:
index_in_parents_arguments = self.find_self_position_in_parents_arguments(p)
parent_to_self_derivative = p.local_partial_derivatives_wrt_arguments()[index_in_parents_arguments]
parent_global_derivative = p.global_derivative_wrt_self()
result += parent_to_self_derivative * parent_global_derivative
# --- TASK_5 ---
# --- TASK_6 ---
self._cache.global_derivative_wrt_self = result
# --- TASK_6 ---
return result
class ConstantNode(ScalarNode):
def __init__(self, value: float) -> None:
super().__init__([])
self._value = value
def value(self) -> float:
return self._value
class SumNode(ScalarNode):
def value(self) -> float:
# --- TASK_6 ---
if self._cache.value is not None:
return self._cache.value
# --- TASK_6 ---
result = 0.0
# sum all arguments values
# --- TASK_2 ---
for arg in self._arguments:
result += arg.value()
# --- TASK_2 ---
# --- TASK_6 ---
self._cache.value = result
# --- TASK_6 ---
return result
def local_partial_derivatives_wrt_arguments(self) -> List[float]:
# Partial derivative wrt. each argument is 1.0, for example
# y = w_1 + w_2 + w_3
# dy/dw_1 = 1
# dy/dw_2 = 1
# dy/dw_3 = 1
# --- TASK_2 ---
return [1.0] * len(self._arguments)
# --- TASK_2 ---
class ProductNode(ScalarNode):
def value(self) -> float:
# --- TASK_6 ---
if self._cache.value is not None:
return self._cache.value
# --- TASK_6 ---
result = 1.0
# multiply all arguments values
# --- TASK_3 ---
for arg in self._arguments:
result *= arg.value()
# --- TASK_3 ---
# --- TASK_6 ---
self._cache.value = result
# --- TASK_6 ---
return result
def local_partial_derivatives_wrt_arguments(self) -> List[float]:
# --- TASK_6 ---
if self._cache.local_partial_derivatives_wrt_arguments is not None:
return self._cache.local_partial_derivatives_wrt_arguments
# --- TASK_6 ---
# Partial derivative wrt. each argument is a product of all other arguments, for example
# y = w_1 * w_2 * w_3
# dy/dw_1 = w_2 * w_3
# dy/dw_2 = w_1 * w_3
# dy/dw_3 = w_1 * w_2
# zero-filled result
result = [0.0] * len(self._arguments)
# --- TASK_3 ---
for i in range(len(self._arguments)):
ith_result = 1.0
for j in range(len(self._arguments)):
if i != j:
j_value = self._arguments[j].value()
ith_result *= j_value
result[i] = ith_result
# --- TASK_3 ---
# --- TASK_6 ---
self._cache.local_partial_derivatives_wrt_arguments = result
# --- TASK_6 ---
return result