print(f'Positive mean = [{mu_train_c1[0]:.4f}, {mu_train_c1[1]:.4f}]') print(f'Negative mean = [{mu_train_c0[0]:.4f}, {mu_train_c0[1]:.4f}]') # %% # Generating Adversarial Examples from test set # This implementation uses multiple iterations to update x, until all of them # match the target classes. # scale down the parameter epsilon = 0.0006 / np.average(x_norms) adversarial_examples = np.copy(x_test) # make a clone pred = pred_test # Aimed to generate labels which completely opposite to the true labels, # not the prediction from SVM. targets = and_gen.get_not_y(y_test) epoch = 1 while np.array_equal(pred, targets) == False: adversarial_examples = adversarial.moving_mean( x=adversarial_examples, y=pred, targets=targets, means={0: mu_train_c0, 1: mu_train_c1}, epsilon=epsilon, verbose=0, epoch=epoch) pred = model_svm.predict(adversarial_examples) epoch += 1 print(f'Completed after {epoch} epoch.')
max_epoch = 2000 ind_train_c0 = np.where(y_train == 0) x_train_c0 = x_train[ind_train_c0] mu_train_c0 = np.mean(x_train_c0, axis=0) ind_train_c1 = np.where(y_train == 1) x_train_c1 = x_train[ind_train_c1] mu_train_c1 = np.mean(x_train_c1, axis=0) print('Negative mean =', mu_train_c0) print('Positive mean =', mu_train_c1) x_ae = np.copy(x_test) pred_ae = np.copy(pred_test) targets = get_not_y(y_test) epoch = 1 while np.array_equal(pred_ae, targets) == False and epoch <= max_epoch: x_ae = adversarial.moving_mean(x=x_ae, y=pred_ae, targets=targets, means={ 0: mu_train_c0, 1: mu_train_c1 }, epsilon=epsilon, verbose=0, epoch=epoch) pred_ae = model.predict(x_ae) epoch += 1