-
Notifications
You must be signed in to change notification settings - Fork 4
/
figure-color.py
117 lines (100 loc) · 3.92 KB
/
figure-color.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2009 Nicolas Rougier - INRIA - CORTEX Project
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Contact: CORTEX Project - INRIA
# INRIA Lorraine,
# Campus Scientifique, BP 239
# 54506 VANDOEUVRE-LES-NANCY CEDEX
# FRANCE
def plot(axes, net):
classname = net.__class__.__name__
axes.set_xticks([])
axes.set_yticks([])
divider = make_axes_locatable(axes)
subaxes = divider.new_vertical(1.0, pad=0.4, sharex=axes)
fig.add_axes(subaxes)
subaxes.set_xticks([])
subaxes.yaxis.set_major_locator(matplotlib.ticker.MaxNLocator(2))
subaxes.yaxis.set_ticks_position('right')
subaxes.set_ylabel('Distortion')
subaxes.set_xlabel('Time')
Y = net.distortion[::1]
X = np.arange(len(Y))/float(len(Y)-1)
subaxes.plot(X,Y)
if classname == 'NG':
plt.title('Neural Gas', fontsize=20)
elif classname == 'SOM':
plt.title('Self-Organizing Map', fontsize=20)
elif classname == 'DSOM':
plt.title('Dynamic Self-Organizing Map', fontsize=20)
axes.axis([0,1,0,1])
axes.set_aspect(1)
codebook = net.codebook
axes.imshow(codebook, interpolation='nearest')
#interpolation='bicubic')
if classname == 'NG':
axes.text(0.5, -0.01,
r'$\lambda_i = %.3f,\lambda_f = %.3f, \varepsilon_i=%.3f, \varepsilon_f=%.3f$' % (
net.sigma_i, net.sigma_f, net.lrate_i, net.lrate_f),
fontsize=16,
horizontalalignment='center',
verticalalignment='top',
transform = axes.transAxes)
if classname == 'SOM':
axes.text(0.5, -0.01,
r'$\sigma_i = %.3f,\sigma_f = %.3f, \varepsilon_i=%.3f, \varepsilon_f=%.3f$' % (
net.sigma_i, net.sigma_f, net.lrate_i, net.lrate_f),
fontsize=16,
horizontalalignment='center',
verticalalignment='top',
transform = axes.transAxes)
elif classname == 'DSOM':
axes.text(0.5, -0.01,
r'$elasticity = %.2f$' % (net.elasticity),
fontsize=16,
horizontalalignment='center',
verticalalignment='top',
transform = axes.transAxes)
if __name__ == '__main__':
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid import make_axes_locatable
from mpl_toolkits.axes_grid import AxesGrid
from network import NG,SOM,DSOM
n = 60
epochs = 5000
N = 5000
np.random.seed(123)
samples = np.zeros((N,3))
samples[:,0] = np.random.uniform(low=0,high=1,size=N)
samples[:,1] = np.random.uniform(low=0,high=1,size=N)
samples[:,2] = np.random.uniform(low=0,high=1,size=N)
samples[:,0] = np.random.randint(low=0,high=3,size=N)/2.
samples[:,1] = np.random.randint(low=0,high=3,size=N)/2.
samples[:,2] = np.random.randint(low=0,high=3,size=N)/2.
print 'Dynamic Self-Organizing Map'
np.random.seed(123)
dsom = DSOM((n,n,3), elasticity=1.0, init_method='fixed')
dsom.learn(samples, epochs)
fig = plt.figure(figsize=(8,8))
axes = plt.subplot(111)
plot(axes,dsom)
fig.savefig('color.png', dpi=150)
#plt.show()