forked from the-aerospace-corporation/brainblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathonline_learning_discrete_sequence_anomalies.py
More file actions
54 lines (42 loc) · 1.57 KB
/
online_learning_discrete_sequence_anomalies.py
File metadata and controls
54 lines (42 loc) · 1.57 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
# ==============================================================================
# online_learning_discrete_sequence_anomalies.py
# ==============================================================================
from brainblocks.blocks import DiscreteTransformer, SequenceLearner
from sklearn import preprocessing
values = [
'a', 'a', 'a', 'a', 'a', 'b', 'c', 'd', 'e', 'f',
'a', 'a', 'a', 'a', 'a', 'b', 'c', 'd', 'e', 'f',
'a', 'a', 'a', 'a', 'a', 'b', 'c', 'g', 'e', 'f']
scores = [0.0 for _ in range(len(values))]
# Convert values to integers
le = preprocessing.LabelEncoder()
le.fit(values)
integers = le.transform(values)
# Setup blocks
lt = DiscreteTransformer(
num_v=26, # number of discrete values
num_s=208) # number of statelets
sl = SequenceLearner(
num_spc=10, # 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
# Connect blocks
sl.input.add_child(lt.output, 0)
# Loop through the values
for i in range(len(integers)):
# Set scalar transformer value
lt.set_value(integers[i])
# Compute the scalar transformer
lt.feedforward()
# Compute the sequence learner
sl.feedforward(learn=True)
# Get anomaly score
scores[i] = sl.get_anomaly_score()
# Print output
print("val, scr")
for i in range(len(values)):
print("%3s, %0.1f" % (values[i], scores[i]))