-
Notifications
You must be signed in to change notification settings - Fork 0
/
Perceptron_source.py
458 lines (361 loc) · 14.7 KB
/
Perceptron_source.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
"""Created on Mon Sep 23 17:50:04 2013.
@author: baskiotis, salmon, gramfort
"""
###############################################################################
# Import part
###############################################################################
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.colors import ListedColormap
from matplotlib import cm
import seaborn as sns
from matplotlib import rc
###############################################################################
# Displaying labeled data
###############################################################################
symlist = ['o', 'p', '*', 's', '+', 'x', 'D', 'v', '-', '^']
rc('font', **{'family': 'sans-serif', 'sans-serif': ['Computer Modern Roman']})
params = {'axes.labelsize': 12,
'font.size': 16,
'legend.fontsize': 16,
'text.usetex': False,
'figure.figsize': (8, 6)}
plt.rcParams.update(params)
sns.set_context("poster")
sns.set_palette("colorblind")
sns.set_style("white")
sns.axes_style()
###############################################################################
# Data Generation (you can skip the understanding)
###############################################################################
def rand_gauss(n=100, mu=[1, 1], sigmas=[0.1, 0.1]):
"""Sample points from a Gaussian variable.
Parameters
----------
n : number of samples
mu : centered
sigma : standard deviation
"""
d = len(mu)
res = np.random.randn(n, d)
return np.array(mu + res * sigmas)
def rand_bi_gauss(n1=100, n2=100, mu1=[1, 1], mu2=[-1, -1], sigmas1=[0.1, 0.1],
sigmas2=[0.1, 0.1]):
"""Sample points from two Gaussian distributions.
Parameters
----------
n1 : number of sample from first distribution
n2 : number of sample from second distribution
mu1 : center for first distribution
mu2 : center for second distribution
sigma1: std deviation for first distribution
sigma2: std deviation for second distribution
"""
ex1 = rand_gauss(n1, mu1, sigmas1)
ex2 = rand_gauss(n2, mu2, sigmas2)
y = np.hstack([np.ones(n1), -1 * np.ones(n2)])
X = np.vstack([ex1, ex2])
ind = np.random.permutation(n1 + n2)
return X[ind, :], y[ind]
def rand_clown(n1=100, n2=100, sigma1=1, sigma2=2):
"""Create samples and labels form a **clown** dataset.
Parameters
----------
n1 : number of sample from first blob
n2 : number of sample from second blob
sigma1 : noise std deviation for the first blob
sigma2 : noise std deviation for the second blob
"""
x0 = np.random.randn(n1, 1)
x1 = x0 * x0 + sigma1 * np.random.randn(n1, 1)
x2 = np.hstack([sigma2 * np.random.randn(n2, 1),
sigma2 * np.random.randn(n2, 1) + 2.])
X = np.vstack([np.hstack([x0, x1]), x2])
y = np.hstack([np.ones(n1), -1 * np.ones(n2)])
ind = np.random.permutation(n1 + n2)
return X[ind, :], y[ind]
def rand_checkers(n1=100, n2=100, sigma=0.1):
"""Create samples and labels from a noisy checker.
Parameters
----------
n1 : number of samples for the first class
n2 : number of samples for the second class
"""
nbp = int(np.floor(n1 / 8))
nbn = int(np.floor(n2 / 8))
xapp = np.reshape(np.random.rand((nbp + nbn) * 16), [(nbp + nbn) * 8, 2])
yapp = np.ones((nbp + nbn) * 8)
idx = 0
for i in range(-2, 2):
for j in range(-2, 2):
if (((i + j) % 2) == 0):
nb = nbp
else:
nb = nbn
yapp[idx:(idx + nb)] = [-1] * nb
xapp[idx:(idx + nb), 0] = np.random.rand(nb)
xapp[idx:(idx + nb), 0] += i + sigma * np.random.randn(nb)
xapp[idx:(idx + nb), 1] = np.random.rand(nb)
xapp[idx:(idx + nb), 1] += j + sigma * np.random.randn(nb)
idx += nb
ind = np.random.permutation((nbp + nbn) * 8)
res = np.hstack([xapp, yapp[:, np.newaxis]])
return np.array(res[ind, :2]), np.array(res[ind, 2])
###############################################################################
# Displaying labeled data
###############################################################################
symlist = ['o', 's', '+', 'x', 'D', '*', 'p', 'v', '-', '^']
collist = ['blue', 'red', 'purple', 'orange', 'salmon', 'black', 'grey',
'fuchsia']
def plot_2d(X, y, w=None, step=50, alpha_choice=1):
"""2D dataset data ploting according to labels.
Parameters
----------
X : data features
y : label vector
w :(optional) the separating hyperplan w
alpha_choice : control alpha display parameter
"""
min_tot0 = np.min(X[:, 0])
min_tot1 = np.min(X[:, 1])
max_tot0 = np.max(X[:, 0])
max_tot1 = np.max(X[:, 1])
delta0 = (max_tot0 - min_tot0)
delta1 = (max_tot1 - min_tot1)
labels = np.unique(y)
k = np.unique(y).shape[0]
color_blind_list = sns.color_palette("colorblind", k)
sns.set_palette(color_blind_list)
for i, label in enumerate(y):
label_num = np.where(labels == label)[0][0]
plt.scatter(X[i, 0], X[i, 1],
c=np.reshape(color_blind_list[label_num], (1, -1)),
s=80, marker=symlist[label_num])
plt.xlim([min_tot0 - delta0 / 10., max_tot0 + delta0 / 10.])
plt.ylim([min_tot1 - delta1 / 10., max_tot1 + delta1 / 10.])
if w is not None:
plt.plot([min_tot0, max_tot0],
[min_tot0 * -w[1] / w[2] - w[0] / w[2],
max_tot0 * -w[1] / w[2] - w[0] / w[2]],
"k", alpha=alpha_choice)
###############################################################################
# Loss functions and their gradient
###############################################################################
def predict(x, w):
"""Prediction from a normal vector."""
return np.dot(x, w[1:]) + w[0]
def predict_class(x, w):
"""Predict a class from at point x thanks to a normal vector."""
return np.sign(predict(x, w))
def zero_one_loss(x, y, w):
"""0-1 loss function."""
return abs(y - np.sign(predict(x, w))) / 2.
def hinge_loss(x, y, w):
"""Hinge loss function."""
return np.maximum(0., 1. - y * predict(x, w))
def mse_loss(x, y, w):
"""Mean square error loss."""
return (y - predict(x, w)) ** 2
def norm2(x, y, w):
"""Squared norm of a vector."""
return np.dot(w, w)
def gr_hinge_loss(x, y, w):
"""Sub-gradient of the loss function hingeloss."""
return np.dot(-y * (hinge_loss(x, y, w) > 0.),
np.hstack((np.ones((x.shape[0], 1)), x)))
def gr_mse_loss(x, y, w):
"""Gradient of the least squares lost function."""
return -2. * np.dot(y - predict(x, w),
np.hstack((np.ones((x.shape[0], 1)), x)))
def gr_norm2(x, y, w):
"""Gradient of the squared norm."""
return 2. * w
def pen_loss_aux(x, y, w, l):
"""Loss function penalized by hinge loss."""
return hinge_loss(x, y, w) + l * norm2(x, y, w)
def gr_pen_loss_aux(x, y, w, l):
"""Gradient of hinge loss penalized loss function."""
return gr_hinge_loss(x, y, w) + l * gr_norm2(x, y, w, )
def pen_loss(l):
"""Penalized loss function."""
return lambda x, y, w: pen_loss_aux(x, y, w, l)
def gr_pen_loss(l):
"""Gradient penalized loss function."""
return lambda x, y, w: gr_pen_loss_aux(x, y, w, l)
###############################################################################
# Displaying tools for the Frontiere
###############################################################################
def frontiere(f, X, step=50, cmap_choice=cm.coolwarm):
"""Frontiere plotting for a decision function f."""
min_tot0 = np.min(X[:, 0])
max_tot0 = np.max(X[:, 0])
min_tot1 = np.min(X[:, 1])
max_tot1 = np.max(X[:, 1])
delta0 = (max_tot0 - min_tot0)
delta1 = (max_tot1 - min_tot1)
xx, yy = np.meshgrid(np.arange(min_tot0, max_tot0, delta0 / step),
np.arange(min_tot1, max_tot1, delta1 / step))
z = np.array([f(vec) for vec in np.c_[xx.ravel(), yy.ravel()]])
z = z.reshape(xx.shape)
plt.imshow(z, origin='lower', interpolation="nearest", cmap=cmap_choice,
extent=[min_tot0, max_tot0, min_tot1, max_tot1])
plt.colorbar()
def frontiere_new(clf, X, y, w=None, step=50, alpha_choice=1, colorbar=True,
samples=True, n_labels=3, n_neighbors=3):
"""Trace la frontiere pour la fonction de decision de clf."""
min_tot0 = np.min(X[:, 0])
min_tot1 = np.min(X[:, 1])
max_tot0 = np.max(X[:, 0])
max_tot1 = np.max(X[:, 1])
delta0 = (max_tot0 - min_tot0)
delta1 = (max_tot1 - min_tot1)
xx, yy = np.meshgrid(np.arange(min_tot0, max_tot0, delta0 / step),
np.arange(min_tot1, max_tot1, delta1 / step))
XX = np.c_[xx.ravel(), yy.ravel()]
print(XX.shape)
z = clf.predict(XX)
z = z.reshape(xx.shape)
labels = np.unique(z)
color_blind_list = sns.color_palette("colorblind", labels.shape[0])
my_cmap = ListedColormap(color_blind_list)
plt.imshow(z, origin='lower', interpolation="mitchell", alpha=0.80,
cmap=my_cmap, extent=[min_tot0, max_tot0, min_tot1, max_tot1])
if colorbar is True:
ax = plt.gca()
cbar = plt.colorbar(ticks=labels)
cbar.ax.set_yticklabels(labels)
# color_blind_list = sns.color_palette("colorblind", labels.shape[0])
# sns.set_palette(color_blind_list)
ax = plt.gca()
if samples is True:
for i, label in enumerate(y):
label_num = np.where(labels == label)[0][0]
plt.scatter(X[i, 0], X[i, 1], c=color_blind_list[label_num],
s=80, marker=symlist[label_num])
plt.xlim([min_tot0, max_tot0])
plt.ylim([min_tot1, max_tot1])
ax.get_yaxis().set_ticks([])
ax.get_xaxis().set_ticks([])
if w is not None:
plt.plot([min_tot0, max_tot0],
[min_tot0 * -w[1] / w[2] - w[0] / w[2],
max_tot0 * -w[1] / w[2] - w[0] / w[2]],
"k", alpha=alpha_choice)
plt.title("L=" + str(n_labels) + ",k=" +
str(n_neighbors))
def frontiere_3d(f, data, step=20):
"""Plot the 3d frontiere for the decision function f."""
ax = plt.gca(projection='3d')
xmin, xmax = data[:, 0].min() - 1., data[:, 0].max() + 1.
ymin, ymax = data[:, 1].min() - 1., data[:, 1].max() + 1.
xx, yy = np.meshgrid(np.arange(xmin, xmax, (xmax - xmin) * 1. / step),
np.arange(ymin, ymax, (ymax - ymin) * 1. / step))
z = np.array([f(vec) for vec in np.c_[xx.ravel(), yy.ravel()]])
z = z.reshape(xx.shape)
ax.plot_surface(xx, yy, z, rstride=1, cstride=1,
linewidth=0., antialiased=False,
cmap=plt.cm.coolwarm)
def plot_cout(X, y, loss_fun, w=None):
"""Plot the cost function encoded by loss_fun,
Parameters
----------
X : data features
y : labels
loss_fun : loss function
w : (optionnal) can be used to give a historic path of the weights """
def _inter(wn):
ww = np.zeros(3)
ww[1:] = wn
return loss_fun(X, y, ww).mean()
datarange = np.array([[np.min(X[:, 0]), np.min(X[:, 1])],
[np.max(X[:, 0]), np.max(X[:, 1])]])
frontiere(_inter, np.array(datarange))
if w is not None:
plt.plot(w[:, 1], w[:, 2], 'k')
plt.xlim([np.min(X[:, 0]), np.max(X[:, 0])])
plt.ylim([np.min(X[:, 1]), np.max(X[:, 1])])
def plot_cout3d(x, y, loss_fun, w):
""" trace le cout de la fonction cout loss_fun passee en parametre, en x,y,
en faisant varier les coordonnees du poids w.
W peut etre utilise pour passer un historique de poids"""
def _inter(wn):
ww = np.zeros(3)
ww[1:] = wn
return loss_fun(x, y, ww).mean()
datarange = np.array([[w[:, 1].min(), w[:, 2].min()],
[w[:, 1].max(), w[:, 2].max()]])
frontiere_3d(_inter, np.array(datarange))
plt.plot(w[:, 1], w[:, 2], np.array([_inter(w[i, 1:]) for i in
range(w.shape[0])]), 'k-', linewidth=3)
###############################################################################
# Algorithms and functions
###############################################################################
def gradient(x, y, eps, niter, w_ini, loss_fun, gr_loss_fun, stochastic=True):
""" algorithme de descente du gradient:
- x : donnees
- y : label
- eps : facteur multiplicatif de descente
- niter : nombre d'iterations
- w_ini
- loss_fun : fonction de cout
- gr_loss_fun : gradient de la fonction de cout
- stoch : True : gradient stochastique
"""
w = np.zeros((niter, w_ini.size))
w[0] = w_ini
loss = np.zeros(niter)
loss[0] = loss_fun(x, y, w[0]).mean()
for i in range(1, niter):
if stochastic: # this is for Stochastic Gradient Descent
idx = [np.random.randint(x.shape[0])]
else: # this is for pure Gradient Descent
idx = np.arange(x.shape[0])
w[i, :] = w[i - 1, :] - eps * gr_loss_fun(x[idx, :],
y[idx], w[i - 1, :])
loss[i] = loss_fun(x, y, w[i, :]).mean()
return w, loss
def plot_gradient(X, y, wh, cost_hist, loss_fun):
""" display 4 figures on how (stochastic) gradient descent behaves
wh : solution history
cost_hist : cost history
loss_fun : loss function
"""
best = np.argmin(cost_hist)
plt.subplot(221)
plt.title('Data and hyperplane estimated')
plot_2d(X, y, wh[best, :])
plt.subplot(222)
plt.title('Projection of level line and algorithm path')
plot_cout(X, y, loss_fun, wh)
plt.subplot(223)
plt.title('Objective function vs iterations')
plt.plot(range(cost_hist.shape[0]), cost_hist)
plt.subplot(224, projection='3d')
plt.title('Level line and algorithm path')
plot_cout3d(X, y, loss_fun, wh)
###############################################################################
# Polynomial transformations
###############################################################################
def poly2(x):
""" creates features for second order interactions """
if x.ndim == 1:
x = x[None, :]
nb, d = x.shape
res = x
for i in range(0, d):
for j in range(i, d):
res = np.hstack((res, x[:, i:i + 1] * x[:, j:j + 1]))
return res
def poly3(x):
""" creates features for third order interactions """
if x.ndim == 1:
x = x[None, :]
nb, d = x.shape
res = poly2(x)
for i in range(0, d):
for j in range(i, d):
for k in range(j, d):
res = np.hstack(
(res, x[:, i:i + 1] * x[:, j:j + 1] * x[:, k:k + 1]))
return res