This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fitness_similarity(X): | |
""" Maximize adv_target probability while MSE from adv_sample """ | |
y = model.predict(pt.Tensor(X).unsqueeze(1)).detach().numpy() | |
y_target = y[:, adv_target] | |
mse = np.sqrt(np.power(X - adv_sample, 2).mean(axis=1).mean(axis=1)) | |
return y_target - mse |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fitness_class_probability_empty(X): | |
""" Maximize probability of adversarial target class, penalizing mean pixel intensity """ | |
y = model.predict(pt.Tensor(X).unsqueeze(1)).detach().numpy() | |
y_target = y[:, adv_target] | |
X_mean = X.mean(axis=1).mean(axis=1) | |
return y_target - X_mean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fitness_class_probability(X): | |
""" Maximize probability of adversarial target class """ | |
y = model.predict(pt.Tensor(X).unsqueeze(1)).detach().numpy() | |
y_target = y[:, adv_target] | |
return y_target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch as pt | |
class CNN(pt.nn.Module): | |
def __init__(self): | |
super().__init__() | |
self.model = pt.nn.Sequential( | |
pt.nn.Conv2d(1, 30, 3), | |
pt.nn.MaxPool2d(2), | |
pt.nn.ReLU(), | |
pt.nn.Conv2d(30, 30, 3), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Generation #: best score | |
Generation 50 : 0.93 | |
Generation 100 : 0.9375 | |
Generation 150 : 0.9375 | |
Generation 200 : 0.9375 | |
Generation 250 : 0.94 | |
Generation 300 : 0.945 | |
Generation 350 : 0.945 | |
Generation 400 : 0.95 | |
Generation 450 : 0.95 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solve(target, delta, population_size=200, n_generations=300): | |
""" | |
:param target: 20x20 array that represents field in stopping condition | |
:param delta: number of steps to revert | |
:param n_generations: number of evolution generations. Overrides initialization value if specified | |
:return: 20x20 array that represents the best start field found and associated fitness value | |
""" | |
population = generate_population(population_size) | |
for generation in range(n_generations): | |
population = evolve(population, target, delta) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def evolve(population, target, delta, retain_frac=0.8, retain_random=0.05, mutate_chance=0.05): | |
""" | |
Evolution step | |
:param population: list or candidate solutions | |
:param target: 20x20 array that represents field in stopping condition | |
:param delta: number of steps to revert | |
:param retain_frac: percent of top individuals to proceed into the next genetation | |
:param retain_random: chance of retaining sub-optimal individual | |
:param mutate_chance: chance of mutating the particular individual | |
:return: new generation of the same size |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def crossover(mom, dad): | |
""" Take two parents, return two children, interchanging half of the allels of each parent randomly """ | |
select_mask = np.random.binomial(1, 0.5, size=(20, 20)).astype('bool') | |
child1, child2 = np.copy(mom), np.copy(dad) | |
child1[select_mask] = dad[select_mask] | |
child2[select_mask] = mom[select_mask] | |
return child1, child2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def mutate(field, switch_frac=0.1): | |
""" Inplace mutation of the provided field """ | |
a = np.random.binomial(1, switch_frac, size=(20, 20)).astype('bool') | |
field[a] += 1 | |
field[a] %= 2 | |
return field |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def selection(population, scores, retain_frac=0.8, retain_random=0.05): | |
""" | |
Apply selection operator to the population | |
:param population: list of candidate solutions | |
:param scores: list of score associated with each individual | |
:param retain_frac: percent of top individuals to retain | |
:param retain_random: chance of retaining sub-optimal individuals in the population | |
""" | |
retain_len = int(len(scores) * retain_frac) | |
sorted_indices = np.argsort(scores)[::-1] |
NewerOlder