diff --git a/experiments/mixed_setting_exp.py b/experiments/mixed_setting_exp.py index a778775..a1850ea 100644 --- a/experiments/mixed_setting_exp.py +++ b/experiments/mixed_setting_exp.py @@ -95,7 +95,7 @@ class MixedSettingExperiment: # and only they need the batch size. To not affect the number of epochs shown in the 3D plot, will send # forward the number "1" for batch size with the variable irrelevant_batch_size = 1 - plot_3d_self_train(self.nets, exp_name, self.directory_name, irrelevant_batch_size) + plot_3d_self_train(self.nets, exp_name, self.directory_name, irrelevant_batch_size, True) def count_fixpoints(self): exp_details = f"SA steps: {self.SA_steps}; ST steps: {self.ST_steps_between_SA}" diff --git a/experiments/soup_exp.py b/experiments/soup_exp.py index 31916f5..678b772 100644 --- a/experiments/soup_exp.py +++ b/experiments/soup_exp.py @@ -88,8 +88,7 @@ class SoupExperiment: # Testing for fixpoints after each batch of ST steps to see relevant data if i % self.ST_steps == 0: test_for_fixpoints(self.fixpoint_counters, self.population) - fixpoints_percentage = round((self.fixpoint_counters["fix_zero"] + self.fixpoint_counters["fix_weak"] + - self.fixpoint_counters["fix_sec"]) / self.population_size, 1) + fixpoints_percentage = round(self.fixpoint_counters["identity_func"] / self.population_size, 1) self.fixpoint_counters_history.append(fixpoints_percentage) # Resetting the fixpoint counter. Last iteration not to be reset - diff --git a/journal_basins.py b/journal_basins.py index df16f14..72bf21c 100644 --- a/journal_basins.py +++ b/journal_basins.py @@ -17,13 +17,14 @@ import pandas as pd import seaborn as sns from matplotlib import pyplot as plt + def prng(): return random.random() def l1(tup): a, b = tup - return abs(a-b) + return abs(a - b) def mean_invariate_manhattan_distance(x, y): @@ -65,13 +66,14 @@ def distance_from_parent(nets, distance="MIM", print_it=True): for dist in distance_range: for idx, clone in enumerate(clones): - clone_weights = clone.create_target_weights(clone.input_weight_matrix()) + clone_weights = clone.create_target_weights(clone.input_weight_matrix()) if distance in ["MSE"]: matrix[idx][dist] = MSE(parent_weights, clone_weights) < pow(10, -dist) elif distance in ["MAE"]: matrix[idx][dist] = MAE(parent_weights, clone_weights) < pow(10, -dist) elif distance in ["MIM"]: - matrix[idx][dist] = mean_invariate_manhattan_distance(parent_weights, clone_weights) < pow(10, -dist) + matrix[idx][dist] = mean_invariate_manhattan_distance(parent_weights, clone_weights) < pow(10, + -dist) if print_it: print(f"\nDistances from parent {parent.name} [{distance}]:") @@ -80,9 +82,10 @@ def distance_from_parent(nets, distance="MIM", print_it=True): print(tabulate(matrix, showindex=row_headers, headers=col_headers, tablefmt='orgtbl')) list_of_matrices.append(matrix) - + return list_of_matrices + class SpawnExperiment: @staticmethod @@ -92,12 +95,12 @@ class SpawnExperiment: for layer_id, layer_name in enumerate(network.state_dict()): for line_id, line_values in enumerate(network.state_dict()[layer_name]): for weight_id, weight_value in enumerate(network.state_dict()[layer_name][line_id]): - #network.state_dict()[layer_name][line_id][weight_id] = weight_value + noise + # network.state_dict()[layer_name][line_id][weight_id] = weight_value + noise if prng() < 0.5: network.state_dict()[layer_name][line_id][weight_id] = weight_value + noise else: network.state_dict()[layer_name][line_id][weight_id] = weight_value - noise - + return network def __init__(self, population_size, log_step_size, net_input_size, net_hidden_size, net_out_size, net_learning_rate, @@ -144,7 +147,9 @@ class SpawnExperiment: def spawn_and_continue(self, number_clones: int = None): number_clones = number_clones or self.nr_clones - df = pd.DataFrame(columns=['parent', 'MAE_pre','MAE_post', 'MSE_pre', 'MSE_post', 'MIM_pre', 'MIM_post', 'noise', 'status_post']) + df = pd.DataFrame( + columns=['parent', 'MAE_pre', 'MAE_post', 'MSE_pre', 'MSE_post', 'MIM_pre', 'MIM_post', 'noise', + 'status_post']) # For every initial net {i} after populating (that is fixpoint after first epoch); for i in range(self.population_size): @@ -155,7 +160,7 @@ class SpawnExperiment: net.start_time = self.ST_steps - 150 net_input_data = net.input_weight_matrix() net_target_data = net.create_target_weights(net_input_data) - + if is_identity_function(net): print(f"\nNet {i} is fixpoint") @@ -171,7 +176,7 @@ class SpawnExperiment: clone = self.apply_noise(clone, rand_noise) clone.s_train_weights_history = copy.deepcopy(net.s_train_weights_history) clone.number_trained = copy.deepcopy(net.number_trained) - + # Pre Training distances (after noise application of course) clone_pre_weights = clone.create_target_weights(clone.input_weight_matrix()) MAE_pre = MAE(net_target_data, clone_pre_weights) @@ -182,7 +187,7 @@ class SpawnExperiment: for _ in range(self.epochs - 1): for _ in range(self.ST_steps): clone.self_train(1, self.log_step_size, self.net_learning_rate) - + # Post Training distances for comparison clone_post_weights = clone.create_target_weights(clone.input_weight_matrix()) MAE_post = MAE(net_target_data, clone_post_weights) @@ -192,23 +197,24 @@ class SpawnExperiment: # .. log to data-frame and add to nets for 3d plotting if they are fixpoints themselves. test_status(clone) if is_identity_function(clone): - print(f"Clone {j} (of net_{i}) is fixpoint." + print(f"Clone {j} (of net_{i}) is fixpoint." f"\nMSE({i},{j}): {MSE_post}" f"\nMAE({i},{j}): {MAE_post}" f"\nMIM({i},{j}): {MIM_post}\n") self.nets.append(clone) - df.loc[clone.name] = [net.name, MAE_pre, MAE_post, MSE_pre, MSE_post, MIM_pre, MIM_post, self.noise, clone.is_fixpoint] + df.loc[clone.name] = [net.name, MAE_pre, MAE_post, MSE_pre, MSE_post, MIM_pre, MIM_post, self.noise, + clone.is_fixpoint] # Finally take parent net {i} and finish it's training for comparison to clone development. for _ in range(self.epochs - 1): for _ in range(self.ST_steps): net.self_train(1, self.log_step_size, self.net_learning_rate) net_weights_after = net.create_target_weights(net.input_weight_matrix()) - print(f"Parent net's distance to original position." - f"\nMSE(OG,new): {MAE(net_target_data, net_weights_after)}" - f"\nMAE(OG,new): {MSE(net_target_data, net_weights_after)}" - f"\nMIM(OG,new): {mean_invariate_manhattan_distance(net_target_data, net_weights_after)}\n") + print(f"Parent net's distance to original position." + f"\nMSE(OG,new): {MAE(net_target_data, net_weights_after)}" + f"\nMAE(OG,new): {MSE(net_target_data, net_weights_after)}" + f"\nMIM(OG,new): {mean_invariate_manhattan_distance(net_target_data, net_weights_after)}\n") self.df = df @@ -222,11 +228,11 @@ class SpawnExperiment: self.loss_history.append(net_loss_history) plot_loss(self.loss_history, self.directory) - def save(self): pickle.dump(self, open(f"{self.directory}/experiment_pickle.p", "wb")) print(f"\nSaved experiment to {self.directory}.") + if __name__ == "__main__": NET_INPUT_SIZE = 4 @@ -248,7 +254,7 @@ if __name__ == "__main__": print(f"Running the Spawn experiment:") exp_list = [] - for noise_factor in range(2,5): + for noise_factor in range(2, 5): exp = SpawnExperiment( population_size=ST_population_size, log_step_size=ST_log_step_size, @@ -272,4 +278,4 @@ if __name__ == "__main__": # Catplot (either kind="point" or "box") that shows before-after training distances to parent mlt = df[["MIM_pre", "MIM_post", "noise"]].melt("noise", var_name="time", value_name='Average Distance') sns.catplot(data=mlt, x="time", y="Average Distance", col="noise", kind="point", col_wrap=5, sharey=False) - plt.savefig(f"output/spawn_basin/{ST_name_hash}/clone_distance_catplot.png") \ No newline at end of file + plt.savefig(f"output/spawn_basin/{ST_name_hash}/clone_distance_catplot.png") diff --git a/journal_soup_basins.py b/journal_soup_basins.py index c9e06ab..f701ef4 100644 --- a/journal_soup_basins.py +++ b/journal_soup_basins.py @@ -124,11 +124,13 @@ class SoupSpawnExperiment: # Populating environment & evolving entities self.nets = [] + self.id_functions = [] + self.clone_soup = [] self.populate_environment() - self.evolve() self.spawn_and_continue() - self.weights_evolution_3d_experiment() + self.weights_evolution_3d_experiment(self.nets, "parents") + self.weights_evolution_3d_experiment(self.clone_soup, "clones") # self.visualize_loss() self.distance_matrix = distance_matrix(self.nets, print_it=False) self.parent_clone_distances = distance_from_parent(self.nets, print_it=False) @@ -140,27 +142,35 @@ class SoupSpawnExperiment: for i in loop_population_size: loop_population_size.set_description("Populating experiment %s" % i) - net_name = f"soup_net_{str(i)}" + net_name = f"parent_net_{str(i)}" net = Net(self.net_input_size, self.net_hidden_size, self.net_out_size, net_name) + for _ in range(self.ST_steps): + net.self_train(1, self.log_step_size, self.net_learning_rate) + self.nets.append(net) - def evolve(self): - loop_epochs = tqdm(range(self.epochs)) + if is_identity_function(net): + self.id_functions.append(net) + + def evolve(self, population): + print(f"Clone soup has a population of {len(population)} networks") + + loop_epochs = tqdm(range(self.epochs-1)) for i in loop_epochs: - loop_epochs.set_description("Evolving soup %s" % i) + loop_epochs.set_description("\nEvolving clone soup %s" % i) # A network attacking another network with a given percentage if random.randint(1, 100) <= self.attack_chance: - random_net1, random_net2 = random.sample(range(self.population_size), 2) - random_net1 = self.nets[random_net1] - random_net2 = self.nets[random_net2] + random_net1, random_net2 = random.sample(range(len(population)), 2) + random_net1 = population[random_net1] + random_net2 = population[random_net2] print(f"\n Attack: {random_net1.name} -> {random_net2.name}") random_net1.attack(random_net2) # Self-training each network in the population - for j in range(self.population_size): - net = self.nets[j] + for j in range(len(population)): + net = population[j] for _ in range(self.ST_steps): net.self_train(1, self.log_step_size, self.net_learning_rate) @@ -172,8 +182,10 @@ class SoupSpawnExperiment: columns=['parent', 'MAE_pre', 'MAE_post', 'MSE_pre', 'MSE_post', 'MIM_pre', 'MIM_post', 'noise', 'status_post']) + # MAE_pre, MSE_pre, MIM_pre = 0, 0, 0 + # For every initial net {i} after populating (that is fixpoint after first epoch); - for i in range(self.population_size): + for i in range(len(self.id_functions)): net = self.nets[i] # We set parent start_time to just before this epoch ended, so plotting is zoomed in. Comment out to # to see full trajectory (but the clones will be very hard to see). @@ -182,66 +194,73 @@ class SoupSpawnExperiment: net_input_data = net.input_weight_matrix() net_target_data = net.create_target_weights(net_input_data) - if is_identity_function(net): - print(f"\nNet {i} is fixpoint") + print(f"\nNet {i} is fixpoint") - # Clone the fixpoint x times and add (+-)self.noise to weight-sets randomly; - # To plot clones starting after first epoch (z=ST_steps), set that as start_time! - # To make sure PCA will plot the same trajectory up until this point, we clone the - # parent-net's weight history as well. - for j in range(number_clones): - clone = Net(net.input_size, net.hidden_size, net.out_size, - f"ST_net_{str(i)}_clone_{str(j)}", start_time=self.ST_steps) - clone.load_state_dict(copy.deepcopy(net.state_dict())) - rand_noise = prng() * self.noise - clone = self.apply_noise(clone, rand_noise) - clone.s_train_weights_history = copy.deepcopy(net.s_train_weights_history) - clone.number_trained = copy.deepcopy(net.number_trained) + # Clone the fixpoint x times and add (+-)self.noise to weight-sets randomly; + # To plot clones starting after first epoch (z=ST_steps), set that as start_time! + # To make sure PCA will plot the same trajectory up until this point, we clone the + # parent-net's weight history as well. + for j in range(number_clones): + clone = Net(net.input_size, net.hidden_size, net.out_size, + f"net_{str(i)}_clone_{str(j)}", start_time=self.ST_steps) + clone.load_state_dict(copy.deepcopy(net.state_dict())) + rand_noise = prng() * self.noise + clone = self.apply_noise(clone, rand_noise) + clone.s_train_weights_history = copy.deepcopy(net.s_train_weights_history) + clone.number_trained = copy.deepcopy(net.number_trained) - # Pre Training distances (after noise application of course) - clone_pre_weights = clone.create_target_weights(clone.input_weight_matrix()) - MAE_pre = MAE(net_target_data, clone_pre_weights) - MSE_pre = MSE(net_target_data, clone_pre_weights) - MIM_pre = mean_invariate_manhattan_distance(net_target_data, clone_pre_weights) + # Pre Training distances (after noise application of course) + clone_pre_weights = clone.create_target_weights(clone.input_weight_matrix()) + MAE_pre = MAE(net_target_data, clone_pre_weights) + MSE_pre = MSE(net_target_data, clone_pre_weights) + MIM_pre = mean_invariate_manhattan_distance(net_target_data, clone_pre_weights) - # Then finish training each clone {j} (for remaining epoch-1 * ST_steps) .. - for _ in range(self.epochs - 1): - for _ in range(self.ST_steps): - clone.self_train(1, self.log_step_size, self.net_learning_rate) + net.children.append(clone) + self.clone_soup.append(clone) - # Post Training distances for comparison - clone_post_weights = clone.create_target_weights(clone.input_weight_matrix()) - MAE_post = MAE(net_target_data, clone_post_weights) - MSE_post = MSE(net_target_data, clone_post_weights) - MIM_post = mean_invariate_manhattan_distance(net_target_data, clone_post_weights) + self.evolve(self.clone_soup) - # .. log to data-frame and add to nets for 3d plotting if they are fixpoints themselves. - test_status(clone) - if is_identity_function(clone): - print(f"Clone {j} (of net_{i}) is fixpoint." - f"\nMSE({i},{j}): {MSE_post}" - f"\nMAE({i},{j}): {MAE_post}" - f"\nMIM({i},{j}): {MIM_post}\n") - self.nets.append(clone) + for i in range(len(self.id_functions)): + net = self.nets[i] + net_input_data = net.input_weight_matrix() + net_target_data = net.create_target_weights(net_input_data) - df.loc[clone.name] = [net.name, MAE_pre, MAE_post, MSE_pre, MSE_post, MIM_pre, MIM_post, self.noise, - clone.is_fixpoint] + for j in range(len(net.children)): + clone = net.children[j] - # Finally take parent net {i} and finish it's training for comparison to clone development. - for _ in range(self.epochs - 1): - for _ in range(self.ST_steps): - net.self_train(1, self.log_step_size, self.net_learning_rate) - net_weights_after = net.create_target_weights(net.input_weight_matrix()) - print(f"Parent net's distance to original position." - f"\nMSE(OG,new): {MAE(net_target_data, net_weights_after)}" - f"\nMAE(OG,new): {MSE(net_target_data, net_weights_after)}" - f"\nMIM(OG,new): {mean_invariate_manhattan_distance(net_target_data, net_weights_after)}\n") + # Post Training distances for comparison + clone_post_weights = clone.create_target_weights(clone.input_weight_matrix()) + MAE_post = MAE(net_target_data, clone_post_weights) + MSE_post = MSE(net_target_data, clone_post_weights) + MIM_post = mean_invariate_manhattan_distance(net_target_data, clone_post_weights) + + # .. log to data-frame and add to nets for 3d plotting if they are fixpoints themselves. + test_status(clone) + if is_identity_function(clone): + print(f"Clone {j} (of net_{i}) is fixpoint." + f"\nMSE({i},{j}): {MSE_post}" + f"\nMAE({i},{j}): {MAE_post}" + f"\nMIM({i},{j}): {MIM_post}\n") + self.nets.append(clone) + + df.loc[clone.name] = [net.name, MAE_pre, MAE_post, MSE_pre, MSE_post, MIM_pre, MIM_post, self.noise, + clone.is_fixpoint] + + # Finally take parent net {i} and finish it's training for comparison to clone development. + for _ in range(self.epochs - 1): + for _ in range(self.ST_steps): + net.self_train(1, self.log_step_size, self.net_learning_rate) + net_weights_after = net.create_target_weights(net.input_weight_matrix()) + print(f"Parent net's distance to original position." + f"\nMSE(OG,new): {MAE(net_target_data, net_weights_after)}" + f"\nMAE(OG,new): {MSE(net_target_data, net_weights_after)}" + f"\nMIM(OG,new): {mean_invariate_manhattan_distance(net_target_data, net_weights_after)}\n") self.df = df - def weights_evolution_3d_experiment(self): - exp_name = f"soup_basins_{str(len(self.nets))}_nets_3d_weights_PCA" - return plot_3d_soup(self.nets, exp_name, self.directory) + def weights_evolution_3d_experiment(self, nets_population, suffix): + exp_name = f"soup_basins_{str(len(self.nets))}_nets_3d_weights_PCA_{suffix}" + return plot_3d_soup(nets_population, exp_name, self.directory) def visualize_loss(self): for i in range(len(self.nets)): @@ -262,12 +281,12 @@ if __name__ == "__main__": # Define number of runs & name: ST_runs = 1 ST_runs_name = "test-27" - soup_ST_steps = 2500 + soup_ST_steps = 1500 soup_epochs = 2 soup_log_step_size = 10 # Define number of networks & their architecture - nr_clones = 15 + nr_clones = 2 soup_population_size = 2 soup_net_hidden_size = 2 soup_net_learning_rate = 0.04 diff --git a/network.py b/network.py index deccbdc..fa3ff95 100644 --- a/network.py +++ b/network.py @@ -48,7 +48,10 @@ class Net(nn.Module): def __init__(self, i_size: int, h_size: int, o_size: int, name=None, start_time=1) -> None: super().__init__() self.start_time = start_time + self.name = name + self.children = [] + self.input_size = i_size self.hidden_size = h_size self.out_size = o_size diff --git a/visualization.py b/visualization.py index 45080b7..fe9d3c8 100644 --- a/visualization.py +++ b/visualization.py @@ -73,7 +73,7 @@ def bar_chart_fixpoints(fixpoint_counter: Dict, population_size: int, directory: def plot_3d(matrices_weights_history, directory: Union[str, Path], population_size, z_axis_legend, - exp_name="experiment", is_trained="", batch_size=1, plot_pca_together=False): + exp_name="experiment", is_trained="", batch_size=1, plot_pca_together=False, nets_array=None): """ Plotting the the weights of the nets in a 3d form using principal component analysis (PCA) """ fig = plt.figure() @@ -134,7 +134,10 @@ def plot_3d(matrices_weights_history, directory: Union[str, Path], population_si zdata = np.arange(start_time, len(ydata)*batch_size+start_time, batch_size) ax.plot3D(xdata, ydata, zdata, label=f"net {i}") - ax.scatter(np.asarray(xdata), np.asarray(ydata), zdata, s=7) + if "parent" in nets_array[i].name: + ax.scatter(np.asarray(xdata), np.asarray(ydata), zdata, s=3, c="b") + else: + ax.scatter(np.asarray(xdata), np.asarray(ydata), zdata, s=3) steps = mpatches.Patch(color="white", label=f"{z_axis_legend}: {len(matrices_weights_history)} steps") population_size = mpatches.Patch(color="white", label=f"Population: {population_size} networks") @@ -165,7 +168,7 @@ def plot_3d(matrices_weights_history, directory: Union[str, Path], population_si else: plt.savefig(str(filepath)) - plt.show() + # plt.show() def plot_3d_self_train(nets_array: List, exp_name: str, directory: Union[str, Path], batch_size: int, plot_pca_together: bool): @@ -177,12 +180,12 @@ def plot_3d_self_train(nets_array: List, exp_name: str, directory: Union[str, Pa for i in loop_nets_array: loop_nets_array.set_description("Creating ST weights history %s" % i) - matrices_weights_history.append( (nets_array[i].s_train_weights_history, nets_array[i].start_time) ) + matrices_weights_history.append((nets_array[i].s_train_weights_history, nets_array[i].start_time)) z_axis_legend = "epochs" return plot_3d(matrices_weights_history, directory, len(nets_array), z_axis_legend, exp_name, "", batch_size, - plot_pca_together=plot_pca_together) + plot_pca_together=plot_pca_together, nets_array=nets_array) def plot_3d_self_application(nets_array: List, exp_name: str, directory_name: Union[str, Path], batch_size: int) -> None: