-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexemplars.py
201 lines (172 loc) · 6.75 KB
/
exemplars.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
import copy
import random
class Exemplars(object):
"""
Generates the exemplars for the numbers 0 through 8, returning them as either 10x10 matrices, or
vectors of length 100.
"""
def __init__(self):
self.__zero = [
[-1, -1, -1, 1, 1, 1, 1, -1, -1, -1],
[-1, -1, 1, 1, -1, -1, 1, 1, -1, -1],
[-1, 1, 1, -1, -1, -1, -1, 1, 1, -1],
[-1, 1, 1, -1, -1, -1, -1, 1, 1, -1],
[-1, 1, 1, -1, -1, -1, -1, 1, 1, -1],
[-1, 1, 1, -1, -1, -1, -1, 1, 1, -1],
[-1, 1, 1, -1, -1, -1, -1, 1, 1, -1],
[-1, 1, 1, -1, -1, -1, -1, 1, 1, -1],
[-1, -1, 1, 1, -1, -1, 1, 1, -1, -1],
[-1, -1, -1, 1, 1, 1, 1, -1, -1, -1]
]
self.__one = [
[-1, -1, 1, 1, -1, -1, -1, -1, -1, -1],
[-1, -1, 1, 1, -1, -1, -1, -1, -1, -1],
[-1, -1, 1, 1, -1, -1, -1, -1, -1, -1],
[-1, -1, 1, 1, -1, -1, -1, -1, -1, -1],
[-1, -1, 1, 1, -1, -1, -1, -1, -1, -1],
[-1, -1, 1, 1, -1, -1, -1, -1, -1, -1],
[-1, -1, 1, 1, -1, -1, -1, -1, -1, -1],
[-1, -1, 1, 1, -1, -1, -1, -1, -1, -1],
[-1, -1, 1, 1, -1, -1, -1, -1, -1, -1],
[-1, -1, 1, 1, -1, -1, -1, -1, -1, -1],
]
self.__two = [
[-1, -1, 1, 1, 1, 1, 1, 1, 1, 1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, 1, 1, 1, 1, 1, 1, 1, 1],
[-1, -1, 1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, 1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, 1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, 1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, 1, 1, 1, 1, 1, 1, 1, 1],
]
self.__three = [
[-1, -1, 1, 1, 1, 1, 1, 1, 1, 1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, 1, 1, 1, 1, 1, 1, 1, 1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, 1, 1, 1, 1, 1, 1, 1, 1],
]
self.__four = [
[-1, -1, -1, 1, -1, -1, -1, -1, -1, 1],
[-1, -1, -1, 1, -1, -1, -1, -1, -1, 1],
[-1, -1, -1, 1, -1, -1, -1, -1, -1, 1],
[-1, -1, -1, 1, -1, -1, -1, -1, -1, 1],
[-1, -1, -1, 1, -1, -1, -1, -1, -1, 1],
[-1, -1, -1, 1, 1, 1, 1, 1, 1, 1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, 1],
]
self.__five = [
[1, 1, 1, 1, 1, 1, 1, -1, -1, -1],
[1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[1, 1, 1, 1, 1, 1, 1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, 1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, 1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, 1, -1, -1, -1],
[1, -1, -1, -1, -1, -1, 1, -1, -1, -1],
[1, 1, 1, 1, 1, 1, 1, -1, -1, -1],
]
self.__six = [
[-1, -1, 1, 1, 1, 1, 1, 1, 1, 1],
[-1, -1, 1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, 1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, 1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, 1, 1, 1, 1, 1, 1, 1, 1],
[-1, -1, 1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, 1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, 1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, 1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, 1, 1, 1, 1, 1, 1, 1, 1],
]
self.__seven = [
[-1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, 1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, 1],
]
@property
def exemplars(self):
return list([
self.__zero,
self.__one,
self.__two,
self.__three,
self.__four,
self.__five,
self.__six,
self.__seven
])
@staticmethod
def get_exemplars(as_matrices=False):
x = Exemplars()
if as_matrices:
return x.exemplars
else:
exemplars = []
for exemplar in x.exemplars:
v_exemplar = [col for row in exemplar for col in row]
exemplars.append(v_exemplar)
return exemplars
@staticmethod
def to_matrix(v_exemplar):
"""
Convert the vector representation of an exemplar to its 10x10 matrix form
:param v_exemplar: list of 100 elements representing the exemplar
:return: the exemplar in its 10x10 matrix form
"""
assert(len(v_exemplar) == 100)
return list([
v_exemplar[0:10],
v_exemplar[10:20],
v_exemplar[20:30],
v_exemplar[30:40],
v_exemplar[40:50],
v_exemplar[50:60],
v_exemplar[60:70],
v_exemplar[70:80],
v_exemplar[80:90],
v_exemplar[90:]
])
@staticmethod
def to_vector(exemplar):
"""
Convert the matrix representation of the exemplar to its vector form (100 elements)
:param exemplar: 10x10 matrix form the exemplar
:return: a 100 element list representing the vector for that exemplar
"""
assert(len(exemplar) == 10)
return list([col for row in exemplar for col in row])
@staticmethod
def add_noise(exemplar, p=.25):
"""
Add random noise to an exemplar by flipping the value at each index (-1 => 1, 1 => -1) with probability p.
:param exemplar: exemplar to add noise to
:param p: probability with which to add noise
:return: a copy of the exemplar with noise added
"""
def flip_bit(x):
i = random.random()
if i <= p:
return x * -1
else:
return x
noisy_exemplar = copy.deepcopy(exemplar)
return [flip_bit(x) for x in noisy_exemplar]