-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReasoningByEliminationAnalysis.py
175 lines (133 loc) · 6.72 KB
/
ReasoningByEliminationAnalysis.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
from tmu.models.classification.vanilla_classifier import TMClassifier
import numpy as np
from time import time
number_of_training_examples = 500000
number_of_test_examples = 10000
noise = [0.05, 0.1, 0.2]
number_of_features = 10000
number_of_characterizing_features = 1000 # Each class gets this many unique features in total
number_of_characterizing_features_per_example = 2 # Each example consists of this number of unique features
number_of_common_features_per_example = 10
number_of_clauses = 100
T = number_of_clauses*100
s = 1.0
a = 1.1
b = 2.7
#characterizing_features = np.random.choice(number_of_features, size=(2, number_of_characterizing_features), replace=False).astype(np.uint32)
characterizing_features = np.arange(number_of_characterizing_features*2).reshape((2, number_of_characterizing_features)).astype(np.uint32)
common_features = np.setdiff1d(np.arange(number_of_features), characterizing_features.reshape(-1))
p_common_feature = np.empty(common_features.shape[0])
for k in range(common_features.shape[0]):
p_common_feature[k] = (k + b)**(-a)
p_common_feature = p_common_feature / p_common_feature.sum()
X_train = np.zeros((number_of_training_examples, number_of_features), dtype=np.uint32)
Y_train = np.zeros(number_of_training_examples, dtype=np.uint32)
for i in range(number_of_training_examples):
Y_train[i] = np.random.choice(2)
indexes = np.random.choice(characterizing_features[Y_train[i]], number_of_characterizing_features_per_example, replace=False)
for j in indexes:
X_train[i, j] = 1
indexes = np.random.choice(characterizing_features[1 - Y_train[i]], number_of_characterizing_features_per_example, replace=False)
for j in indexes:
X_train[i, j] = np.random.choice(2, p=[1.0 - noise[j%len(noise)], noise[j%len(noise)]])
indexes = np.random.choice(common_features, number_of_common_features_per_example, replace=False, p=p_common_feature)
for j in indexes:
X_train[i, j] = 1
X_test = np.zeros((number_of_test_examples, number_of_features), dtype=np.uint32)
Y_test = np.zeros(number_of_test_examples, dtype=np.uint32)
for i in range(number_of_test_examples):
Y_test[i] = np.random.choice(2)
indexes = np.random.choice(characterizing_features[Y_test[i]], number_of_characterizing_features_per_example, replace=False)
for j in indexes:
X_test[i, j] = 1
indexes = np.random.choice(common_features, number_of_common_features_per_example, replace=False, p=p_common_feature)
for j in indexes:
X_test[i, j] = 1
np.savetxt("ReasoningByEliminationTrainingData.txt", np.concatenate((X_train, Y_train.reshape(-1,1)), axis=1), delimiter=" ")
np.savetxt("ReasoningByEliminationTestingData.txt", np.concatenate((X_test, Y_test.reshape(-1,1)), axis=1), delimiter=" ")
tm = TMClassifier(number_of_clauses, T, s, platform='CPU', weighted_clauses=True, max_included_literals=64)
start = time()
tm.fit(X_train, Y_train)
stop = time()
print(stop-start)
np.set_printoptions(threshold=np.inf, linewidth=200, precision=2, suppress=True)
print("\nClass 0 Positive Clauses:\n")
precision = tm.clause_precision(0, 0, X_test, Y_test)
recall = tm.clause_recall(0, 0, X_test, Y_test)
for j in range(number_of_clauses//2):
print("Clause #%d W:%d P:%.2f R:%.2f " % (j, tm.get_weight(0, 0, j), precision[j], recall[j]), end=' ')
l = []
for k in range(number_of_features*2):
if tm.get_ta_action(j, k, the_class = 0, polarity = 0):
if k < number_of_features:
l.append(" x%d(%d)" % (k, tm.get_ta_state(j, k, the_class = 1, polarity = 0)))
else:
l.append("¬x%d(%d)" % (k-number_of_features, tm.get_ta_state(j, k, the_class = 0, polarity = 0)))
print(" ∧ ".join(l))
print("\nClass 0 Negative Clauses:\n")
precision = tm.clause_precision(0, 1, X_test, Y_test)
recall = tm.clause_recall(0, 1, X_test, Y_test)
for j in range(number_of_clauses//2):
print("Clause #%d W:%d P:%.2f R:%.2f " % (j, tm.get_weight(0, 1, j), precision[j], recall[j]), end=' ')
l = []
for k in range(number_of_features*2):
if tm.get_ta_action(j, k, the_class = 0, polarity = 1):
if k < number_of_features:
l.append(" x%d(%d)" % (k, tm.get_ta_state(j, k, the_class = 0, polarity = 1)))
else:
l.append("¬x%d(%d)" % (k-number_of_features, tm.get_ta_state(j, k, the_class = 0, polarity = 1)))
print(" ∧ ".join(l))
print("\nClass 1 Positive Clauses:\n")
precision = tm.clause_precision(1, 0, X_test, Y_test)
recall = tm.clause_recall(1, 0, X_test, Y_test)
for j in range(number_of_clauses//2):
print("Clause #%d W:%d P:%.2f R:%.2f " % (j, tm.get_weight(1, 0, j), precision[j], recall[j]), end=' ')
l = []
for k in range(number_of_features*2):
if tm.get_ta_action(j, k, the_class = 1, polarity = 0):
if k < number_of_features:
l.append(" x%d(%d)" % (k, tm.get_ta_state(j, k, the_class = 1, polarity = 0)))
else:
l.append("¬x%d(%d)" % (k-number_of_features, tm.get_ta_state(j, k, the_class = 1, polarity = 0)))
print(" ∧ ".join(l))
print("\nClass 1 Negative Clauses:\n")
precision = tm.clause_precision(1, 1, X_test, Y_test)
recall = tm.clause_recall(1, 1, X_test, Y_test)
for j in range(number_of_clauses//2):
print("Clause #%d W:%d P:%.2f R:%.2f " % (j, tm.get_weight(1, 1, j), precision[j], recall[j]), end=' ')
l = []
for k in range(number_of_features*2):
if tm.get_ta_action(j, k, the_class = 1, polarity = 1):
if k < number_of_features:
l.append(" x%d(%d)" % (k, tm.get_ta_state(j, k, the_class = 1, polarity = 1)))
else:
l.append("¬x%d(%d)" % (k-number_of_features, tm.get_ta_state(j, k, the_class = 1, polarity = 1)))
print(" ∧ ".join(l))
print("\nLiteral Clause Frequency:", end=' ')
literal_frequency = tm.literal_clause_frequency()
sorted_literals = np.argsort(-1*literal_frequency)
for k in sorted_literals:
if literal_frequency[k] == 0:
break
if k < number_of_features:
print("%d(%.2f,%d,%d)" % (k, 1.0*literal_frequency[k]/number_of_clauses, (k - number_of_features) % len(noise), k in common_features), end=' ')
else:
print("¬%d(%.2f,%d,%d)" % (k - number_of_features, 1.0*literal_frequency[k]/number_of_clauses, (k - number_of_features) % len(noise), k in common_features), end=' ')
print()
for i in range(2):
print("\nLiteral Importance Class #%d:" % (i), end=' ')
literal_importance = tm.literal_importance(i, negated_features=False, negative_polarity=False).astype(np.int32)
sorted_literals = np.argsort(-1*literal_importance)
for k in sorted_literals:
if literal_importance[k] == 0:
break
print(k, end=' ')
literal_importance = tm.literal_importance(i, negated_features=True, negative_polarity=False).astype(np.int32)
sorted_literals = np.argsort(-1*literal_importance)
for k in sorted_literals:
if literal_importance[k] == 0:
break
print("¬" + str(k - number_of_features), end=' ')
print()
print(characterizing_features, common_features.shape)
print("Accuracy:", 100*(tm.predict(X_test) == Y_test).mean())