-
Notifications
You must be signed in to change notification settings - Fork 3
/
BinaryHopfieldNN.py
210 lines (175 loc) · 4.22 KB
/
BinaryHopfieldNN.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
zero="""
XXXXXXXX
X______X
X______X
X______X
X______X
X______X
X______X
XXXXXXXX
"""
one="""
____X___
____X___
____X___
____X___
____X___
____X___
____X___
____X___
"""
two="""
XXXXXXXX
_______X
_______X
XXXXXXXX
X_______
X_______
XXXXXXXX
________
"""
three="""
________
XXXXXXXX
_______X
_______X
XXXXXXXX
_______X
_______X
XXXXXXXX
"""
def bi_vec_pattern(numeral):
from numpy import array
return array([+1 if i=='X' else -1 for i in numeral.replace('\n','')])
bipolar_0=bi_vec_pattern(zero)
bipolar_1=bi_vec_pattern(one)
bipolar_2=bi_vec_pattern(two)
bipolar_3=bi_vec_pattern(three)
def visualize(pattern):
from pylab import imshow, cm, show
imshow(pattern.reshape((8,8)),cmap=cm.binary, interpolation='nearest')
show()
visualize(bipolar_0)
visualize(bipolar_1)
visualize(bipolar_2)
visualize(bipolar_3)
import numpy as np
#store sample patterns as rows of the matrix sample_patterns
sample_patterns = np.array([bipolar_0,bipolar_1,bipolar_2,bipolar_3])
def network_train(sample_patterns):
from numpy import zeros, outer, diag_indices
i=sample_patterns.shape[1]
j=sample_patterns.shape[0]
T = zeros((i,i)) #create 8*8 zero matrix
#weight matrix as summations of outer products of sample vectors.
for s in sample_patterns:
T = T + outer(s,s)
#set diagonal entries to zero to avoid self inputs.
T[diag_indices(i)] = 0
return T/j #normalization
T=network_train(sample_patterns)
from numpy import vectorize, dot
sgn = vectorize(lambda x: -1 if x<0 else +1)
def classify(T, test_patterns, iteration_steps):
for i in xrange(iteration_steps):
test_patterns = sgn(dot(test_patterns,T))
return test_patterns
zero_test_1="""
XXXXXXX_
X______X
X___X___
X_______
X______X
_______X
X______X
XXXXXX_X
"""
one_test_1="""
_____XXX
_______X
_______X
________
_______X
_______X
_______X
______X_
"""
two_test_1="""
XXXXXXXX
______X_
_______X
X__XXXXX
X_______
X_______
XXXXXXXX
________
"""
three_test_1="""
________
X___XXXX
_______X
_______X
X_XXXXXX
_______X
__X____X
XX_XXXXX
"""
#Stores test patterns as rows in test_patterns matrix.
test_patterns_1= np.array([bi_vec_pattern(zero_test_1),
bi_vec_pattern(one_test_1),
bi_vec_pattern(two_test_1),
bi_vec_pattern(three_test_1)])
for i in range(4):
visualize(test_patterns_1[i])
result_1=classify(T, test_patterns_1, 1)
result_2=classify(T, test_patterns_1, 2)
result_3=classify(T, test_patterns_1, 3)
for i in range(4):
visualize(result_1[i])
for i in range(4):
visualize(result_2[i])
for i in range(4):
visualize(result_3[i])
from sklearn.datasets import load_digits
#8*8 images of digits data set from sklearn
digits = load_digits()
print(digits.data.shape)
def to_bipolar(img, lower, upper): #convert images to bipolar arrays
img=(lower < img) & (img < upper)
img=img.astype(int)
img=np.asarray([j for i in img for j in i])
img[img==0]=-1
return img
zero_test_3=to_bipolar(digits.images[0],10,100)
one_test_3=to_bipolar(digits.images[1],10,100)
two_test_3=to_bipolar(digits.images[2],10,100)
three_test_3=to_bipolar(digits.images[3],10,100)
visualize(zero_test_3)
visualize(one_test_3)
visualize(two_test_3)
visualize(three_test_3)
test_patterns_3 = [zero_test_3, one_test_3,two_test_3,three_test_3]
result1_=classify(T, test_patterns_3, 1)
result2_=classify(T, test_patterns_3, 2)
for i in range(4):
visualize(result_1[i])
import random
for i in range(64):
while random.random()<0.4:
bipolar_0[i]=-bipolar_0[i]
bipolar_1[i]=-bipolar_1[i]
bipolar_2[i]=-bipolar_2[i]
bipolar_3[i]=-bipolar_3[i]
a,b,c,d=bipolar_0,bipolar_1,bipolar_2,bipolar_3
test_patternsw= np.array([a,b,c,d])
for i in [a,b,c,d]:
visualize(i)
r1=classify(T, test_patternsw, 1)
r2=classify(T, test_patternsw, 2)
r3=classify(T, test_patternsw, 3)
for i in range(4):
visualize(r1[i])
for i in range(4):
visualize(r2[i])
for i in range(4):
visualize(r3[i])