-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSoftmax.py
executable file
·311 lines (276 loc) · 13 KB
/
Softmax.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
from brian2 import *
import numpy as np
import scipy as sp
from scipy import stats
import struct
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
warnings.filterwarnings("ignore")
data_path = '../../../Data/MNIST_data/'
class Base():
def __init__(self, duration, dt):
self.duration = duration
self.dt = dt
self.interval = duration * dt
def get_states(self, input, running_time, sample, normalize=False):
n = int(running_time / self.interval)
step = int(self.interval / sample / defaultclock.dt)
interval_ = int(self.interval / defaultclock.dt)
temp = []
for i in range(n):
sum = np.sum(input[:, i * interval_: (i + 1) * interval_][:, ::-step], axis=1)
temp.append(sum)
if normalize:
return MinMaxScaler().fit_transform(np.asarray(temp)).T
else:
return np.asarray(temp).T
def update_states(self, type='pandas', *args, **kwargs):
for seq, state in enumerate(kwargs):
if type == 'pandas':
kwargs[state] = kwargs[state].append(pd.DataFrame(args[seq]))
elif type == 'numpy':
kwargs[state] = self.np_extend(kwargs[state], args[seq], 1)
return kwargs
def normalization_min_max(self, arr):
arr_n = arr
for i in range(arr.size):
x = float(arr[i] - np.min(arr)) / (np.max(arr) - np.min(arr))
arr_n[i] = x
return arr_n
def mse(self, y_test, y):
return sp.sqrt(sp.mean((y_test - y) ** 2))
def classification(self, thea, data):
data_n = self.normalization_min_max(data)
data_class = []
for a in data_n:
if a >= thea:
b = 1
else:
b = 0
data_class.append(b)
return np.asarray(data_class), data_n
def allocate(self, G, X, Y, Z):
V = np.zeros((X, Y, Z), [('x', float), ('y', float), ('z', float)])
V['x'], V['y'], V['z'] = np.meshgrid(np.linspace(0, Y - 1, Y), np.linspace(0, X - 1, X),
np.linspace(0, Z - 1, Z))
V = V.reshape(X * Y * Z)
np.random.shuffle(V)
n = 0
for g in G:
for i in range(g.N):
g.x[i], g.y[i], g.z[i] = V[n][0], V[n][1], V[n][2]
n += 1
return G
def w_norm2(self, n_post, Synapsis):
for i in range(n_post):
a = Synapsis.w[np.where(Synapsis._synaptic_post == i)[0]]
Synapsis.w[np.where(Synapsis._synaptic_post == i)[0]] = a / np.linalg.norm(a)
def np_extend(self, a, b, axis=0):
if a is None:
shape = list(b.shape)
shape[axis] = 0
a = np.array([]).reshape(tuple(shape))
return np.append(a, b, axis)
def np_append(self, a, b):
shape = list(b.shape)
shape.insert(0, -1)
if a is None:
a = np.array([]).reshape(tuple(shape))
return np.append(a, b.reshape(tuple(shape)), axis=0)
def parameters_GS(self, *args, **kwargs):
#---------------
# args = [(min,max),]
# kwargs = {'parameter' = number,}
#---------------
parameters = np.zeros(tuple(kwargs.values()), [(x, float) for x in kwargs.keys()])
grids = np.meshgrid(*[np.linspace(min_max[0], min_max[1], scale)
for min_max,scale in zip(args,kwargs.values())], indexing='ij')
for index, parameter in enumerate(kwargs.keys()):
parameters[parameter] = grids[index]
parameters = parameters.reshape(-1)
return parameters
def set_local_parameter_PS(self, S, parameter, boundary = None, method='random', **kwargs):
if method == 'random':
random = rand(S.N_post) * (boundary[1]-boundary[0]) + boundary[0]
if '_post' in parameter:
S.variables[parameter].set_value(random)
else:
S.variables[parameter].set_value(random[S.j])
if method == 'group':
try:
group_n = kwargs['group_parameters'].shape[0]
n = int(np.floor(S.N_post / group_n))
random = zeros(S.N_post)
for i in range(group_n):
random[i * n:(i + 1) * n] = kwargs['group_parameters'][i]
for j in range(S.N_post - group_n*n):
random[group_n * n + j:group_n * n + j + 1] = random[j * n]
except KeyError:
group_n = kwargs['group_n']
n = int(np.floor(S.N_post / group_n))
random = zeros(S.N_post)
for i in range(group_n):
try:
random[i * n:(i + 1) * n] = rand() * (boundary[1]-boundary[0]) + boundary[0]
except IndexError:
random[i * n:] = rand() * (boundary[1]-boundary[0]) + boundary[0]
continue
if '_post' in parameter:
S.variables[parameter].set_value(random)
else:
S.variables[parameter].set_value(random[S.j])
if method == 'location':
group_n = kwargs['group_n']
location_label = kwargs['location_label']
random = zeros(S.N_post)
bound = np.linspace(0, max(S.variables[location_label].get_value() + 1), num=group_n + 1)
for i in range(group_n):
random[(S.variables[location_label].get_value() >= bound[i]) & (
S.variables[location_label].get_value() < bound[i + 1])] \
= rand() * (boundary[1]-boundary[0]) + boundary[0]
if '_post' in parameter:
S.variables[parameter].set_value(random)
else:
S.variables[parameter].set_value(random[S.j])
if method == 'in_coming':
max_incoming = max(S.N_incoming)
random = S.N_incoming / max_incoming * (boundary[1]-boundary[0]) + boundary[0]
if '_post' in parameter:
S.variables[parameter].set_value(random)
else:
S.variables[parameter].set_value(random[S.j])
class MNIST_classification(Base):
def __init__(self, shape, duration, dt):
super().__init__(duration, dt)
self.shape = shape
def load_Data_MNIST(self, n, path_value, path_label, is_norm=True):
with open(path_value, 'rb') as f1:
buf1 = f1.read()
with open(path_label, 'rb') as f2:
buf2 = f2.read()
image_index = 0
image_index += struct.calcsize('>IIII')
im = []
for i in range(n):
temp = struct.unpack_from('>784B', buf1, image_index)
im.append(np.reshape(temp, self.shape))
image_index += struct.calcsize('>784B')
label_index = 0
label_index += struct.calcsize('>II')
label = np.asarray(struct.unpack_from('>' + str(n) + 'B', buf2, label_index))
if is_norm:
f = lambda x: (x - np.min(x)) / (np.max(x) - np.min(x))
df = pd.DataFrame({'value': pd.Series(im).apply(f), 'label': pd.Series(label)})
else:
df = pd.DataFrame({'value': pd.Series(im), 'label': pd.Series(label)})
return df
def load_Data_MNIST_all(self, path, is_norm=True):
self.train = self.load_Data_MNIST(60000, path + 'train-images.idx3-ubyte',
path + 'train-labels.idx1-ubyte', is_norm)
self.test = self.load_Data_MNIST(10000, path + 't10k-images.idx3-ubyte',
path + 't10k-labels.idx1-ubyte', is_norm)
def select_data(self, fraction, data_frame, is_order=True, **kwargs):
try:
selected = kwargs['selected']
except KeyError:
selected = np.arange(10)
if is_order:
data_frame_selected = data_frame[data_frame['label'].isin(selected)].sample(
frac=fraction).sort_index().reset_index(drop=True)
else:
data_frame_selected = data_frame[data_frame['label'].isin(selected)].sample(frac=fraction).reset_index(
drop=True)
return data_frame_selected
def _encoding_cos_rank(self, x, n, A):
encoding = np.zeros((x.shape[0] * A, n * x.shape[1]), dtype='<i1')
for i in range(int(n)):
trans_cos = np.around(0.5 * A * (np.cos(x + np.pi * (i / n)) + 1)).clip(0, A - 1)
for index_0, p in enumerate(trans_cos):
for index_1, q in enumerate(p):
encoding[int(q)+ A * index_0, index_1 * n + i] = 1
return encoding
def _encoding_cos_rank_ignore_0(self, x, n, A):
encoding = np.zeros((x.shape[0] * A, n * x.shape[1]), dtype='<i1')
for i in range(int(n)):
trans_cos = np.around(0.5 * A * (np.cos(x + np.pi * (i / n)) + 1)).clip(0, A - 1)
encoded_zero = int(np.around(0.5 * A * (np.cos(0 + np.pi * (i / n)) + 1)).clip(0, A - 1))
for index_0, p in enumerate(trans_cos):
for index_1, q in enumerate(p):
if int(q) == encoded_zero:
continue
else:
encoding[int(q)+ A * index_0, index_1 * n + i] = 1
return encoding
def encoding_latency_MNIST(self, coding_f, analog_data, coding_n, min=0, max=np.pi):
f = lambda x: (max - min) * (x - np.min(x)) / (np.max(x) - np.min(x))
coding_duration = self.duration / self.shape[0]
if (coding_duration - int(coding_duration)) == 0.0:
value = analog_data['value'].apply(f).apply(coding_f, n=coding_n, A=int(coding_duration))
return pd.DataFrame({'value': pd.Series(value), 'label': pd.Series(analog_data['label'])})
else:
raise ValueError('duration must divide (coding_n*length of data) exactly')
def get_series_data(self, data_frame, is_group=False):
data_frame_s = None
if not is_group:
for value in data_frame['value']:
data_frame_s = self.np_extend(data_frame_s, value, 0)
else:
for value in data_frame['value']:
data_frame_s = self.np_append(data_frame_s, value)
label = data_frame['label']
return data_frame_s, label
def get_series_data_list(self, data_frame, is_group=False):
data_frame_s = []
if not is_group:
for value in data_frame['value']:
data_frame_s.extend(value)
else:
for value in data_frame['value']:
data_frame_s.append(value)
label = data_frame['label']
return np.asarray(data_frame_s), label
def run_for_average(seed):
result = []
for s in seed:
np.random.seed(s)
###################################
# -----simulation parameter setting-------
coding_n = 3
MNIST_shape = (1, 784)
coding_duration = 30
duration = coding_duration*MNIST_shape[0]
F_train = 0.05
F_validation = 0.00833333
F_test = 0.05
Dt = defaultclock.dt = 1*ms
#-------class initialization----------------------
base = Base(duration, Dt)
MNIST = MNIST_classification(MNIST_shape, duration, Dt)
#-------data initialization----------------------
MNIST.load_Data_MNIST_all(data_path)
df_train_validation = MNIST.select_data(F_train+F_validation, MNIST.train)
df_train, df_validation = train_test_split(df_train_validation, test_size=F_validation/(F_validation+F_train),
random_state=42)
df_test = MNIST.select_data(F_test, MNIST.test)
df_test.value = df_test.value.apply(lambda x : x.reshape(-1,))
df_train.value = df_train.value.apply(lambda x : x.reshape(-1,))
df_validation.value = df_validation.value.apply(lambda x : x.reshape(-1,))
data_train_s, label_train = MNIST.get_series_data_list(df_train, is_group = True)
data_validation_s, label_validation = MNIST.get_series_data_list(df_validation, is_group = True)
data_test_s, label_test = MNIST.get_series_data_list(df_test, is_group = True)
print ("Start evaluating softmax regression model by sklearn...")
reg = LogisticRegression(solver="lbfgs", multi_class="multinomial")
reg.fit(data_train_s, label_train)
# np.savetxt('coef_softmax_sklearn.txt', reg.coef_, fmt='%.6f') # Save coefficients to a text file
train_y_predict = reg.predict(data_train_s)
print ("Accuracy of train set: %f" % accuracy_score(label_train, train_y_predict))
test_y_predict = reg.predict(data_test_s)
print ("Accuracy of test set: %f" % accuracy_score(label_test, test_y_predict))
result.append(accuracy_score(label_test, test_y_predict))
return result
if __name__ == '__main__':
print(run_for_average(np.arange(100,110)))
print(mean(run_for_average(np.arange(100, 200))))