forked from wwjbrugger/DCDL
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SLS_Algorithm.py
executable file
·304 lines (267 loc) · 17.9 KB
/
SLS_Algorithm.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
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
import model.boolean_Formel as bofo
import numpy as np
import parallel_sls.python_wrapper.sls_wrapper as sls_wrapper
import parallel_sls.python_wrapper.data_wrapper as data_wrapper
"""
Input in SLS are values in True/False Form
"""
# call SLS Implementation in C
def rule_extraction_with_sls_test(train, train_label, val, val_label, test, test_label,
number_of_disjunction_term, maximum_steps_in_SLS, kernel,
p_g1, p_g2, p_s, batch, cold_restart, decay, min_prob, zero_init):
# use SLS with a train, validation and test set
# check input dimensions
if np.ndim(train) != 2 or np.ndim(val) != 2 or np.ndim(test) != 2:
raise ValueError('Input data are not flatten. Shape of input is {}, {}, {}'.
format(train.shape, val.shape, test.shape))
if np.ndim(train_label) != 1 or np.ndim(val_label) != 1 or np.ndim(test_label) != 1:
raise ValueError('Label data are not flatten. Shape of input is {}, {}, {}'.
format(train_label.shape, val_label.shape, test_label.shape))
# number of input variables is rounded up to a multiple of eight
# C++ implementation stores formula in uint 8 variables
num_of_features = (8 - train.shape[1]) % 8 + train.shape[1]
# how many uint8 variables are needed
num_of_8_bit_units_to_store_feature = int(num_of_features / 8)
training_set_data_packed_continguous = data_wrapper.binary_to_packed_uint8_continguous(train)
training_set_label_bool_continguous = np.ascontiguousarray(train_label, dtype=np.bool)
validation_set_data_packed_continguous = data_wrapper.binary_to_packed_uint8_continguous(val)
validation_set_label_bool_continguous = np.ascontiguousarray(val_label, dtype=np.bool)
test_set_data_packed_continguous = data_wrapper.binary_to_packed_uint8_continguous(test)
test_set_label_bool_continguous = np.ascontiguousarray(test_label, dtype=np.bool)
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Free space to store formulas found
pos_neg = np.ascontiguousarray(
np.empty((number_of_disjunction_term * num_of_8_bit_units_to_store_feature,), dtype=np.uint8))
on_off = np.ascontiguousarray(
np.empty((number_of_disjunction_term * num_of_8_bit_units_to_store_feature,), dtype=np.uint8))
pos_neg_to_store = np.ascontiguousarray(
np.empty((number_of_disjunction_term * num_of_8_bit_units_to_store_feature,), dtype=np.uint8))
on_off_to_store = np.ascontiguousarray(
np.empty((number_of_disjunction_term * num_of_8_bit_units_to_store_feature,), dtype=np.uint8))
if not isinstance(kernel, bool):
# Initialisation with kernel values from neural net not used in experiment
if kernel.ndim == 1:
output_relevant, output_negated = bofo.Boolean_formula.split_fomula(kernel)
output_relevant_numbers = bofo.Boolean_formula.transform_arrays_code_in_number_code(output_relevant)
output_negated_numbers = bofo.Boolean_formula.transform_arrays_code_in_number_code(output_negated)
size_kernel_8bit = output_relevant_numbers.size
for i in range(0, number_of_disjunction_term * num_of_8_bit_units_to_store_feature,
num_of_8_bit_units_to_store_feature):
pos_neg[i:i + size_kernel_8bit] = output_negated_numbers
on_off[i:i + size_kernel_8bit] = output_relevant_numbers
else:
raise ValueError("kernel should be one dimensional no {}".format(kernel.ndim))
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Start SLS
sls_obj = sls_wrapper.sls_test(clauses_n=number_of_disjunction_term,
maxSteps=maximum_steps_in_SLS,
p_g1=p_g1, # Prob of rand term in H
p_g2=p_g2, # Prob of rand literal in H
p_s=p_s, # Prob of rand term in H
data=training_set_data_packed_continguous,
label=training_set_label_bool_continguous,
data_val=validation_set_data_packed_continguous,
label_val=validation_set_label_bool_continguous,
data_test=test_set_data_packed_continguous, # Data input
label_test=test_set_label_bool_continguous, # Label input
pos_neg=pos_neg, # Positive or negative for formula
on_off=on_off, # Mask for formula
pos_neg_to_store=pos_neg_to_store, # Positive or negative for formula
on_off_to_store=on_off_to_store, # Mask for formula
vector_n=train.shape[0],
# of data vectors !!!!NEEDS TO BE BIGGER THEN BATCH_SIZE!!!!
# vector_n_val=second_split - first_split
vector_n_val=val.shape[0],
# of data vectors !!!!NEEDS TO BE BIGGER THEN BATCH_SIZE!!!!
vector_n_test=test[0],
# of data vectors !!!!NEEDS TO BE BIGGER THEN BATCH_SIZE!!!!
features_n=num_of_features, # of Features
batch=batch,
cold_restart=cold_restart,
decay=decay,
min_prob=min_prob,
zero_init=zero_init
)
found_formula = bofo.Boolean_formula(on_off_to_store, pos_neg_to_store, number_of_disjunction_term,
total_error_on_validation_set=sls_obj.total_error)
# calculate accuracy on train set
# accuracy = number_of_correct_predictions / total_number_of_prediction
# The first split is the train set
found_formula.train_acc = (val.shape[0] - found_formula.total_error_on_validation_set) / val.shape[0]
return found_formula
def rule_extraction_with_sls_val(train, train_label, val, val_label,
number_of_disjunction_term, maximum_steps_in_SLS,
kernel,
p_g1, p_g2, p_s, batch, cold_restart, decay, min_prob,
zero_init
):
# use SLS with a train and validation set
# check input dimensions
if np.ndim(train) != 2 or np.ndim(val) != 2:
raise ValueError('Input data are not flatten. Shape of input is {}, {}'.
format(train.shape, val.shape))
if np.ndim(train_label) != 1 or np.ndim(val_label) != 1 :
raise ValueError('Label data are not flatten. Shape of input is {}, {}'.
format(train_label.shape, val_label.shape))
# run sls with train and validation data
# number of input variables is rounded up to a multiple of eight
# C++ implementation stores formula in uint 8 variables
num_of_features = (8 - train.shape[1]) % 8 + train.shape[1]
# how many uint8 variables are needed
num_of_8_bit_units_to_store_feature = int(num_of_features / 8)
# pack data in C## compatible arrays
training_set_data_packed_continguous = data_wrapper.binary_to_packed_uint8_continguous(train)
training_set_label_bool_continguous = np.ascontiguousarray(train_label, dtype=np.bool)
val_set_data_packed_continguous = data_wrapper.binary_to_packed_uint8_continguous(val)
val_set_label_bool_continguous = np.ascontiguousarray(val_label, dtype=np.bool)
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Free space to store formulas found
pos_neg = np.ascontiguousarray(
np.empty((number_of_disjunction_term * num_of_8_bit_units_to_store_feature,), dtype=np.uint8))
on_off = np.ascontiguousarray(
np.empty((number_of_disjunction_term * num_of_8_bit_units_to_store_feature,), dtype=np.uint8))
pos_neg_to_store = np.ascontiguousarray(
np.empty((number_of_disjunction_term * num_of_8_bit_units_to_store_feature,), dtype=np.uint8))
on_off_to_store = np.ascontiguousarray(
np.empty((number_of_disjunction_term * num_of_8_bit_units_to_store_feature,), dtype=np.uint8))
if not isinstance(kernel, bool):
# Initialisation with kernel values from neural net not used in experiment
if kernel.ndim == 1:
output_relevant, output_negated = bofo.Boolean_formula.split_fomula(kernel)
output_relevant_numbers = bofo.Boolean_formula.transform_arrays_code_in_number_code(output_relevant)
output_negated_numbers = bofo.Boolean_formula.transform_arrays_code_in_number_code(output_negated)
size_kernel_8bit = output_relevant_numbers.size
for i in range(0, number_of_disjunction_term * num_of_8_bit_units_to_store_feature,
num_of_8_bit_units_to_store_feature):
pos_neg[i:i + size_kernel_8bit] = output_negated_numbers
on_off[i:i + size_kernel_8bit] = output_relevant_numbers
else:
raise ValueError("kernel should be one dimensional no {}".format(kernel.ndim))
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Start SLS
sls_obj = sls_wrapper.sls_val(clauses_n=number_of_disjunction_term,
maxSteps=maximum_steps_in_SLS,
p_g1=p_g1, # Prob of rand term in H
p_g2=p_g2, # Prob of rand literal in H
p_s=p_s, # Prob of rand term in H
data=training_set_data_packed_continguous,
label=training_set_label_bool_continguous,
data_val=val_set_data_packed_continguous,
label_val=val_set_label_bool_continguous,
pos_neg=pos_neg, # Positive or negative for formula
on_off=on_off, # Mask for formula
pos_neg_to_store=pos_neg_to_store, # Positive or negative for formula
on_off_to_store=on_off_to_store, # Mask for formula
vector_n=int(train.shape[0]),
# of data vectors !!!!NEEDS TO BE BIGGER THEN BATCH_SIZE!!!!
vector_n_val=int(val.shape[0]),
# of data vectors !!!!NEEDS TO BE BIGGER THEN BATCH_SIZE!!!!
features_n=num_of_features, # of Features
batch=batch,
cold_restart=cold_restart,
decay=decay,
min_prob=min_prob,
zero_init=zero_init)
found_formula = bofo.Boolean_formula(on_off_to_store, pos_neg_to_store, number_of_disjunction_term,
total_error_on_validation_set=sls_obj.total_error)
# calculate accuracy on train set
# accuracy = number_of_correct_predictions / total_number_of_prediction
# The first split is the train set
found_formula.train_acc = (val.shape[0] - found_formula.total_error_on_validation_set) / val.shape[0]
return found_formula
# ---------------------------------------------------------------------------------------------
def rule_extraction_with_sls(train, train_label,
number_of_disjunction_term, maximum_steps_in_SLS,
kernel,
p_g1, p_g2, p_s, batch, cold_restart, decay, min_prob,
zero_init):
# use SLS with a set
# check input dimensions
if np.ndim(train) != 2 :
raise ValueError('Input data are not flatten. Shape of input is {}'.format(train.shape))
if np.ndim(train_label) != 1 :
raise ValueError('Label data are not flatten. Shape of input is {}'.format(train_label.shape))
# run SLS with maximal number of training samples
# number of input variables is rounded up to a multiple of eight
# C++ implementation stores formula in uint 8 variables
num_of_features = (8 - train.shape[1]) % 8 + train.shape[1]
# how many uint8 variables are needed
num_of_8_bit_units_to_store_feature = int(num_of_features / 8)
# pack data in C## compatible arrays
training_set_data_packed_continguous = data_wrapper.binary_to_packed_uint8_continguous(train)
training_set_label_bool_continguous = np.ascontiguousarray(train_label, dtype=np.bool)
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Free space to store formulas found
pos_neg = np.ascontiguousarray(
np.empty((number_of_disjunction_term * num_of_8_bit_units_to_store_feature,), dtype=np.uint8))
on_off = np.ascontiguousarray(
np.empty((number_of_disjunction_term * num_of_8_bit_units_to_store_feature,), dtype=np.uint8))
pos_neg_to_store = np.ascontiguousarray(
np.empty((number_of_disjunction_term * num_of_8_bit_units_to_store_feature,), dtype=np.uint8))
on_off_to_store = np.ascontiguousarray(
np.empty((number_of_disjunction_term * num_of_8_bit_units_to_store_feature,), dtype=np.uint8))
if not isinstance(kernel, bool):
# Initialisation with kernel values from neural net not used in experiment
if kernel.ndim == 1:
output_relevant, output_negated = bofo.Boolean_formula.split_fomula(kernel)
output_relevant_numbers = bofo.Boolean_formula.transform_arrays_code_in_number_code(output_relevant)
output_negated_numbers = bofo.Boolean_formula.transform_arrays_code_in_number_code(output_negated)
size_kernel_8bit = output_relevant_numbers.size
for i in range(0, number_of_disjunction_term * num_of_8_bit_units_to_store_feature,
num_of_8_bit_units_to_store_feature):
pos_neg[i:i + size_kernel_8bit] = output_negated_numbers
on_off[i:i + size_kernel_8bit] = output_relevant_numbers
else:
raise ValueError("kernel should be one dimensional no {}".format(kernel.ndim))
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Start SLS
sls_obj = sls_wrapper.sls(clauses_n=number_of_disjunction_term,
maxSteps=maximum_steps_in_SLS,
p_g1=p_g1, # Prob of rand term in H
p_g2=p_g2, # Prob of rand literal in H
p_s=p_s, # Prob of rand term in H
data=training_set_data_packed_continguous,
label=training_set_label_bool_continguous,
pos_neg=pos_neg, # Positive or negative for formula
on_off=on_off, # Mask for formula
pos_neg_to_store=pos_neg_to_store, # Positive or negative for formula
on_off_to_store=on_off_to_store, # Mask for formula
vector_n=int(train.shape[0]),
# of data vectors !!!!NEEDS TO BE BIGGER THEN BATCH_SIZE!!!!
features_n=num_of_features, # of Features
batch=batch,
cold_restart=cold_restart,
decay=decay,
min_prob=min_prob,
zero_init=zero_init
)
found_formula = bofo.Boolean_formula(on_off_to_store, pos_neg_to_store, number_of_disjunction_term,
total_error_on_validation_set=sls_obj.total_error)
# calculate accuracy on train set
# accuracy = number_of_correct_predictions / total_number_of_prediction
# The first split is the train set
found_formula.train_acc = (train.shape[0] - found_formula.total_error_on_validation_set) / train.shape[0]
return found_formula
# return bofo.Boolean_formula(on_off_to_store, pos_neg_to_store, number_of_disjunction_term, total_error = sls_obj.total_error)
# ------------------------------------------------------------------------------------------------
# calc prediction of learned SLS in C
def calc_prediction_in_C(data, label_shape, found_formula):
# use C++ code to calculate prediction for given data with found formula
# test input shape
if np.ndim(data) != 2:
raise ValueError('Input data are not flatten. Shape of input is {}'.format(data.shape))
num_anzahl_input_data = int(data.shape[0])
num_of_features = found_formula.variable_pro_term
number_of_disjunction_term = found_formula.number_of_disjunction_term_in_SLS
data_packed_continguous = data_wrapper.binary_to_packed_uint8_continguous(data)
space_label_bool_continguous = np.ascontiguousarray(np.empty(label_shape, np.bool), dtype=np.bool)
pos_neg_to_store = np.ascontiguousarray(found_formula.pixel_negated_in_number_code.copy(), dtype=np.uint8)
on_off_to_store = np.ascontiguousarray(found_formula.pixel_relevant_in_number_code, dtype=np.uint8)
prediction_obj = sls_wrapper.calc_prediction(data_packed_continguous,
space_label_bool_continguous,
pos_neg_to_store,
on_off_to_store,
num_anzahl_input_data,
number_of_disjunction_term,
num_of_features)
return space_label_bool_continguous