def run( ATTACK_RESULTS_DIR=config["dirs"]["attack_results"], OBJECTIVES_PATH=config["paths"]["objectives"], THRESHOLD=config["threshold"], MODEL_PATH=config["paths"]["model"], ): model = load(MODEL_PATH) model.set_params(verbose=0, n_jobs=1) encoder = VenusEncoder() success_rates = np.array( pickle_from_dir( ATTACK_RESULTS_DIR, handler=lambda i, x: process(x, encoder, THRESHOLD, model), )) success_rates_df = pd.DataFrame(success_rates, columns=out_columns) success_rates_df.to_csv(OBJECTIVES_PATH, index=False)
def run( MODEL_PATH=config["paths"]["model"], ATTACK_RESULTS_PATH=config["paths"]["attack_results"], OBJECTIVES_PATH=config["paths"]["objectives"], THRESHOLD=config["threshold"], ): Path(OBJECTIVES_PATH).parent.mkdir(parents=True, exist_ok=True) efficient_results = Pickler.load_from_file(ATTACK_RESULTS_PATH) encoder = VenusEncoder() model = load(MODEL_PATH) model.set_params(verbose=0, n_jobs=1) success_rates = calculate_success_rates(efficient_results, encoder, THRESHOLD, model) columns = ["o{}".format(i + 1) for i in range(success_rates.shape[0])] success_rate_df = pd.DataFrame( success_rates.reshape([1, -1]), columns=columns, ) success_rate_df.to_csv(OBJECTIVES_PATH, index=False)
def run( MODEL_PATH=config["paths"]["model"], RESISTANT_MODEL_PATH=config["paths"]["resistant_model"], ATTACK_RESULTS_PATH=config["paths"]["attack_results"], TRAIN_TEST_DATA_DIR=config["dirs"]["train_test_data"], THRESHOLD=config["threshold"], ): model = load(MODEL_PATH) model.set_params(verbose=0, n_jobs=1) encoder = VenusEncoder() attack_results = Pickler.load_from_file(ATTACK_RESULTS_PATH) X_adv = adversarial_training(attack_results, encoder, THRESHOLD, model) y_adv = np.zeros(X_adv.shape[0]) + 1 X_train = np.load("{}/X_train.npy".format(TRAIN_TEST_DATA_DIR)) y_train = np.load("{}/y_train.npy".format(TRAIN_TEST_DATA_DIR)) resistant_model = clone(model) resistant_model.set_params(verbose=2, n_jobs=-1) resistant_model.fit(np.concatenate([X_train, X_adv]), np.concatenate([y_train, y_adv])) resistant_model.set_params(verbose=0, n_jobs=1) dump(resistant_model, RESISTANT_MODEL_PATH) X_test = np.load("{}/X_test.npy".format(TRAIN_TEST_DATA_DIR)) y_test = np.load("{}/y_test.npy".format(TRAIN_TEST_DATA_DIR)) y_pred_proba = model.predict_proba(X_test) y_pred = (y_pred_proba[:, 1] >= THRESHOLD).astype(bool) print("Original AUROC: {}, MCC: {}".format( roc_auc_score(y_test, y_pred_proba[:, 1]), matthews_corrcoef(y_test, y_pred))) y_pred_proba = resistant_model.predict_proba(X_test) y_pred = (y_pred_proba[:, 1] >= THRESHOLD).astype(bool) print("Resistant AUROC: {}, MCC: {}".format( roc_auc_score(y_test, y_pred_proba[:, 1]), matthews_corrcoef(y_test, y_pred)))
def run( MODEL_PATH=config["paths"]["model"], SCALER_PATH=config["paths"]["scaler"], X_ATTACK_CANDIDATES_PATH=config["paths"]["x_candidates"], ATTACK_RESULTS_DIR=config["dirs"]["attack_results"], RANDOM_SEED=config["random_seed"], LIST_N_OFFSPRING=config["list_n_offsprings"], BUDGET=config["budget"], N_REPETITION=config["n_repetition"], ALGORITHM=config["algorithm"], N_INITIAL_STATE=config["n_initial_state"], WEIGHT=config["weights"], ): Path(ATTACK_RESULTS_DIR).mkdir(parents=True, exist_ok=True) # ----- Load and create necessary objects model = load(MODEL_PATH) model.set_params(verbose=0, n_jobs=1) X_initial_states = np.load(X_ATTACK_CANDIDATES_PATH) scaler = Pickler.load_from_file(SCALER_PATH) encoder = VenusEncoder() X_initial_states = X_initial_states[:N_INITIAL_STATE] # ----- Check constraints venus_constraints.respect_constraints_or_exit(X_initial_states) # ----- Set random seed random.seed(RANDOM_SEED) np.random.seed(RANDOM_SEED) # ----- Copy the initial states n_repetition times X_initial_states = np.repeat(X_initial_states, N_REPETITION, axis=0) list_n_offsprings = np.array(LIST_N_OFFSPRING) list_n_generation = (BUDGET / list_n_offsprings).astype(np.int) list_pop_size = list_n_offsprings * 2 for i in range(len(list_n_offsprings)): n_gen = list_n_generation[i] pop_size = list_pop_size[i] n_offsprings = list_n_offsprings[i] logging.info("Parameters: {} {} {} ({}/{})".format( n_gen, pop_size, n_offsprings, i + 1, len(list_n_offsprings))) # Initial state loop (threaded) results = attack_multiple_input.attack( model, scaler, encoder, n_gen, pop_size, n_offsprings, X_initial_states, weight=WEIGHT, attack_type=ALGORITHM, ) efficient_results = [EfficientResult(result) for result in results] Pickler.save_to_file( efficient_results, "{}/results_{}.pickle".format(ATTACK_RESULTS_DIR, n_gen))
def run( MODEL_PATH=config["paths"]["model"], SCALER_PATH=config["paths"]["scaler"], X_ATTACK_CANDIDATES_PATH=config["paths"]["x_candidates"], ATTACK_RESULTS_DIR=config["dirs"]["attack_results"], RANDOM_SEED=config["random_seed"], N_REPETITION=config["n_repetition"], ALGORITHM=config["algorithm"], N_INITIAL_STATE=config["n_initial_state"], N_GEN=config["n_gen"], POP_SIZE=config["pop_size"], N_OFFSPRINGS=config["n_offsprings"], N_WEIGHTS=config["n_weights"], WEIGHT_MIN=config["weight_min"], WEIGHT_MAX=config["weight_max"], ): Path(ATTACK_RESULTS_DIR).mkdir(parents=True, exist_ok=True) # ----- Load and create necessary objects model = load(MODEL_PATH) model.set_params(verbose=0, n_jobs=1) X_initial_states = np.load(X_ATTACK_CANDIDATES_PATH) scaler = Pickler.load_from_file(SCALER_PATH) encoder = VenusEncoder() X_initial_states = X_initial_states[:N_INITIAL_STATE] # ----- Check constraints venus_constraints.respect_constraints_or_exit(X_initial_states) # ----- Set random seed random.seed(RANDOM_SEED) np.random.seed(RANDOM_SEED) # ----- Copy the initial states n_repetition times X_initial_states = np.repeat(X_initial_states, N_REPETITION, axis=0) for i in range(N_WEIGHTS): weight = np.random.uniform(WEIGHT_MIN, WEIGHT_MAX, 4) weight = { "alpha": weight[0], "beta": weight[1], "gamma": weight[2], "delta": weight[3], } logging.info("Parameters: {} ({}/{})".format(weight, i + 1, N_WEIGHTS)) # Initial state loop (threaded) results = attack_multiple_input.attack( model, scaler, encoder, N_GEN, POP_SIZE, N_OFFSPRINGS, X_initial_states, weight=weight, attack_type=ALGORITHM, ) efficient_results = [EfficientResult(result) for result in results] Pickler.save_to_file( efficient_results, "{}/results_{}_{}.pickle".format(ATTACK_RESULTS_DIR, RANDOM_SEED, i), )