def balance_scale_eqiv_top_1(): path = examples + 'balance_scale_lin.h5' inl = [1, 1, 1, 1] inh = [5, 5, 5, 5] enc = Encoder() enc.encode_equivalence_from_file(path, path, inl, inh, 'optimize_ranking_top_1', 'optimize_ranking_top_1') interval_arithmetic(enc.get_constraints()) for i in range(1, 3): enc.optimize_layer(enc.a_layers, i) enc.optimize_layer(enc.b_layers, i) interval_arithmetic(enc.get_constraints()) model1 = create_gurobi_model(enc.get_vars(), enc.get_constraints(), 'balance_scale_opt_ranking_top_1_diff1') diff = model1.getVarByName('E_diff_0_1') model1.setObjective(diff, grb.GRB.MAXIMIZE) model1.setParam('TimeLimit', 10 * 60) model2 = create_gurobi_model(enc.get_vars(), enc.get_constraints(), 'balance_scale_opt_ranking_top_1_diff2') diff = model2.getVarByName('E_diff_0_2') model2.setObjective(diff, grb.GRB.MAXIMIZE) model2.setParam('TimeLimit', 10 * 60) # maximum for both diffs should be below 0 return model1, model2
def mnist_eqiv(mode): # optimize_ranking_top_k allowed as mode for k = 1..9 path = examples + 'mnist8x8_lin.h5' inl = [0 for i in range(64)] inh = [16 for i in range(64)] enc = Encoder() enc.encode_equivalence_from_file(path, path, inl, inh, mode, mode) interval_arithmetic(enc.get_constraints()) for i in range(1, 3): enc.optimize_layer(enc.a_layers, i) enc.optimize_layer(enc.b_layers, i) interval_arithmetic(enc.get_constraints()) if mode == 'one_hot_diff': k = 0 else: k = int(mode.split('_')[-1]) model = create_gurobi_model(enc.get_vars(), enc.get_constraints(), 'mnist_lin_' + mode) diff = model.getVarByName('E_diff_0_{num}'.format(num=k)) model.setObjective(diff, grb.GRB.MAXIMIZE) model.setParam('TimeLimit', 30 * 60) # maximum for diff should be greater 0 return model
def encode_optimize_radius(path1, path2, input_los, input_his, equiv_mode, center, radius_lo, radius_hi, metric, name): # accepts one_hot_partial_top_k as mode # also one_hot_diff ??? fc.use_asymmetric_bounds = True fc.use_context_groups = True fc.use_grb_native = False fc.use_eps_maximum = True fc.manhattan_use_absolute_value = True fc.epsilon = 1e-4 enc = Encoder() enc.encode_equivalence_from_file(path1, path2, input_los, input_his, equiv_mode, equiv_mode) enc.add_input_radius(center, radius_hi, metric, radius_mode='variable', radius_lo=radius_lo) interval_arithmetic(enc.get_constraints()) for i in range(1, 3): enc.optimize_layer(enc.a_layers, i) enc.optimize_layer(enc.b_layers, i) interval_arithmetic(enc.get_constraints()) model = create_gurobi_model(enc.get_vars(), enc.get_constraints(), name) r = model.getVarByName('r_0_0') model.setObjective(r, grb.GRB.MINIMIZE) model.setParam('TimeLimit', 30 * 60) # for obj val of r -> NNs are different # for (bound val - eps) of r -> NNs are equivalent return model
def encodeOneHotExample(): invars = encode_inputs([-1, 0, 1], [-1, 0, 1]) outs, vars, constraints = encode_one_hot(invars, 1, '') vars = [invars, vars, outs] constraints = [constraints] pretty_print(vars, constraints) print('\n### now with interval arithmetic ###') interval_arithmetic(constraints) pretty_print(vars, constraints)
def encodeMaxpoolExample(): invars = encode_inputs([0, 1, 2], [1, 2, 3]) outs, deltas, ineqs = encode_maxpool_layer(invars, 1, '') vars = [invars, deltas, outs] constraints = [ineqs] pretty_print(vars, constraints) print('\n### now with interval arithmetic ###') interval_arithmetic(constraints) pretty_print(vars, constraints)
def check_outputs(nn_file, ins, sort=True, printing=True): nn_vars, nn_constraints = encode_NN_from_file(nn_file, ins, ins, '') interval_arithmetic(nn_constraints) outs = nn_vars[-1] if sort: outs = sorted(outs, key=lambda x: x.lo) if printing: for v in outs: print(str(v) + ' : ' + str(v.lo)) return outs
def encodeRankingExample(): invars = encode_inputs([-1, 0, 1], [-1, 0, 1]) permute_matrix, vars, constraints = encode_ranking_layer(invars, 1, '') vars = [invars, vars, permute_matrix] constraints = [constraints] pretty_print(vars, constraints) print('\n### now with interval arithmetic ###') interval_arithmetic(constraints) pretty_print(vars, constraints) return vars, constraints
def exampleEncodeSimpleCancer(): # encode simple cancer classifier # result for given input should be 19.67078 kl = KerasLoader() kl.load('ExampleNNs/cancer_simple_lin.h5') inputs = [8, 10, 10, 8, 6, 9, 3, 10, 10] layers = kl.getHiddenLayers() vars, constraints = encodeNN(layers, inputs, inputs, '') interval_arithmetic(constraints) pretty_print(vars, constraints)
def prepare_layer_wise_equivalence(path1, path2, input_los, input_his, mode): old_eps = fc.epsilon fc.epsilon = 1e-4 fc.use_grb_native = False enc = Encoder() enc.encode_equivalence_from_file(path1, path2, input_los, input_his, mode) interval_arithmetic(enc.get_constraints()) for i in range(1, 3): enc.optimize_layer(enc.a_layers, i) enc.optimize_layer(enc.b_layers, i) interval_arithmetic(enc.get_constraints()) return enc
def encode_equiv_radius(path1, path2, input_los, input_his, equiv_mode, center, radius, metric, name): # accepts one_hot_partial_top_k, one_hot_diff as mode fc.use_asymmetric_bounds = True fc.use_context_groups = True fc.use_grb_native = False fc.use_eps_maximum = True fc.manhattan_use_absolute_value = True fc.epsilon = 1e-4 enc = Encoder() enc.encode_equivalence_from_file(path1, path2, input_los, input_his, equiv_mode, equiv_mode) enc.add_input_radius(center, radius, metric) interval_arithmetic(enc.get_constraints()) for i in range(1, 3): if '30_10' in path1: if i < 2: enc.optimize_layer(enc.a_layers, i) else: enc.optimize_layer(enc.a_layers, i) if '30_10' in path2: if i < 2: enc.optimize_layer(enc.b_layers, i) else: enc.optimize_layer(enc.b_layers, i) interval_arithmetic(enc.get_constraints()) if equiv_mode == 'one_hot_diff': k = 0 else: k = int(equiv_mode.split('_')[-1]) model = create_gurobi_model(enc.get_vars(), enc.get_constraints(), name) diff = model.getVarByName('E_diff_0_{num}'.format(num=k)) model.setObjective(diff, grb.GRB.MAXIMIZE) model.setParam('TimeLimit', 30 * 60) # maximum for diff should be greater 0 return model
def balance_scale_not_eqiv_top_2(): path1 = examples + 'balance_scale_lin.h5' path2 = examples + 'balance_scale_lin2.h5' inl = [1, 1, 1, 1] inh = [5, 5, 5, 5] enc = Encoder() enc.encode_equivalence_from_file(path1, path2, inl, inh, 'optimize_ranking_top_2', 'optimize_ranking_top_2') interval_arithmetic(enc.get_constraints()) model = create_gurobi_model(enc.get_vars(), enc.get_constraints(), 'balance_scale_opt_different_top_2') diff = model.getVarByName('E_diff_0_2') model.setObjective(diff, grb.GRB.MAXIMIZE) model.setParam('TimeLimit', 10 * 60) # maximal diff should be greater 0 return model
def exampleEncodeCancer(with_interval_arithmetic=True): # smallest inputs for whole training data input_los = [ -2.019404, -2.272988, -1.977589, -1.426379, -3.176344, -1.664312, -1.125696, -1.262871, -2.738225, -1.865718, -1.024522, -1.569514, -1.016081, -0.6933525, -1.862462, -1.304206, -1.012913, -1.977069, -1.544220, -1.080050, -1.704360, -2.218398, -1.673608, -1.188201, -2.711807, -1.468356, -1.341360, -1.754014, -2.128278, -1.598903 ] # highest inputs for all training data input_his = [ 3.963628, 3.528104, 3.980919, 5.163006, 3.503046, 4.125777, 4.366097, 3.955644, 4.496561, 5.105021, 8.697088, 6.788612, 9.410281, 10.52718, 5.747718, 6.308377, 11.73186, 6.984494, 4.999672, 10.02360, 4.049783, 3.938555, 4.261315, 5.758096, 3.988374, 5.270909, 4.936910, 2.695096, 5.934052, 6.968987 ] input_malign_zero_lo = input_los # highest score in feature 3 for benign data is 0.945520 -> set input vector higher than that # -> if NN works correctly, then input should be classified as malign (label = 1) input_malign_zero_lo[0] = 1.1 input_malign_zero_lo[3] = 1.25 input_malign_zero_lo[4] = 2.96 input_malign_zero_lo[7] = 1.45 input_malign_zero_lo[20] = 0.9 vars, constraints = encode_from_file('ExampleNNs/cancer_lin.h5', input_malign_zero_lo, input_his) if with_interval_arithmetic: interval_arithmetic(constraints) pretty_print(vars, constraints) print('\n### smtlib ###\n') # for proof that nn is correct manually insert constraint: # (assert (<= x_3_0 0)) # if this can be satisfied, then a counterexample to correctness has been found print(print_to_smtlib(vars, constraints)) return vars, constraints
def cancer_eqiv(): path = examples + 'cancer_lin.h5' inl = [-3.18 for i in range(30)] inh = [11.74 for i in range(30)] enc = Encoder() enc.encode_equivalence_from_file(path, path, inl, inh, 'outputs', 'optimize_diff') interval_arithmetic(enc.get_constraints()) for i in range(1, 4): enc.optimize_layer(enc.a_layers, i) enc.optimize_layer(enc.b_layers, i) interval_arithmetic(enc.get_constraints()) model = create_gurobi_model(enc.get_vars(), enc.get_constraints(), 'cancer_lin_opt_diff') diff = model.getVarByName('E_diff_0_0') model.setObjective(diff, grb.GRB.MAXIMIZE) model.setParam('TimeLimit', 10 * 60) # maximum for diff should be exactly 0 return model
def encode_equiv(path1, path2, input_los, input_his, mode, name): # accepts one_hot_partial_top_k, one_hot_diff as mode enc = Encoder() enc.encode_equivalence_from_file(path1, path2, input_los, input_his, mode, mode) interval_arithmetic(enc.get_constraints()) for i in range(1, 3): enc.optimize_layer(enc.a_layers, i) enc.optimize_layer(enc.b_layers, i) interval_arithmetic(enc.get_constraints()) if mode == 'one_hot_diff': k = 0 else: k = int(mode.split('_')[-1]) model = create_gurobi_model(enc.get_vars(), enc.get_constraints(), name) diff = model.getVarByName('E_diff_0_{num}'.format(num=k)) model.setObjective(diff, grb.GRB.MAXIMIZE) model.setParam('TimeLimit', 30 * 60) # maximum for diff should be greater 0 return model
def optimize_constraints(self): interval_arithmetic(self.get_constraints()) self.optimize_net(self.a_layers) self.optimize_net(self.b_layers)
def optimize_net(self, net): for i in range(len(net)): if not net[i].activation == 'one_hot': self.optimize_layer(net, i) interval_arithmetic(self.get_constraints())