forked from the-aerospace-corporation/brainblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhigher_order_learner.py
More file actions
445 lines (352 loc) · 17 KB
/
higher_order_learner.py
File metadata and controls
445 lines (352 loc) · 17 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import pathlib
import pickle
from time import time
from brainblocks.blocks import ContextLearner, DiscreteTransformer, PatternClassifier
import numpy as np
# convert letters to integer labels instead of byte values
from sklearn import preprocessing
# printing boolean arrays neatly
np.set_printoptions(precision=3, suppress=True, threshold=1000000, linewidth=512,
formatter={'bool': lambda bin_val: 'X' if bin_val else '-'})
def reduce_letters(text_data):
"""
Reduce the number of letters to consider, so we don't allocate states to chars we never see
:param text_data:
:return:
"""
# string to a list of char
text_array = [*text_data]
# Use scikit-learn to convert letters to discrete integers
char_to_int_encoder = preprocessing.LabelEncoder()
char_to_int_encoder.fit(text_array)
# create the input you would use for your machine learning model
text_as_integers = char_to_int_encoder.transform(text_array)
num_letters = len(char_to_int_encoder.classes_)
print(len(text_as_integers), "total characters")
print(f"{num_letters} distinct characters")
# get the count of each character
# unique, counts = np.unique(text_array, return_counts=True)
# all_freq = dict(zip(unique, counts))
# show each character's integer label and frequency
# print(f"char, index, count")
# for i in range(num_letters):
# char_letter = char_to_int_encoder.inverse_transform([i, ])[0]
# print(repr(char_letter), i, all_freq[char_letter])
return char_to_int_encoder, text_as_integers
if __name__ == "__main__":
# text_path = "data/t8.shakespeare.txt"
text_path = "data/sherlock.txt"
with open(text_path, 'r') as f:
raw_text = f.read()
# load a cached LabelEncoder if it exists, otherwise create one
root_name = pathlib.Path(text_path).stem
try:
with open(f"le_{root_name}.obj", "rb") as f:
char_to_int_encoder = pickle.load(f)
print("Loaded Cached Label Encoder")
integer_input_data = char_to_int_encoder.transform([*raw_text])
print("Converted Letters to Integers")
except FileNotFoundError as e:
print("Creating Label Encoder")
char_to_int_encoder, integer_input_data = reduce_letters(raw_text)
print("Converted Letters to Integers")
with open(f"le_{root_name}.obj", "wb") as f:
pickle.dump(char_to_int_encoder, f)
print("Cached Label Encoder")
scores = []
discrete_encodings = []
num_labels = len(char_to_int_encoder.classes_)
# number of bits total for the encoder
bits_per_discrete_state = 1
# bits_per_discrete_state = 32
num_s = num_labels * bits_per_discrete_state
# history_window = 1
history_window = 10
num_statelets_per_column = 10
# num_statelets_per_column = 30
# num_statelets_per_column = 100
d_thresh = 3
discrete_encoder = DiscreteTransformer(num_v=num_labels, num_s=num_s)
# for i in range(num_labels):
# discrete_encoder.set_value(i)
# discrete_encoder.feedforward()
# discrete_binary_array = np.array(discrete_encoder.output.bits, dtype=bool)
# discrete_encodings.append(discrete_binary_array)
#
# char_letter = char_to_int_encoder.inverse_transform([i, ])[0]
# print(i, repr(char_letter))
# print(discrete_binary_array)
print("DiscreteTransformer")
print("num_labels, init statelets, allocated statelets")
num_encoder_bits = len(discrete_encoder.output.bits)
print(num_labels, num_s, num_encoder_bits)
#num_rpd = 12, # number of receptors per dendrite
sl = ContextLearner(
num_c=num_encoder_bits, # number of columns
num_spc=num_statelets_per_column, # number of statelets per column
num_dps=10, # number of dendrites per statelet
num_rpd=12, # number of receptors per dendrite
d_thresh=d_thresh, # dendrite threshold
perm_thr=20, # receptor permanence threshold
perm_inc=2, # receptor permanence increment
perm_dec=1, # receptor permanence decrement
num_t=1 + history_window,
always_update=True)
sl_bits = len(sl.output.bits)
pc = PatternClassifier(
num_l=num_labels, # number of labels
num_s=256*num_labels, # number of statelets
num_as=16, # number of active statelets
perm_thr=20, # receptor permanence threshold
perm_inc=2, # receptor permanence increment
perm_dec=1, # receptor permanence decrement
pct_pool=0.8, # percent pooled
pct_conn=0.5, # percent initially connected
pct_learn=0.3) # percent learn
slabels = pc.get_statelet_labels()
slabel_str = f"0"
for s in range(len(slabels)):
char_letter = char_to_int_encoder.inverse_transform([slabels[s], ])[0]
slabel_str += f",{char_letter!r}"
slabel_str += "\n"
with open("slabel_states.csv", "w") as f:
f.write(slabel_str)
# hol = SequenceLearner(
# num_c=num_encoder_bits, # number of columns
# num_spc=num_statelets_per_column, # number of statelets per column
# num_dps=10, # number of dendrites per statelet
# num_rpd=12, # number of receptors per dendrite
# d_thresh=6, # dendrite threshold
# perm_thr=20, # receptor permanence threshold
# perm_inc=2, # receptor permanence increment
# perm_dec=1, # receptor permanence decrement
# num_t=2,
# always_update=True)
#
# hol_bits = len(hol.output.bits)
# hol_pp = PatternPooler(
# num_s=num_encoder_bits, # number of statelets
# num_as=8, # number of active statelets
# perm_thr=20, # receptor permanence threshold
# perm_inc=2, # receptor permanence increment
# perm_dec=1, # receptor permanence decrement
# pct_pool=0.8, # pooling percentage
# pct_conn=0.5, # initially connected percentage
# pct_learn=0.3) # learn percentage
# hol_pp_bits = len(hol_pp.output.bits)
# print("Bits for each block output")
# ic(sl_bits, hol_bits, hol_pp_bits)
# # HOL, history 0, no PP, configuration
# sl.input.add_child(discrete_encoder.output)
# hol.input.add_child(sl.output, 0)
# sl.context.add_child(hol.output, 0)
# # Standard SequenceLearner configuration
sl.input.add_child(discrete_encoder.output)
for i in range(1, history_window + 1):
sl.context.add_child(sl.output, i)
# # Standard Classifier configuration
pc.input.add_child(sl.output)
# //for (uint32_t i = 1 ; i < num_t ; i++ ) {
# // Connect context to previous output
# // context.add_child(&output, i);
# //}
# # HOL, history 0, with PP, configuration
# sl.input.add_child(discrete_encoder.output)
# hol_pp.input.add_child(sl.output)
# hol.input.add_child(hol_pp.output, 0)
# sl.context.add_child(hol.output, 0)
# # SL / concat HOL context, history 0, with PP, configuration
# sl.input.add_child(discrete_encoder.output)
# hol_pp.input.add_child(sl.output)
# hol.input.add_child(hol_pp.output, 0)
# sl.context.add_child(sl.output, 1)
# sl.context.add_child(hol.output, 0)
t0 = time()
# break_count = 5000
# with (open("sl_hol_pp_match_encoder_bits_sherlock.csv", 'w') as f):
# with (open("sl_hol_pp_match_encoder_bits_sherlock_scratch.csv", 'w') as f):
# with (open("sl_sherlock_random.csv", 'w') as f):
# with (open("sl_hol_pp_match_encoder_bits_sherlock_random.csv", 'w') as f):
# with (open("sl_sherlock_thin_history_10_random.csv", 'w') as f):
# with (open(f"sl_sherlock_wide_history_{history_window:02d}_random.csv", 'w') as f):
# output_csv = f"sl_sherlock_wide_{bits_per_discrete_state:02d}_deep_{num_statelets_per_column:03d}"
# output_csv += f"_history_{history_window:02d}_dthresh_{d_thresh:02d}_random.csv"
# sl_sherlock_wide_01_deep_100_history_10_dthresh_3_random.cs
# output_csv = f"seq_pred_sherlock_wide_{bits_per_discrete_state:02d}_deep_{num_statelets_per_column:03d}"
# output_csv += f"_history_{history_window:02d}_dthresh_{d_thresh:02d}_random.csv"
output_csv = f"seq_pred_sherlock_wide_{bits_per_discrete_state:02d}_deep_{num_statelets_per_column:03d}"
output_csv += f"_history_{history_window:02d}_dthresh_{d_thresh:02d}_random.csv"
with open(output_csv, "w") as f:
# Header
header_str = f"step,current,next"
for i in range(0, num_labels):
char_letter = char_to_int_encoder.inverse_transform([i, ])[0]
header_str += f",{char_letter!r}"
header_str += "\n"
f.write(header_str)
# Training run
num_iter = 1
for this_iter in range(num_iter):
for i in range(len(integer_input_data)-1):
# Set discrete transformer value
discrete_encoder.set_value(integer_input_data[i])
# Compute the discrete transformer
discrete_encoder.feedforward()
# Compute the sequence learner
# t_sl = time()
sl.feedforward(learn=True)
# t_sl = time() - t_sl
# print(f"SL learn {t_sl:.6f}s")
# Training run
num_iter = 1
for this_iter in range(num_iter):
for i in range(len(integer_input_data)-1):
# Set discrete transformer value
discrete_encoder.set_value(integer_input_data[i])
# Compute the discrete transformer
discrete_encoder.feedforward()
# Compute the sequence learner
# t_sl = time()
sl.feedforward(learn=False)
# t_sl = time() - t_sl
# print(f"SL step {t_sl:.6f}s")
# Compute the classifier
# t_pc = time()
pc.set_label(integer_input_data[i+1])
pc.feedforward(learn=True)
# t_pc = time() - t_pc
# print(f"PC learn {t_pc:.6f}s")
print(f"Train {i:d}")
print(np.array(pc.output.bits, dtype=bool))
# Testing run
for i in range(len(integer_input_data)-1):
# Set discrete transformer value
discrete_encoder.set_value(integer_input_data[i])
# Compute the discrete transformer
discrete_encoder.feedforward()
# discrete_binary_array = np.array(discrete_encoder.output.bits, dtype=bool)
# Compute the sequence learner
# t_sl = time()
sl.feedforward(learn=False)
# t_sl = time() - t_sl
# print(f"SL step {t_sl:.6f}s")
# Compute the classifier
# t_pc = time()
pc.set_label(integer_input_data[i + 1])
pc.feedforward(learn=False)
# t_pc = time() - t_pc
# print(f"PC step {t_pc:.6f}s")
print(f"Test {i:d}")
print(np.array(pc.output.bits, dtype=bool))
# t_a = time()
# # sl_nonzero = sl.output.state.num_set
# sl_binary_array = np.array(sl.output.bits, dtype=bool)
# sl_nonzero = np.count_nonzero(sl_binary_array)
# t_a = time() - t_a
# print(f"SL active statelets {t_a:.6f}s")
# t_a = time()
# sl_state_count = sl.get_historical_count()
# t_a = time() - t_a
# print(f"SL historical statelets {t_a:.6f}s")
anom_score1 = sl.get_anomaly_score()
# t_a = time()
next_char_probs = pc.get_probabilities()
next_char = np.argmax(next_char_probs)
anom_score3 = 0 if next_char == integer_input_data[i+1] else 1
# t_a = time() - t_a
# print(f"PC anomaly score {t_a:.6f}s")
char_letter = char_to_int_encoder.inverse_transform([integer_input_data[i], ])[0]
char_letter_next = char_to_int_encoder.inverse_transform([integer_input_data[i+1], ])[0]
prob_str = f"{i:d},{char_letter!r},{char_letter_next!r}"
for k in range(0, len(next_char_probs)):
prob_str += f",{next_char_probs[k]:.4f}"
prob_str += "\n"
f.write(prob_str)
# f.write(f"{i}, {repr(char_letter)}, {integer_input_data[i]}, {sl_state_count}, ")
# f.write(f"{sl_nonzero}, {anom_score1:.6f}, {anom_score3:.6f}\n")
"""
# Loop through the values
for i in range(len(integer_input_data)-1):
# Set discrete transformer value
discrete_encoder.set_value(integer_input_data[i])
# Compute the discrete transformer
discrete_encoder.feedforward()
# discrete_binary_array = np.array(discrete_encoder.output.bits, dtype=bool)
# Compute the sequence learner
t_sl = time()
sl.feedforward(learn=False)
t_sl = time() - t_sl
print(f"SL step {t_sl:.2f}s")
# Compute the classifier
t_pc = time()
pc.set_label(integer_input_data[i + 1])
pc.feedforward(learn=False)
t_pc = time() - t_pc
print(f"PC step {t_pc:.2f}s")
sl_binary_array = np.array(sl.output.bits,
dtype=bool) # .reshape((num_encoder_bits, num_statelets_per_column))
# sl_context_array = np.array(sl.context.bits,
# dtype=bool) # .reshape((num_encoder_bits, num_statelets_per_column))
next_char_probs = pc.get_probabilities()
next_char = np.argmax(next_char_probs)
anom_score3 = 0 if next_char == integer_input_data[i+1] else 1
# print("Level 1")
# for k in range(num_encoder_bits):
# if np.count_nonzero(sl_binary_array[k, :]) > 0:
# print(k, sl_binary_array[k, :])
# Compute the pattern pooler
# hol_pp.feedforward(learn=True)
# hol_pp_binary_array = np.array(hol_pp.output.bits, dtype=bool)
# Compute the higher-order learner
# hol.feedforward(learn=True)
# hol_binary_array = np.array(hol.output.bits, dtype=bool)#.reshape(
# (num_encoder_bits, num_statelets_per_column))
# print(len(sl_binary_array), len(sl_context_array), len(hol_binary_array), len(sl_binary_array)+len(hol_binary_array))
# hol_binary_array = np.array(hol.output.bits, dtype=bool).reshape(
# (512, 10))
# sl_binary_array = np.array(sl.output.bits, dtype=bool) #.reshape((num_encoder_bits, num_statelets_per_column))
# print("Level 2")
# for k in range(num_encoder_bits * num_statelets_per_column):
# if np.count_nonzero(hol_binary_array[k, :]) > 0:
# print(k, hol_binary_array[k, :])
# print("second level output")
# print(hol_binary_array)
# Get anomaly score
sl_nonzero = np.count_nonzero(sl_binary_array)
# hol_pp_nonzero = np.count_nonzero(hol_pp_binary_array)
# hol_nonzero = np.count_nonzero(hol_binary_array)
sl_state_count = sl.get_historical_count()
# hol_state_count = hol.get_historical_count()
char_letter = char_to_int_encoder.inverse_transform([integer_input_data[i], ])[0]
anom_score1 = sl.get_anomaly_score()
# anom_score2 = hol.get_anomaly_score()
scores.append(anom_score1)
f.write(f"{i}, {repr(char_letter)}, {integer_input_data[i]}, {sl_state_count}, ")
f.write(f"{sl_nonzero}, {anom_score1:.2f}, {anom_score3:.2f}\n")
# f.write(f"{i}, {repr(char_letter)}, {integer_input_data[i]}, {sl_state_count}, {hol_state_count}, ")
# f.write(f"{sl_nonzero}, {hol_nonzero}, {anom_score1:.2f}, {anom_score2:.2f}\n")
# # Standard SequenceLearner configuration
# sl_shakespeare.csv
# 5458199 letters learned in 8372.40 sec. (1.53ms / letter)
# sl_sherlock.csv
# 581864 letters learned in 929.67 sec. (1.6ms / letter)
# # HOL, history 0, no PP, configuration
# hol_sherlock.csv
# 581864 letters learned in 9210.79 sec. (15.8ms / letter)
# # HOL, history 0, with PP, configuration
# hol_pp_sherlock.csv
# 5001 letters learned in 25.24 sec. (5ms / letter)
# # SL / concat HOL context, history 0, with PP, configuration
# sl_hol_pp_sherlock.csv
# 5001 letters learned in 23.46 sec. (4.7ms / letter)
# sl_hol_pp_sherlock_reduced_verbosity.csv
# 5001 letters learned in 15.50 sec. (3.1ms / letter)
# # SL / concat HOL context, match encoder bits, history 0, with PP, configuration
# sl_hol_pp_match_encoder_bits_sherlock.csv
# 5000 letters learned in 36.15 sec.
# 5000 letters learned in 48.05 sec.
# sl_hol_pp_match_encoder_bits_sherlock_scratch.csv
"""
t = time() - t0
num_letters = len(integer_input_data)
print(f"{num_letters} letters trained and tested in {t:.2f} sec.")