-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSimple_NN_classification.py
390 lines (322 loc) · 10.8 KB
/
Simple_NN_classification.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
"""
Code tha implement a shallow neural network for a binary classifications.
The code is witten imposed 2 imput, 1 output e one hidden layer.
Is possible to choose the dimesions of hidden layer.
It is also possible to save plots during the run to see how the network is learning.
In this code, the learning rate is fixed
"""
import os
import glob
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
#=============================================================
# Loss function binary classification
#=============================================================
def Loss(Yp, Y):
'''
loss function, binary crosss entropy
Parameters
----------
Yp : 1darray
actual prediction
Y : 1darray
Target
Returns
-------
float, binary crosss entropy
'''
m = len(Y)
return -np.sum(Y*np.log(Yp) + (1 - Y)*np.log(1 - Yp))/m
#=============================================================
# Activation function
#=============================================================
# Hidden layer
def g1(x):
return np.tanh(x)
# Output layer
def g2(x):
return 1 / (1 + np.exp(-x))
#=============================================================
# Initialization
#=============================================================
def init(n):
'''
Random initialization of parameters weights and biases
Parameters
----------
n : int
number of neurons in the hidden layer
Returns
-------
W1, b1 : 2darray
weights and bias for hidden layer
W2, b2 : 2darray
weights and bias for output layer
'''
# Hidden layer
# nx2 because 2 featurs and n neurons
W1 = np.random.randn(n, 2)
b1 = np.random.rand(n, 1)
# Output layer
# 1xn because 1 output and n neurons
W2 = np.random.randn(1, n)
b2 = np.random.rand(1, 1)
return W1, b1, W2, b2
#=============================================================
# Network prediction function
#=============================================================
def predict(X, W1, b1, W2, b2):
"""
Function that returns the prediction of the network
Parameters
----------
X : 2darray
data, featurs
W1, b1, W2, b2 : 2darray
parameter of the network
Returns
-------
A1 : 1d array
intermediate prediction
A2 : 1d array
final prediction
"""
# Hidden layer
Z1 = W1 @ X + b1
A1 = g1(Z1)
# Output layer
Z2 = W2 @ A1 + b2
A2 = g2(Z2)
return A1, A2
#=============================================================
# Backpropagation function
#=============================================================
def backpropagation(X, Y, step, A1, A2, W1, b1, W2, b2):
'''
Backpropagation function.
Update weights and biases with gradient descendent
all the quantities came from taking the derivative of the Loss
Y : 1darray
Target
step : float
learning rate
A1, A2 : 1darray
predictions of the network
W1, b1, W2, b2 : 2darray
parameter of the network
'''
m = len(Y)
# Output layer
dLdZ2 = (A2 - Y)
dLdW2 = dLdZ2 @ A1.T / m
dLdb2 = np.sum(dLdZ2, axis=1)[:, None] / m
# Hidden layer
dLdZ1 = W2.T @ dLdZ2 * (1 - A1**2)
dLdW1 = dLdZ1 @ X.T / m
dLdb1 = np.sum(dLdZ1, axis=1)[:, None] / m
# Update of parameters
W1 -= step * dLdW1
b1 -= step * dLdb1
W2 -= step * dLdW2
b2 -= step * dLdb2
return W1, b1, W2, b2
#=============================================================
# Accuracy mesuraments
#=============================================================
def accuracy(Yp, Y):
'''
accuracy of prediction. We use:
accuracy = 1 - | sum ( prediction - target )/target_size |
Parameters
----------
Yp : 1darray
actual prediction
Y : 1darray
Target
Returns
-------
a : float
accuracy
'''
m = len(Y)
a = 1 - abs(np.sum(Yp.ravel() - Y)/m)
return a
#=============================================================
# Train of the network
#=============================================================
def train(X, Y, n_epoch, neuro, step, sp=False, verbose=True):
'''
function for the training of the network
Parameters
----------
X : 2darray
data, featurs
Y : 1darray
Target
n_epoch : int
number of epoch
neuro : int
number of neurons in the hidden layer
step : float
learning rate
sp : boolean, optional, default False
if True a plot of boundary is saved each 100 epoch
usefull for animations
verbose : boolean, optional, default True
if True print loss and accuracy each 100 epoch
Returns
-------
result : dict
params -> W1, b1, W2, b2 weights and bias of network
train_Loss -> loss on train data
valid_Loss -> loss on validation data
'''
W1, b1, W2, b2 = init(neuro)
L_t = np.zeros(n_epoch) # training loss
L_v = np.zeros(n_epoch) # validation loss
N = X.shape[1] # total number of data
M = N//4 # nuber of data for validation
# split dataset in validation and train
X_train, Y_train = X[:, :N-M ], Y[:N-M ]
X_valid, Y_valid = X[:, N-M:], Y[ N-M:]
for i in range(n_epoch):
# train
A1, A2 = predict(X_train, W1, b1, W2, b2)
L_t[i] = Loss(A2, Y_train)
# validation
_, Yp = predict(X_valid, W1, b1, W2, b2)
L_v[i] = Loss(Yp, Y_valid)
# update
W1, b1, W2, b2 = backpropagation(X_train, Y_train, step, A1, A2, W1, b1, W2, b2)
if not i % 100:
if sp : plot(X_train, Y_train, (W1, b1, W2, b2), i)
if verbose:
acc = accuracy(A2, Y_train)
print(f'Loss = {L_t[i]:.5f}, accuracy = {acc:.5f}, epoch = {i} \r', end='')
if verbose: print()
result = {'params' : (W1, b1, W2, b2),
'train_Loss' : L_t,
'valid_Loss' : L_v,
}
return result
#=============================================================
# Plot
#=============================================================
def plot(X, Y, par, k, close=True, i=0):
'''
Plot of boundary
Parameters
----------
X : 2darray
data, featurs
Y : 1darray
Target
par : tuple
parameter of the network (W1, b1, W2, b2)
close : boolean, optional, defautl True
if we want to see only the last epoch we don't
want the figure to be closed, while we have to
make many for the animation the various figures
must be closed
i : int
index of figure
'''
# bound of plot
x_min, x_max = np.min(X[0, :]), np.max(X[0, :])
y_min, y_max = np.min(X[1, :]), np.max(X[1, :])
# data to predict to create the decision boundary
h = 0.01
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
Z = predict(np.array([xx.ravel(), yy.ravel()]), *par)[-1]
Z = Z.reshape(xx.shape)
# Plot boundary as a contour plot
fig = plt.figure(i, figsize=(6, 6))
plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
plt.contourf(xx, yy, Z, cmap='plasma')
plt.scatter(X[0, :], X[1, :], c=Y, cmap='plasma', s=8)
plt.title(f'Neural network learning epoch={k}', fontsize=15)
plt.ylabel('x2', fontsize=15)
plt.xlabel('x1', fontsize=15)
if close : plt.savefig(f'frames/{k}.png')
if close : plt.close(fig)
#=============================================================
# GIF
#=============================================================
def GIF(path, name, ext='.png'):
'''
function to create a gif from several plot
Parameters
----------
path : string
path of the varius plot
name : string
name of the gif
ext : string, optional, default .png
type of file
'''
fig_path = path + '/*' + ext
gif_path = path + '/' + name + '.gif'
frames=[]
imgs = sorted(glob.glob(fig_path))
imgs.sort(key=len)
for i in imgs:
new_frame = Image.open(i)
frames.append(new_frame)
frames[0].save(gif_path, format='GIF', \
append_images=frames[:],save_all=True,duration=100,loop=0)
if __name__ == '__main__':
np.random.seed(69420)
#=============================================================
# Creation of dataset
#=============================================================
N = 5000 # Total number of points
M = 1000 # number of test and validation points
X = np.random.random(size=(2, N)) # Two features
Y = np.ones(N) # one Target
# Selection of some regions where the target value is different
for x1, x2, i in zip(X[0, :], X[1, :], range(N)):
if np.sqrt( (x1 - 0.3)**2 + (x2 - 0.3)**2 ) < 0.2:
Y[i] = 0
if np.sqrt( (x1 - 0.65)**2 + (x2 - 0.7)**2 ) < 0.2:
Y[i] = 0
# split dataset in test and train
# Part of train data will be used for validation
X_train, Y_train = X[:, :N-M ], Y[:N-M ]
X_test, Y_test = X[:, N-M:], Y[ N-M:]
#=============================================================
# Parameter of computation
#=============================================================
n_epoch = 6000 + 1 # number of epoch
n_neuro = 20 # number of neurons for the hidden layer
lr_rate = 1.5 # learning rate
sp_gif = 0 # save plot and make gif
#=============================================================
# Train of the network
#=============================================================
result = train(X_train, Y_train, n_epoch, n_neuro, lr_rate, sp_gif)
parameters = result['params']
L_t = result['train_Loss']
L_v = result['valid_Loss']
if sp_gif : GIF('frames', 'NN')
print(f'Loss on train set = {L_t[-1]:.5f}')
print(f'Loos on validation set = {L_v[-1]:.5f}')
#=============================================================
# Test of the network
#=============================================================
_, A2 = predict(X_test, *parameters)
acc = accuracy(A2, Y_test)
loss = Loss(A2, Y_test)
plot(X_test, Y_test, parameters, k=n_epoch, close=False, i=1)
print(f'Loss on test set = {loss:.5f}')
print(f"Accuracy on test set = {acc:.5f}")
plt.figure(2)
plt.plot(np.linspace(1, n_epoch, n_epoch), L_t, 'b', label='train Loss')
plt.plot(np.linspace(1, n_epoch, n_epoch), L_v, 'r', label='validation loss')
plt.title('Binary cross entropy', fontsize=15)
plt.xlabel('epoch')
plt.ylabel('Loss')
plt.legend(loc='best')
plt.grid()
plt.show()