-
Notifications
You must be signed in to change notification settings - Fork 74
/
art1.py
202 lines (180 loc) · 6.06 KB
/
art1.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
#!/usr/bin/env python
# -----------------------------------------------------------------------------
# Adaptive Resonance Theory
# Copyright (C) 2011 Nicolas P. Rougier
#
# Distributed under the terms of the BSD License.
# -----------------------------------------------------------------------------
# Reference: Grossberg, S. (1987)
# Competitive learning: From interactive activation to
# adaptive resonance, Cognitive Science, 11, 23-63
#
# Requirements: python 2.5 or above => http://www.python.org
# numpy 1.0 or above => http://numpy.scipy.org
# -----------------------------------------------------------------------------
from __future__ import print_function
from __future__ import division
import numpy as np
class ART:
''' ART class
Usage example:
--------------
# Create a ART network with input of size 5 and 20 internal units
>>> network = ART(5,10,0.5)
'''
def __init__(self, n=5, m=10, rho=.5):
'''
Create network with specified shape
Parameters:
-----------
n : int
Size of input
m : int
Maximum number of internal units
rho : float
Vigilance parameter
'''
# Comparison layer
self.F1 = np.ones(n)
# Recognition layer
self.F2 = np.ones(m)
# Feed-forward weights
self.Wf = np.random.random((m,n))
# Feed-back weights
self.Wb = np.random.random((n,m))
# Vigilance
self.rho = rho
# Number of active units in F2
self.active = 0
def learn(self, X):
''' Learn X '''
# Compute F2 output and sort them (I)
self.F2[...] = np.dot(self.Wf, X)
I = np.argsort(self.F2[:self.active].ravel())[::-1]
for i in I:
# Check if nearest memory is above the vigilance level
d = (self.Wb[:,i]*X).sum()/X.sum()
if d >= self.rho:
# Learn data
self.Wb[:,i] *= X
self.Wf[i,:] = self.Wb[:,i]/(0.5+self.Wb[:,i].sum())
return self.Wb[:,i], i
# No match found, increase the number of active units
# and make the newly active unit to learn data
if self.active < self.F2.size:
i = self.active
self.Wb[:,i] *= X
self.Wf[i,:] = self.Wb[:,i]/(0.5+self.Wb[:,i].sum())
self.active += 1
return self.Wb[:,i], i
return None,None
# -----------------------------------------------------------------------------
if __name__ == '__main__':
np.random.seed(1)
# Example 1 : very simple data
# -------------------------------------------------------------------------
network = ART( 5, 10, rho=0.5)
data = [" O ",
" O O",
" O",
" O O",
" O",
" O O",
" O",
" OO O",
" OO ",
" OO O",
" OO ",
"OOO ",
"OO ",
"O ",
"OO ",
"OOO ",
"OOOO ",
"OOOOO",
"O ",
" O ",
" O ",
" O ",
" O",
" O O",
" OO O",
" OO ",
"OOO ",
"OO ",
"OOOO ",
"OOOOO"]
X = np.zeros(len(data[0]))
for i in range(len(data)):
for j in range(len(data[i])):
X[j] = (data[i][j] == 'O')
Z, k = network.learn(X)
print("|%s|"%data[i],"-> class", k)
# Example 2 : Learning letters
# -------------------------------------------------------------------------
def letter_to_array(letter):
''' Convert a letter to a numpy array '''
shape = len(letter), len(letter[0])
Z = np.zeros(shape, dtype=int)
for row in range(Z.shape[0]):
for column in range(Z.shape[1]):
if letter[row][column] == '#':
Z[row][column] = 1
return Z
def print_letter(Z):
''' Print an array as if it was a letter'''
for row in range(Z.shape[0]):
for col in range(Z.shape[1]):
if Z[row,col]:
print( '#', end="" )
else:
print( ' ', end="" )
print( )
A = letter_to_array( [' #### ',
'# #',
'# #',
'######',
'# #',
'# #',
'# #'] )
B = letter_to_array( ['##### ',
'# #',
'# #',
'##### ',
'# #',
'# #',
'##### '] )
C = letter_to_array( [' #### ',
'# #',
'# ',
'# ',
'# ',
'# #',
' #### '] )
D = letter_to_array( ['##### ',
'# #',
'# #',
'# #',
'# #',
'# #',
'##### '] )
E = letter_to_array( ['######',
'# ',
'# ',
'#### ',
'# ',
'# ',
'######'] )
F = letter_to_array( ['######',
'# ',
'# ',
'#### ',
'# ',
'# ',
'# '] )
samples = [A,B,C,D,E,F]
network = ART( 6*7, 10, rho=0.15 )
for i in range(len(samples)):
Z, k = network.learn(samples[i].ravel())
print("%c"%(ord('A')+i),"-> class",k)
print_letter(Z.reshape(7,6))