forked from the-aerospace-corporation/brainblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_test_scalar_sequence_anomalies.py
More file actions
83 lines (63 loc) · 2.31 KB
/
train_test_scalar_sequence_anomalies.py
File metadata and controls
83 lines (63 loc) · 2.31 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
# ==============================================================================
# online_learning_scalar_sequence_anomalies.py
# ==============================================================================
from brainblocks.blocks import ScalarTransformer, SequenceLearner
train_values = [
0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0,
0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0,
0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0]
test_values = [
0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.2, 1.0, 1.0] # <-- anomaly is 0.2
train_scores = [0.0 for _ in range(len(train_values))]
test_scores = [0.0 for _ in range(len(test_values))]
# Setup blocks
st = ScalarTransformer(
min_val=0.0, # minimum input value
max_val=1.0, # maximum input value
num_s=64, # number of statelets
num_as=8) # number of active 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(st.output, 0)
# Loop through the values twice
for iteration_i in range(2):
for i in range(len(train_values)):
# Set scalar transformer value
st.set_value(train_values[i])
# Compute the scalar transformer
st.feedforward()
# Compute the sequence learner w/ learning
sl.feedforward(learn=True)
# Get anomaly score
train_scores[i] = sl.get_anomaly_score()
# Reset SequenceLearner
st.clear()
sl.clear()
# Loop through the values
for i in range(len(test_values)):
# Set scalar transformer value
st.set_value(test_values[i])
# Compute the scalar transformer
st.feedforward()
# Compute the sequence learner w/o learning
sl.feedforward(learn=False)
# Get anomaly score
test_scores[i] = sl.get_anomaly_score()
# Print output
print("TRAIN")
print("val, scr")
for i in range(len(train_values)):
print("%0.1f, %0.1f" % (train_values[i], train_scores[i]))
# Print output
print()
print("TEST")
print("val, scr")
for i in range(len(test_values)):
print("%0.1f, %0.1f" % (test_values[i], test_scores[i]))