コード例 #1
0
                                                       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.')

# %%
# Results
original_pred = model_svm.predict(x_test)
pred_ae = model_svm.predict(adversarial_examples)
y_ae = and_gen.get_y(adversarial_examples)

for xx, ae, p, p_ae, yy_ae in zip(x_test, adversarial_examples, original_pred,
                                  pred_ae, y_ae):
    if p != p_ae:
        print(f'from [{xx[0]: .4f}, {xx[1]: .4f}] = {p} to ' +
              f'[{ae[0]: .4f}, {ae[1]: .4f}] = {p_ae}; True y = {yy_ae}')

matches = np.equal(y_ae, pred)
count = len(matches[matches == False])
print(f'\nFound {count} Adversarial Examples out of ' +
      f'{len(y_ae)}. {count / len(y_ae) * 100.0:.4f}% successful rate')

# %%
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(x_min, x_max, h))
Z = model_svm.predict(np.c_[xx.ravel(), yy.ravel()])
コード例 #2
0
        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.')

# %%
# Results
original_pred = model_svm.predict(x_test)
pred_ae = model_svm.predict(adversarial_examples)
y_ae = and_gen.get_y(adversarial_examples)

matches = np.equal(y_ae, pred_ae)
ind_misclassified = np.where(matches == False)[0]

# Print all misclassified samples
# for i in ind_misclassified:
#     print(
#         f'from [{x_test[i][0]: .4f}, {x_test[i][1]: .4f}] = {original_pred[i]}'
#         + f' to [{adversarial_examples[i][0]: .4f}, '
#         + f'{adversarial_examples[i][1]: .4f}] = {pred_ae[i]};'
#         + f' True y = {y_ae[i]}')

missclassified = len(ind_misclassified)
print(f'Misclassified = {missclassified}')
コード例 #3
0
    [0.5010, 0.5020],  # 1
    [0.5020, 0.5020],  # 1
    # out of training range
    # positive
    [0.5003, 1.0000],  # 1
    [1.0000, 0.5001],  # 1
    [1.4584, 1.0000],  # 1
    [1.0000, 1.4010],  # 1
    # negative
    [-0.7756,  0.0000],  # 0
    [1.4500,  0.0000],  # 0
    [0.0000, -0.9839],  # 0
    [0.0000,  1.4690]   # 0
])

y_ae = and_gen.get_y(x_ae)
print(*y_ae, sep=', ')

# %%
def print_misclassified_samples(x, y, pred):
    ind_mis = np.where(np.logical_xor(y, pred) == True)
    for xx, yy, pp in zip(x[ind_mis], y[ind_mis], pred[ind_mis]):
        print(f'[{xx[0]: .4f}, {xx[1]: .4f}] y = {yy} pred = {pp}')

# %%
# Do NOT change the initial predictions
pred_ae = tuple(model_svm1.predict(x_ae))
print('Misclassified samples:')
print_misclassified_samples(x_ae, y_ae, np.array(pred_ae))

# %% [markdown]