-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnn.py
More file actions
211 lines (156 loc) · 6.9 KB
/
Copy pathnn.py
File metadata and controls
211 lines (156 loc) · 6.9 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
"""
A Neural Network from scratch, based on Karpathy's micrograd.
References:
- https://github.com/karpathy/micrograd
"""
import math
class Value:
def __init__(self, data, parents = set()):
self.data = float(data)
self.parents = parents
self.grad = 0.0 # Global derivate, i.e. dL/dv
self._backward = lambda: None # Applies chain rule
def __repr__(self):
return f"Value = {self.data}; Gradient = {self.grad}"
def __neg__(self):
return self * -1
def __add__(self, other):
other = other if isinstance(other, Value) else Value(other)
out = Value(
data = self.data + other.data,
parents = {self, other}
)
def _backward():
self.grad += out.grad # o = a + b; dL/da = dL/do . do/da = dL/do
other.grad += out.grad # similarly to dL/db
out._backward = _backward
return out
def __radd__(self, other):
return self + other
def __sub__(self, other):
other = other if isinstance(other, Value) else Value(other)
out = Value(
data = self.data - other.data,
parents = {self, other}
)
def _backward():
self.grad += out.grad # o = a - b; dL/da = dL/do . do/da = dL/do
other.grad -= out.grad # o = a - b; dL/db = dL/do . do/db = - dL/do
self._backward = _backward
return out
def __rsub__(self, other):
return self + (-other)
def __mul__(self, other):
other = other if isinstance(other, Value) else Value(other)
out = Value(
data = self.data * other.data,
parents = {self, other}
)
def _backward():
self.grad += out.grad * other.data # o = a * b; dL/da = dL/do . do/da = dL/do . b
other.grad += self.data * out.grad # o = a * b; dL/db = dL/do . do/db = a . dL/do
out._backward = _backward
return out
def __rmul__(self, other):
return other * self
def __truediv__(self, other):
"""
other = other if isinstance(other, Value) else Value(other)
out = Value(
data = self.data * other.data,
parents = {self, other}
)
def _backward():
self.grad += out.grad / other.data # o = a / b; dL/da = dL/do . do/da = dL/do / b
other.grad += self.data / out.grad # o = a / b; dL/db = dL/do . do/db = a / dL/do
out._backward = _backward
return out
"""
# a / b == a * (1/b) == a * (b**(-1))
return self * (other ** -1)
def __rtruediv__(self, other):
return other * (self ** -1)
def __pow__(self, other):
assert isinstance(other, (int, float)), "only supporting int/float powers for now"
out = Value(
data = self.data ** other,
parents = {self, other}
)
def _backward():
# o = a^b; dL/da = dL/do . do/da = dL/do . (b . a ^ (b - 1))
self.grad += out.grad * (other * (self.data ** (other - 1)))
# o = a^b; dL/db = dL/do . do/db = dL/do . (a**b ln a)
other.grad += out.grad * ((self.data**other) * math.log(self.data))
out._backward = _backward
return out
def exp(self):
out = Value(
data = math.exp(self.data),
parents = {self}
)
def _backward():
# o = e^a; dL/da = dL/do . do/da = dL/do . e^x
self.grad += out.grad * out.data # out.data == math.exp(self.data) == e^x
out._backward = _backward
return out
def relu(self):
out = Value(
data = max(0, self.data),
parents = {self}
)
def _backward():
# o = a.relu(); dL/da = dL/do . do/da = dL/do . (1 if x > 0 else 0 if x < 0)
self.grad += out.grad * int(self.data > 0)
out._backward = _backward
return out
def backward(self):
topsort = []
visited = set()
def build_topsort(v):
if v not in visited:
visited.add(v)
for parent in v.parents:
build_topsort(parent)
topsort.append(v)
build_topsort(self)
self.grad = 1.0
for v in reversed(topsort):
v._backward()
if __name__ == "__main__":
"""
NN to fit the Iris dataset.
4 input layer, 7, 7, 3
"""
import numpy as np
import random
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True) # len(X) == 150
y = y.reshape((150, 1))
data = np.append(X, y, axis=1)
np.random.shuffle(data)
X_train, y_train = data[0:120,:4], data[0:120,4]
X_test, y_test = data[120:,:4], data[120:,4]
i1, i2, i3, i4 = Value(0), Value(0), Value(0), Value(0)
w1s = [[Value(random.uniform(-1,1)) for _ in range(5)] for _ in range(5)] # np.random.uniform(-1, 1, size=(5, 5))
h11 = (i1*w1s[0][0] + i2*w1s[0][1] + i3*w1s[0][2] + i4*w1s[0][3] + w1s[0][4]).relu()
h12 = (i1*w1s[1][0] + i2*w1s[1][1] + i3*w1s[1][2] + i4*w1s[1][3] + w1s[1][4]).relu()
h13 = (i1*w1s[2][0] + i2*w1s[2][1] + i3*w1s[2][2] + i4*w1s[2][3] + w1s[2][4]).relu()
h14 = (i1*w1s[3][0] + i2*w1s[3][1] + i3*w1s[3][2] + i4*w1s[3][3] + w1s[3][4]).relu()
h15 = (i1*w1s[4][0] + i2*w1s[4][1] + i3*w1s[4][2] + i4*w1s[4][3] + w1s[4][4]).relu()
w2s = [[Value(random.uniform(-1,1)) for _ in range(6)] for _ in range(5)] # np.random.uniform(-1, 1, size=(5, 6))
h21 = (h11*w2s[0][0] + h12*w2s[0][1] + h13*w2s[0][2] + h14*w2s[0][3] + h15*w2s[0][4] + w2s[0][5]).relu()
h22 = (h11*w2s[1][0] + h12*w2s[1][1] + h13*w2s[1][2] + h14*w2s[1][3] + h15*w2s[1][4] + w2s[1][5]).relu()
h23 = (h11*w2s[2][0] + h12*w2s[2][1] + h13*w2s[2][2] + h14*w2s[2][3] + h15*w2s[2][4] + w2s[2][5]).relu()
h24 = (h11*w2s[3][0] + h12*w2s[3][1] + h13*w2s[3][2] + h14*w2s[3][3] + h15*w2s[3][4] + w2s[3][5]).relu()
h25 = (h11*w2s[4][0] + h12*w2s[4][1] + h13*w2s[4][2] + h14*w2s[4][3] + h15*w2s[4][4] + w2s[4][5]).relu()
w3s = [[Value(random.uniform(-1,1)) for _ in range(6)] for _ in range(3)] # np.random.uniform(-1, 1, size=(3, 6))
o1aux = (h21*w3s[0][0] + h22*w3s[0][1] + h23*w3s[0][2] + h24*w3s[0][3] + h25*w3s[0][4] + w3s[0][5])
o2aux = (h21*w3s[1][0] + h22*w3s[1][1] + h23*w3s[1][2] + h24*w3s[1][3] + h25*w3s[1][4] + w3s[1][5])
o3aux = (h21*w3s[2][0] + h22*w3s[2][1] + h23*w3s[2][2] + h24*w3s[2][3] + h25*w3s[2][4] + w3s[2][5])
den = o1aux.exp() + o2aux.exp() + o3aux.exp()
o1 = o1aux.exp() / den
o2 = o2aux.exp() / den
o3 = o3aux.exp() / den
# TODO: define loss
# TODO: train
# TODO: test