-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysom.py
579 lines (478 loc) · 22.2 KB
/
mysom.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# The main source of this code belongs to miniSOM library https://github.com/JustGlowing/minisom
# Modified by Ahmad Pouramini and Benyamin Hosseiny
# Modified parts:
# Converging based on delta_W (Weight matrix) (maximun difference or mean difference)
# Various decay fuctions such as exponential and geometrical
from math import sqrt
import matplotlib.pyplot as plt
import numpy as np
from numpy import (array, unravel_index, nditer, linalg, random, subtract,
power, exp, pi, zeros, arange, outer, meshgrid, dot, max, abs,
logical_and, mean, std, cov, argsort, linspace, transpose)
from collections import defaultdict, Counter
from warnings import warn
from sys import stdout
from time import time
# for unit tests
from numpy.testing import assert_almost_equal, assert_array_almost_equal
from numpy.testing import assert_array_equal
import unittest
"""
Minimalistic implementation of the Self Organizing Maps (SOM).
"""
def _incremental_index_verbose(m):
"""Yields numbers from 0 to m-1 printing the status on the stdout."""
progress = f'\r [ {0:{len(str(m))}} / {m} ] {0:3.0f}% ? it/s'
stdout.write(progress)
beginning = time()
for i in range(m):
yield i
it_per_sec = (time() - beginning) / (i+1)
progress = f'\r [ {i+1:{len(str(m))}} / {m} ]'
progress += f' {100*(i+1)/m:3.0f}%'
progress += f' {it_per_sec:4.5f} it/s'
stdout.write(progress)
def fast_norm(x):
"""Returns norm-2 of a 1-D numpy array.
* faster than linalg.norm in case of 1-D arrays (numpy 1.9.2rc1).
"""
return sqrt(dot(x, x.T))
def asymptotic_decay(learning_rate, t, max_iter):
"""Decay function of the learning process.
Parameters
----------
learning_rate : float
current learning rate.
t : int
current iteration.
max_iter : int
maximum number of iterations for the training.
"""
return learning_rate / (1+t/(max_iter/2))
class MiniSom(object):
def __init__(self, x, y, input_len, sigma=1.0, learning_rate=0.5,
decay_function=asymptotic_decay,
neighborhood_function='gaussian', random_seed=None):
"""Initializes a Self Organizing Maps.
A rule of thumb to set the size of the grid for a dimensionality
reduction task is that it should contain 5*sqrt(N) neurons
where N is the number of samples in the dataset to analyze.
E.g. if your dataset has 150 samples, 5*sqrt(150) = 61.23
hence a map 8-by-8 should perform well.
Parameters
----------
x : int
x dimension of the SOM.
y : int
y dimension of the SOM.
input_len : int
Number of the elements of the vectors in input.
sigma : float, optional (default=1.0)
Spread of the neighborhood function, needs to be adequate
to the dimensions of the map.
(at the iteration t we have sigma(t) = sigma / (1 + t/T)
where T is #num_iteration/2)
learning_rate, initial learning rate
(at the iteration t we have
learning_rate(t) = learning_rate / (1 + t/T)
where T is #num_iteration/2)
decay_function : function (default=None)
Function that reduces learning_rate and sigma at each iteration
the default function is:
learning_rate / (1+t/(max_iterarations/2))
A custom decay function will need to to take in input
three parameters in the following order:
1. learning rate
2. current iteration
3. maximum number of iterations allowed
Note that if a lambda function is used to define the decay
MiniSom will not be pickable anymore.
neighborhood_function : function, optional (default='gaussian')
Function that weights the neighborhood of a position in the map
possible values: 'gaussian', 'mexican_hat', 'bubble'
random_seed : int, optional (default=None)
Random seed to use.
"""
if sigma >= x or sigma >= y:
warn('Warning: sigma is too high for the dimension of the map.')
self._random_generator = random.RandomState(random_seed)
self._learning_rate = learning_rate
self._sigma = sigma
self._input_len = input_len
# random initialization
self._weights = self._random_generator.rand(x, y, input_len)*2-1
for i in range(x):
for j in range(y):
# normalization
norm = fast_norm(self._weights[i, j])
self._weights[i, j] = self._weights[i, j] / norm
self._activation_map = zeros((x, y))
self._neigx = arange(x)
self._neigy = arange(y) # used to evaluate the neighborhood function
self._decay_function = decay_function
neig_functions = {'gaussian': self._gaussian,
'mexican_hat': self._mexican_hat,
'bubble': self._bubble,
'triangle': self._triangle}
if neighborhood_function not in neig_functions:
msg = '%s not supported. Functions available: %s'
raise ValueError(msg % (neighborhood_function,
', '.join(neig_functions.keys())))
if neighborhood_function in ['triangle',
'bubble'] and divmod(sigma, 1)[1] != 0:
warn('sigma should be an integer when triangle or bubble' +
'are used as neighborhood function')
self.neighborhood = neig_functions[neighborhood_function]
def get_weights(self):
"""Returns the weights of the neural network"""
return self._weights
def _activate(self, x):
"""Updates matrix activation_map, in this matrix
the element i,j is the response of the neuron i,j to x"""
s = subtract(x, self._weights) # x - w
it = nditer(self._activation_map, flags=['multi_index'])
while not it.finished:
# || x - w ||
self._activation_map[it.multi_index] = fast_norm(s[it.multi_index])
it.iternext()
def activate(self, x):
"""Returns the activation map to x"""
self._activate(x)
return self._activation_map
def _gaussian(self, c, sigma):
"""Returns a Gaussian centered in c"""
d = 2*pi*sigma*sigma
ax = exp(-power(self._neigx-c[0], 2)/d)
ay = exp(-power(self._neigy-c[1], 2)/d)
return outer(ax, ay) # the external product gives a matrix
def _mexican_hat(self, c, sigma):
"""Mexican hat centered in c"""
xx, yy = meshgrid(self._neigx, self._neigy)
p = power(xx-c[0], 2) + power(yy-c[1], 2)
d = 2*pi*sigma*sigma
return exp(-p/d)*(1-2/d*p)
def _bubble(self, c, sigma):
"""Constant function centered in c with spread sigma.
sigma should be an odd value,
"""
ax = logical_and(self._neigx > c[0]-sigma,
self._neigx < c[0]+sigma)
ay = logical_and(self._neigy > c[1]-sigma,
self._neigy < c[1]+sigma)
return outer(ax, ay)*1.
def _triangle(self, c, sigma):
"""Triangular function centered in c with spread sigma."""
triangle_x = (-abs(c[0] - self._neigx)) + sigma
triangle_y = (-abs(c[1] - self._neigy)) + sigma
triangle_x[triangle_x < 0] = 0.
triangle_y[triangle_y < 0] = 0.
return outer(triangle_x, triangle_y)
def _check_iteration_number(self, num_iteration):
if num_iteration < 1:
raise ValueError('num_iteration must be > 1')
def _check_input_len(self, data):
"""Checks that the data in input is of the correct shape."""
data_len = len(data[0])
if self._input_len != data_len:
msg = 'Received %d features, expected %d.' % (data_len,
self._input_len)
raise ValueError(msg)
def winner(self, x):
"""Computes the coordinates of the winning neuron for the sample x"""
self._activate(x)
return unravel_index(self._activation_map.argmin(),
self._activation_map.shape)
def update(self, x, win, eta, sig):
"""Updates the weights of the neurons.
Parameters
----------
x : np.array
Current pattern to learn
win : tuple
Position of the winning neuron for x (array or tuple)
"""
# improves the performances
g = self.neighborhood(win, sig) * eta
it = nditer(g, flags=['multi_index'])
while not it.finished:
# eta * neighborhood_function * (x-w)
x_w = (x - self._weights[it.multi_index])
self._weights[it.multi_index] += g[it.multi_index] * x_w
# normalization
norm = fast_norm(self._weights[it.multi_index])
self._weights[it.multi_index] = self._weights[it.multi_index] / norm
it.iternext()
def quantization(self, data):
"""Assigns a code book (weights vector of the winning neuron)
to each sample in data."""
self._check_input_len(data)
q = zeros(data.shape)
for i, x in enumerate(data):
q[i] = self._weights[self.winner(x)]
return q
def random_weights_init(self, data):
"""Initializes the weights of the SOM
picking random samples from data"""
self._check_input_len(data)
it = nditer(self._activation_map, flags=['multi_index'])
while not it.finished:
rand_i = self._random_generator.randint(len(data))
self._weights[it.multi_index] = data[rand_i]
norm = fast_norm(self._weights[it.multi_index])
self._weights[it.multi_index] = self._weights[it.multi_index]/norm
it.iternext()
def pca_weights_init(self, data):
"""Initializes the weights to span the first two principal components.
This initialization doesn't depend on random processes and
makes the training process converge faster.
It is strongly reccomended to normalize the data before initializing
the weights and use the same normalization for the training data.
"""
if self._input_len == 1:
msg = 'The data needs at least 2 features for pca initialization'
raise ValueError(msg)
self._check_input_len(data)
if len(self._neigx) == 1 or len(self._neigy) == 1:
msg = 'PCA initialization inappropriate:' + \
'One of the dimensions of the map is 1.'
warn(msg)
pc_length, pc = linalg.eig(cov(transpose(data)))
pc_order = argsort(pc_length)
for i, c1 in enumerate(linspace(-1, 1, len(self._neigx))):
for j, c2 in enumerate(linspace(-1, 1, len(self._neigy))):
self._weights[i, j] = c1*pc[pc_order[0]] + c2*pc[pc_order[1]]
def train_random(self, data, num_iteration, verbose=False):
"""Trains the SOM picking samples at random from data"""
self._check_iteration_number(num_iteration)
self._check_input_len(data)
iterations = range(num_iteration)
if verbose:
iterations = _incremental_index_verbose(num_iteration)
for iteration in iterations:
# pick a random sample
rand_i = self._random_generator.randint(len(data))
eta = self._decay_function(self._learning_rate, iteration, num_iteration)
# sigma and learning rate decrease with the same rule
sig = self._decay_function(self._sigma, iteration, num_iteration)
self.update(data[rand_i], self.winner(data[rand_i]),
eta, sig)
def train_batch(self, data, num_iteration, verbose=False):
"""Trains using all the vectors in data sequentially"""
self._check_iteration_number(num_iteration)
self._check_input_len(data)
iterations = range(num_iteration)
if verbose:
iterations = _incremental_index_verbose(num_iteration)
for iteration in iterations:
idx = iteration % (len(data)-1)
eta = self._decay_function(self._learning_rate, iteration, num_iteration)
# sigma and learning rate decrease with the same rule
sig = self._decay_function(self._sigma, iteration, num_iteration)
self.update(data[idx], self.winner(data[idx]),
eta, sig)
def train_delta(self, data, delta, max_iteration, verbose=False, decay_lr=None, decay_sigma=None, delta_func='mean'):
"""Trains using all the vectors in data sequentially"""
# num_iteration = len(data)
self._check_iteration_number(max_iteration)
self._check_input_len(data)
iterations = range(max_iteration)
if verbose:
iterations = _incremental_index_verbose(max_iteration)
delta_w = 1
eta = self._learning_rate
sig = self._sigma
for iteration in iterations:
# pick a random sample
old_weights = self._weights.copy()
if decay_lr != None:
eta = decay_lr(self._learning_rate, iteration, max_iteration)
else:
eta = self._decay_function(self._learning_rate, iteration, max_iteration)
if decay_sigma != None:
sig = decay_sigma(self._sigma, iteration, max_iteration)
else:
sig = self._decay_function(self._sigma, iteration, max_iteration)
rand_i = self._random_generator.randint(len(data))
self.update(data[rand_i], self.winner(data[rand_i]), eta, sig)
# max_new = np.max(self._weights)
if delta_func == 'mean':
delta_w = np.mean(np.abs(old_weights - self._weights))
else:
delta_w = np.max(np.abs(old_weights - self._weights))
#delta_w = np.max(np.abs(old_weights - self._weights))
if verbose:
print(f"iteration= {iteration} delta = {delta_w}")
plt.plot(iteration, delta_w, 'bo')
plt.xlabel('iteration')
plt.ylabel('delta_w')
if delta_w < delta:
break
def distance_map(self):
"""Returns the distance map of the weights.
Each cell is the normalised sum of the distances between
a neuron and its neighbours."""
um = zeros((self._weights.shape[0], self._weights.shape[1]))
it = nditer(um, flags=['multi_index'])
while not it.finished:
for ii in range(it.multi_index[0]-1, it.multi_index[0]+2):
for jj in range(it.multi_index[1]-1, it.multi_index[1]+2):
if (ii >= 0 and ii < self._weights.shape[0] and
jj >= 0 and jj < self._weights.shape[1]):
w_1 = self._weights[ii, jj, :]
w_2 = self._weights[it.multi_index]
um[it.multi_index] += fast_norm(w_1-w_2)
it.iternext()
um = um/um.max()
return um
def activation_response(self, data):
"""
Returns a matrix where the element i,j is the number of times
that the neuron i,j have been winner.
"""
self._check_input_len(data)
a = zeros((self._weights.shape[0], self._weights.shape[1]))
for x in data:
a[self.winner(x)] += 1
return a
def quantization_error(self, data):
"""Returns the quantization error computed as the average
distance between each input sample and its best matching unit."""
self._check_input_len(data)
error = 0
for x in data:
error += fast_norm(x-self._weights[self.winner(x)])
return error/len(data)
def win_map(self, data):
"""Returns a dictionary wm where wm[(i,j)] is a list
with all the patterns that have been mapped in the position i,j."""
self._check_input_len(data)
winmap = defaultdict(list)
for x in data:
winmap[self.winner(x)].append(x)
return winmap
def labels_map(self, data, labels):
"""Returns a dictionary wm where wm[(i,j)] is a dictionary
that contains the number of samples from a given label
that have been mapped in position i,j.
Parameters
----------
data : data matrix
label : list or array that contains the label of each sample in data.
"""
self._check_input_len(data)
winmap = defaultdict(list)
for x, l in zip(data, labels):
winmap[self.winner(x)].append(l)
for position in winmap:
winmap[position] = Counter(winmap[position])
return winmap
class TestMinisom(unittest.TestCase):
def setUp(self):
self.som = MiniSom(5, 5, 1)
for i in range(5):
for j in range(5):
# checking weights normalization
assert_almost_equal(1.0, linalg.norm(self.som._weights[i, j]))
self.som._weights = zeros((5, 5)) # fake weights
self.som._weights[2, 3] = 5.0
self.som._weights[1, 1] = 2.0
def test_decay_function(self):
assert self.som._decay_function(1., 2., 3.) == 1./(1.+2./(3./2))
def test_fast_norm(self):
assert fast_norm(array([1, 3])) == sqrt(1+9)
def test_check_input_len(self):
with self.assertRaises(ValueError):
self.som.train_batch([[1, 2]], 1)
with self.assertRaises(ValueError):
self.som.random_weights_init(array([[1, 2]]))
with self.assertRaises(ValueError):
self.som._check_input_len(array([[1, 2]]))
self.som._check_input_len(array([[1]]))
self.som._check_input_len([[1]])
def test_unavailable_neigh_function(self):
with self.assertRaises(ValueError):
MiniSom(5, 5, 1, neighborhood_function='boooom')
def test_gaussian(self):
bell = self.som._gaussian((2, 2), 1)
assert bell.max() == 1.0
assert bell.argmax() == 12 # unravel(12) = (2,2)
def test_mexican_hat(self):
bell = self.som._mexican_hat((2, 2), 1)
assert bell.max() == 1.0
assert bell.argmax() == 12 # unravel(12) = (2,2)
def test_bubble(self):
bubble = self.som._bubble((2, 2), 1)
assert bubble[2, 2] == 1
assert sum(sum(bubble)) == 1
def test_triangle(self):
bubble = self.som._triangle((2, 2), 1)
assert bubble[2, 2] == 1
assert sum(sum(bubble)) == 1
def test_win_map(self):
winners = self.som.win_map([[5.0], [2.0]])
assert winners[(2, 3)][0] == [5.0]
assert winners[(1, 1)][0] == [2.0]
def test_labels_map(self):
labels_map = self.som.labels_map([[5.0], [2.0]], ['a', 'b'])
assert labels_map[(2, 3)]['a'] == 1
assert labels_map[(1, 1)]['b'] == 1
def test_activation_reponse(self):
response = self.som.activation_response([[5.0], [2.0]])
assert response[2, 3] == 1
assert response[1, 1] == 1
def test_activate(self):
assert self.som.activate(5.0).argmin() == 13.0 # unravel(13) = (2,3)
def test_quantization_error(self):
assert self.som.quantization_error([[5], [2]]) == 0.0
assert self.som.quantization_error([[4], [1]]) == 1.0
def test_quantization(self):
q = self.som.quantization(array([[4], [2]]))
assert q[0] == 5.0
assert q[1] == 2.0
def test_random_seed(self):
som1 = MiniSom(5, 5, 2, sigma=1.0, learning_rate=0.5, random_seed=1)
som2 = MiniSom(5, 5, 2, sigma=1.0, learning_rate=0.5, random_seed=1)
# same initialization
assert_array_almost_equal(som1._weights, som2._weights)
data = random.rand(100, 2)
som1 = MiniSom(5, 5, 2, sigma=1.0, learning_rate=0.5, random_seed=1)
som1.train_random(data, 10)
som2 = MiniSom(5, 5, 2, sigma=1.0, learning_rate=0.5, random_seed=1)
som2.train_random(data, 10)
# same state after training
assert_array_almost_equal(som1._weights, som2._weights)
def test_train_batch(self):
som = MiniSom(5, 5, 2, sigma=1.0, learning_rate=0.5, random_seed=1)
data = array([[4, 2], [3, 1]])
q1 = som.quantization_error(data)
som.train_batch(data, 10)
assert q1 > som.quantization_error(data)
data = array([[1, 5], [6, 7]])
q1 = som.quantization_error(data)
som.train_batch(data, 10, verbose=True)
assert q1 > som.quantization_error(data)
def test_train_random(self):
som = MiniSom(5, 5, 2, sigma=1.0, learning_rate=0.5, random_seed=1)
data = array([[4, 2], [3, 1]])
q1 = som.quantization_error(data)
som.train_random(data, 10)
assert q1 > som.quantization_error(data)
data = array([[1, 5], [6, 7]])
q1 = som.quantization_error(data)
som.train_random(data, 10, verbose=True)
assert q1 > som.quantization_error(data)
def test_random_weights_init(self):
som = MiniSom(2, 2, 2, random_seed=1)
som.random_weights_init(array([[1.0, .0]]))
for w in som._weights:
assert_array_equal(w[0], array([1.0, .0]))
def test_pca_weights_init(self):
som = MiniSom(2, 2, 2)
som.pca_weights_init(array([[1., 0.], [0., 1.], [1., 0.], [0., 1.]]))
expected = array([[[0., -1.41421356], [1.41421356, 0.]],
[[-1.41421356, 0.], [0., 1.41421356]]])
assert_array_almost_equal(som._weights, expected)
def test_distance_map(self):
som = MiniSom(2, 2, 2, random_seed=1)
som._weights = array([[[1., 0.], [0., 1.]], [[1., 0.], [0., 1.]]])
assert_array_equal(som.distance_map(), array([[1., 1.], [1., 1.]]))