-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnnn.py
339 lines (191 loc) · 8.25 KB
/
nnn.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
import numpy as np
import random
random.seed(43)
import matplotlib.pyplot as plt
# In[ ]:
#generate random data with seed for reproducibility
#input matrix of shape (1000,4)
np.random.seed(43)
a = np.random.randn(1000,4)
# In[ ]:
#generate random data with seed for reproducibility
#output vector of shape (1000,1)
np.random.seed(43)
b = np.random.randn(1000,1)
# $$J = -\frac{1}{m}\sum\limits_{i=1}^{m}y_i \ln(a_i)+(1-y_i)\ln(1-a_i)$$
# $$A = \sigma(w^{T}x + b)$$
# $$ \frac{\partial J}{\partial w} = \frac{1}{m}X(A-Y)^T$$
# $$\frac{\partial J}{\partial b} = \frac{1}{m}\sum\limits_{i=1}^{m}(a_i - y_i)$$
# In[ ]:
class nn(object):
"""
Class constructor.
"""
def __init__(self,x0:np.ndarray,y0:np.ndarray,lr:float=0.001,n_iters:int=1000):
"""
Constructor method.
"""
self.x0 = x0
self.y0 = y0
assert(type(x0)==np.ndarray and type(y0)==np.ndarray)
self.lr = lr
self.n_iters = n_iters
self.m, self.n = np.shape(self.x0)[0], np.shape(self.x0)[1]
#initialise parameters
#weight column vector of shape (4,1)
self.w = np.random.randn(self.n,1)
self.b = 0
def create_array(self):
"""Creates a list
:return:
:rtype: list
"""
self.array = []
return self.array
def sigmoid(self,z):
"""Non-linear activation function.
:return:
:rtype:
"""
return 1/(1+np.exp(-z))
def forward_propagate_vectorised(self):
"""Forward propagation. Vectorised Implementation.
:return:
:rtype:
"""
#number of cols in first array must equal the number of rows in second array
if np.shape(self.x0)[1] == np.shape(self.w)[0]:
#matrix multiplication input matrix and weights
#b is a scalar bias parameter
#shape (1000,4) and (4,1), then result A of shape (1000,1)
#pass into static sigmoid function defined above
A = self.sigmoid(np.matmul(self.x0,self.w)+self.b)
#vector A of shape (1000,1) must be equal to (1000,1)
#to check if A calculated correctly
if np.shape(A) == (np.size(self.x0,0),np.size(self.w,1)):
#cost function J in vectorised form
#mathematical formula defined above
cost = (-1/self.m)*np.sum(self.y0*np.log(A)+(1-self.y0)*(np.log(1-A)))
#calculate gradients of the cost function w.r.t. parameters
#mathematical formula defined above
#A is of shape (1000,1)
#self.y0 output vector is of shape (1000,1)
#self.x0 is of shape (1000,4)
#matrix multiplication can only be calc if num of cols if
#num of cols in first matrix is equal to num of rows in second matrix
#hence self.x0.T is of shape (4,1000)
#resulting d_w is then of shape (4,1)
d_w = (1/self.m)*np.matmul(self.x0.T,(A-self.y0))
#divide by 1 over num of samples
d_b = (1/self.m)*np.sum(A-self.y0)
#store gradients in a dict
gradients = {
"d_w":d_w,
"d_b":d_b
}
else:
print(f"must be of shape ({np.size(self.x0,0)},{np.size(self.w,1)})")
#number of cols in first array not equal the number of rows in second array
#matrix multiplication cannot be calculated
else:
print(f"{np.shape(self.x0)[1]} not equal to {np.shape(self.w)[0]}")
return cost, gradients
def forward_propagate_not_vectorised(self):
"""Forward propagation. Non-Vectorised Implementation.
:return:
:rtype:
"""
#matrix multiplication input matrix of shape (1000,4) and weight column vector (4,1)
#resulting vector B_p is then of shape (1000,1)
#hence first initialise B_p to zeros of shape (1000,1)
B_p = np.zeros(
(np.size(self.x0,0),np.size(self.w,1))
)
#number of cols in first array must equal the number of rows in second array
#if true, then matrix multiplication of self.x0 and self.w
if np.shape(self.x0)[1] == np.shape(self.w)[0]:
for i in range(len(self.x0)):
for j in range(len(self.w[0])):
for k in range(len(self.w)):
B_p[i][j] += self.x0[i][k]*self.w[k][j]
#add scalar bias term to the result and pass to sigmoid function
#B of shape (1000,1)
B = self.sigmoid(B_p + self.b)
#cost function J in non-vectorised form
#mathematical formula defined above
#c_ is equal to cost J, iterate over every sample
#self.y0 and B are both of shape (1000,1)
#result c_ is a scalar because it is not indexed by m
c_ = 0
for m in range(self.m):
c_ += (self.y0[m]*np.log(B[m])+(1-self.y0[m])*(np.log(1-B[m])))
cost = (-1/self.m)*c_
cost = float(cost)
#derivative of the cost function w.r.t. w in non-vectorised form
#first matrix substraction B - self.y0, both shape (1000,1)
#result K also of shape (1000,1)
#initialise K with zeros of shape (1000,1)
K = np.zeros(
(np.size(B,0),np.size(self.y0,1))
)
for u in range(self.m):
K[u] += B[u] - self.y0[u]
#K is of shape (1000,1) and self.x0 of shape (1000,4)
#transpose self.x0 for matrix multiplication in non-vectorised form
#then self.x0.T of shape (4,1000) and K of shape (1000,1)
#resulting L is of shape (4,1)
L = np.zeros((np.size(self.x0.T,0),np.size(K,1)))
for m in range(len(self.x0.T)):
for n in range(len(K[0])):
for k in range(len(K)):
L[m][n] += self.x0.T[m][k]*K[k][n]
d_w1 = (1/self.m)*L
#derivative of the cost function w.r.t. b in non-vectorised form
# result is a scalar hence initialise to zero
#notice d_b1 is not indexed by e
#iterate over each row in input data
#both B and self.y0 are of shape (1000,1)
d_b1 = 0
for e in range(self.m):
d_b1 += B[e]-self.y0[e]
d_b1 = (1/self.m)*d_b1
d_b1 = float(d_b1)
#store gradients in a dict
gradients = {
"d_w1":d_w1,
"d_b1":d_b1
}
return cost, gradients
def gradient_descent(self):
"""The below functions runs gradient descent.
:return: Optimal parameters w,b
:rtype: np.array
"""
costs = self.create_array()
for i in range(self.n_iters):
j, k = self.forward_propagate_vectorised()
self.w = self.w - self.lr*k["d_w"]
self.b = self.b - self.lr*k["d_b"]
costs.append(j)
if i % 100 ==0:
print(f"Costs after iter {i}:{j}")
optim_params = {
"w": self.w,
"b": self.b
}
plt.plot(costs)
return optim_params
# In[ ]:
neural_nets = nn(a,b,0.005,2500)
# In[ ]:
neural_nets.w
# In[ ]:
neural_nets.forward_propagate_vectorised()
# In[ ]:
neural_nets.forward_propagate_not_vectorised()
# In[ ]:
neural_nets.gradient_descent()
# In[ ]: