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_equiv_radius(path1, path2, input_los, input_his, equiv_mode, center, radius, metric, name): # accepts one_hot_partial_top_k 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) enc.optimize_constraints() 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 encode_r_opt(path1, path2, inl, inh, center, radius_lo, radius_hi, mode, time_limit=30*60): 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 fc.not_equiv_tolerance = 1e-2 enc = Encoder() enc.encode_equivalence_from_file(path1, path2, inl, inh, mode, mode) enc.add_input_radius(center, radius_hi, radius_mode='variable', radius_lo=radius_lo) enc.optimize_constraints() model = create_gurobi_model(enc.get_vars(), enc.get_constraints()) r = model.getVarByName('r_0_0') model.setObjective(r, grb.GRB.MINIMIZE) model.setParam('TimeLimit', time_limit) return enc, model
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 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 balance_scale_eqiv_top_2(): 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_2', 'optimize_ranking_top_2') 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(), 'balance_scale_opt_ranking_top_2') diff = model.getVarByName('E_diff_0_2') model.setObjective(diff, grb.GRB.MAXIMIZE) model.setParam('TimeLimit', 10 * 60) # maximum for diff should be below 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 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