forked from the-aerospace-corporation/brainblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern_classifier.py
More file actions
71 lines (61 loc) · 1.97 KB
/
pattern_classifier.py
File metadata and controls
71 lines (61 loc) · 1.97 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
# ==============================================================================
# pattern_classifier.py
# ==============================================================================
from brainblocks.blocks import ScalarTransformer, PatternClassifier
from sklearn import preprocessing
x_trains = [
0.0, 1.0, 0.0, 1.0, 0.0,
1.0, 0.0, 1.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0, 0.0,
1.0, 0.0, 1.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0, 0.0,
1.0, 0.0, 1.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0, 0.0]
y_trains = [
"a", "b", "a", "b", "a",
"b", "a", "b", "a", "b",
"a", "b", "a", "b", "a",
"b", "a", "b", "a", "b",
"a", "b", "a", "b", "a",
"b", "a", "b", "a", "b",
"a", "b", "a", "b", "a"]
x_tests = [0.0, 1.0]
# Convert to integer labels
le = preprocessing.LabelEncoder()
le.fit(y_trains)
y_trains_ints = le.transform(y_trains)
# Setup blocks
st = ScalarTransformer(
min_val=-1.0, # minimum input value
max_val=1.0, # maximum input value
num_s=1024, # number of statelets
num_as=128) # number of active statelets
pp = PatternClassifier(
num_l=2, # number of labels
num_s=512, # 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, # percent pooled
pct_conn=0.5, # percent initially connected
pct_learn=0.3) # percent learn
# Connect blocks
pp.input.add_child(st.output, 0)
# Fit
for i in range(len(x_trains)):
st.set_value(x_trains[i])
pp.set_label(y_trains_ints[i])
st.feedforward()
pp.feedforward(learn=True)
# Predict
probs = []
for i in range(len(x_tests)):
st.set_value(x_tests[i])
st.feedforward()
pp.feedforward(learn=True)
probs.append(pp.get_probabilities())
# Print output
print("x, p_a, p_b")
for i in range(len(x_tests)):
print("%0.1f, %0.1f, %0.1f" % (x_tests[i], probs[i][0], probs[i][1]))