From 27d763f1fb470810f54547f59c8882e44b5b1990 Mon Sep 17 00:00:00 2001 From: steffen-illium Date: Tue, 15 Jun 2021 14:11:40 +0200 Subject: [PATCH] journal linspace basins --- journal_basin_linspace_clones.py | 59 ++++++++++++++++---------------- journal_basins.py | 3 +- visualization.py | 3 +- 3 files changed, 32 insertions(+), 33 deletions(-) diff --git a/journal_basin_linspace_clones.py b/journal_basin_linspace_clones.py index 7da4b2c..0ead425 100644 --- a/journal_basin_linspace_clones.py +++ b/journal_basin_linspace_clones.py @@ -5,6 +5,7 @@ import random import pandas as pd import numpy as np +import torch from functionalities_test import is_identity_function, test_status from journal_basins import SpawnExperiment, prng, mean_invariate_manhattan_distance @@ -16,6 +17,7 @@ from sklearn.metrics import mean_squared_error as MSE import seaborn as sns from matplotlib import pyplot as plt + class SpawnLinspaceExperiment(SpawnExperiment): def spawn_and_continue(self, number_clones: int = None): @@ -44,11 +46,12 @@ class SpawnLinspaceExperiment(SpawnExperiment): # 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. - in_between_weights = np.linspace(net2_target_data, net2_target_data, number_clones) + in_between_weights = np.linspace(net1_target_data, net2_target_data, number_clones,endpoint=False) - for in_between_weight in in_between_weights: - clone = Net(net1.input_size, net1.hidden_size, net1.out_size, start_time=self.ST_steps) - clone.apply_weights(in_between_weight) + for j, in_between_weight in enumerate(in_between_weights): + clone = Net(net1.input_size, net1.hidden_size, net1.out_size, + name=f"{net1.name}_clone_{str(j)}", start_time=self.ST_steps) + clone.apply_weights(torch.as_tensor(in_between_weight)) clone.s_train_weights_history = copy.deepcopy(net1.s_train_weights_history) clone.number_trained = copy.deepcopy(net1.number_trained) @@ -73,14 +76,14 @@ class SpawnLinspaceExperiment(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." - # f"\nMSE({i},{j}): {MSE_post}" - # f"\nMAE({i},{j}): {MAE_post}" - # f"\nMIM({i},{j}): {MIM_post}\n") + print(f"Clone {j} (of net_{net1.name}) is fixpoint." + f"\nMSE({net1.name},{j}): {MSE_post}" + f"\nMAE({net1.name},{j}): {MAE_post}" + f"\nMIM({net1.name},{j}): {MIM_post}\n") self.nets.append(clone) - df.loc[clone.name] = [net1.name, MAE_pre, MAE_post, MSE_pre, MSE_post, MIM_pre, MIM_post, self.noise, - clone.is_fixpoint] + df.loc[clone.name] = [net1.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): @@ -107,36 +110,32 @@ if __name__ == '__main__': ST_log_step_size = 10 # Define number of networks & their architecture - nr_clones = 5 - ST_population_size = 2 + nr_clones = 8 + ST_population_size = 3 ST_net_hidden_size = 2 ST_net_learning_rate = 0.04 ST_name_hash = random.getrandbits(32) print(f"Running the Spawn experiment:") - exp_list = [] - for noise_factor in range(2, 5): - exp = SpawnExperiment( - population_size=ST_population_size, - log_step_size=ST_log_step_size, - net_input_size=NET_INPUT_SIZE, - net_hidden_size=ST_net_hidden_size, - net_out_size=NET_OUT_SIZE, - net_learning_rate=ST_net_learning_rate, - epochs=ST_epochs, - st_steps=ST_steps, - nr_clones=nr_clones, - noise=pow(10, -noise_factor), - directory=Path('output') / 'spawn_basin' / f'{ST_name_hash}' / f'10e-{noise_factor}' - ) - exp_list.append(exp) + df = SpawnLinspaceExperiment( + population_size=ST_population_size, + log_step_size=ST_log_step_size, + net_input_size=NET_INPUT_SIZE, + net_hidden_size=ST_net_hidden_size, + net_out_size=NET_OUT_SIZE, + net_learning_rate=ST_net_learning_rate, + epochs=ST_epochs, + st_steps=ST_steps, + nr_clones=nr_clones, + noise=None, + directory=Path('output') / 'spawn_basin' / f'{ST_name_hash}' / f'linage' + ).df # Boxplot with counts of nr_fixpoints, nr_other, nr_etc. on y-axis - df = pd.concat([exp.df for exp in exp_list]) sns.countplot(data=df, x="noise", hue="status_post") plt.savefig(f"output/spawn_basin/{ST_name_hash}/fixpoint_status_countplot.png") # 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_basins.py b/journal_basins.py index c921780..85bda58 100644 --- a/journal_basins.py +++ b/journal_basins.py @@ -124,7 +124,6 @@ class SpawnExperiment: # 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) - self.save() def populate_environment(self): @@ -243,7 +242,7 @@ if __name__ == "__main__": # Define number of networks & their architecture nr_clones = 5 - ST_population_size = 1 + ST_population_size = 2 ST_net_hidden_size = 2 ST_net_learning_rate = 0.04 ST_name_hash = random.getrandbits(32) diff --git a/visualization.py b/visualization.py index d3544f4..1df47ae 100644 --- a/visualization.py +++ b/visualization.py @@ -216,7 +216,8 @@ def plot_3d_soup(nets_list, exp_name, directory: Union[str, Path]): # will send forward the number "1" for batch size with the variable . irrelevant_batch_size = 1 - plot_3d_self_train(nets_list, exp_name, directory, irrelevant_batch_size, False) + # plot_3d_self_train(nets_list, exp_name, directory, irrelevant_batch_size, False) + plot_3d_self_train(nets_list, exp_name, directory, 10, True) def line_chart_fixpoints(fixpoint_counters_history: list, epochs: int, ST_steps_between_SA: int,