journal linspace basins

This commit is contained in:
steffen-illium
2021-06-15 14:11:40 +02:00
parent 0ba109c083
commit 27d763f1fb
3 changed files with 32 additions and 33 deletions

View File

@ -5,6 +5,7 @@ import random
import pandas as pd import pandas as pd
import numpy as np import numpy as np
import torch
from functionalities_test import is_identity_function, test_status from functionalities_test import is_identity_function, test_status
from journal_basins import SpawnExperiment, prng, mean_invariate_manhattan_distance 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 import seaborn as sns
from matplotlib import pyplot as plt from matplotlib import pyplot as plt
class SpawnLinspaceExperiment(SpawnExperiment): class SpawnLinspaceExperiment(SpawnExperiment):
def spawn_and_continue(self, number_clones: int = None): 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 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 # To make sure PCA will plot the same trajectory up until this point, we clone the
# parent-net's weight history as well. # 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: for j, in_between_weight in enumerate(in_between_weights):
clone = Net(net1.input_size, net1.hidden_size, net1.out_size, start_time=self.ST_steps) clone = Net(net1.input_size, net1.hidden_size, net1.out_size,
clone.apply_weights(in_between_weight) 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.s_train_weights_history = copy.deepcopy(net1.s_train_weights_history)
clone.number_trained = copy.deepcopy(net1.number_trained) 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. # .. log to data-frame and add to nets for 3d plotting if they are fixpoints themselves.
test_status(clone) test_status(clone)
if is_identity_function(clone): if is_identity_function(clone):
#print(f"Clone {j} (of net_{i}) is fixpoint." print(f"Clone {j} (of net_{net1.name}) is fixpoint."
# f"\nMSE({i},{j}): {MSE_post}" f"\nMSE({net1.name},{j}): {MSE_post}"
# f"\nMAE({i},{j}): {MAE_post}" f"\nMAE({net1.name},{j}): {MAE_post}"
# f"\nMIM({i},{j}): {MIM_post}\n") f"\nMIM({net1.name},{j}): {MIM_post}\n")
self.nets.append(clone) self.nets.append(clone)
df.loc[clone.name] = [net1.name, MAE_pre, MAE_post, MSE_pre, MSE_post, MIM_pre, MIM_post, self.noise, df.loc[clone.name] = [net1.name, MAE_pre, MAE_post, MSE_pre, MSE_post, MIM_pre, MIM_post,
clone.is_fixpoint] self.noise, clone.is_fixpoint]
# Finally take parent net {i} and finish it's training for comparison to clone development. # 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.epochs - 1):
@ -107,36 +110,32 @@ if __name__ == '__main__':
ST_log_step_size = 10 ST_log_step_size = 10
# Define number of networks & their architecture # Define number of networks & their architecture
nr_clones = 5 nr_clones = 8
ST_population_size = 2 ST_population_size = 3
ST_net_hidden_size = 2 ST_net_hidden_size = 2
ST_net_learning_rate = 0.04 ST_net_learning_rate = 0.04
ST_name_hash = random.getrandbits(32) ST_name_hash = random.getrandbits(32)
print(f"Running the Spawn experiment:") print(f"Running the Spawn experiment:")
exp_list = [] df = SpawnLinspaceExperiment(
for noise_factor in range(2, 5): population_size=ST_population_size,
exp = SpawnExperiment( log_step_size=ST_log_step_size,
population_size=ST_population_size, net_input_size=NET_INPUT_SIZE,
log_step_size=ST_log_step_size, net_hidden_size=ST_net_hidden_size,
net_input_size=NET_INPUT_SIZE, net_out_size=NET_OUT_SIZE,
net_hidden_size=ST_net_hidden_size, net_learning_rate=ST_net_learning_rate,
net_out_size=NET_OUT_SIZE, epochs=ST_epochs,
net_learning_rate=ST_net_learning_rate, st_steps=ST_steps,
epochs=ST_epochs, nr_clones=nr_clones,
st_steps=ST_steps, noise=None,
nr_clones=nr_clones, directory=Path('output') / 'spawn_basin' / f'{ST_name_hash}' / f'linage'
noise=pow(10, -noise_factor), ).df
directory=Path('output') / 'spawn_basin' / f'{ST_name_hash}' / f'10e-{noise_factor}'
)
exp_list.append(exp)
# Boxplot with counts of nr_fixpoints, nr_other, nr_etc. on y-axis # 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") sns.countplot(data=df, x="noise", hue="status_post")
plt.savefig(f"output/spawn_basin/{ST_name_hash}/fixpoint_status_countplot.png") 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 # 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') 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) 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") plt.savefig(f"output/spawn_basin/{ST_name_hash}/clone_distance_catplot.png")

View File

@ -124,7 +124,6 @@ class SpawnExperiment:
# self.visualize_loss() # self.visualize_loss()
self.distance_matrix = distance_matrix(self.nets, print_it=False) self.distance_matrix = distance_matrix(self.nets, print_it=False)
self.parent_clone_distances = distance_from_parent(self.nets, print_it=False) self.parent_clone_distances = distance_from_parent(self.nets, print_it=False)
self.save() self.save()
def populate_environment(self): def populate_environment(self):
@ -243,7 +242,7 @@ if __name__ == "__main__":
# Define number of networks & their architecture # Define number of networks & their architecture
nr_clones = 5 nr_clones = 5
ST_population_size = 1 ST_population_size = 2
ST_net_hidden_size = 2 ST_net_hidden_size = 2
ST_net_learning_rate = 0.04 ST_net_learning_rate = 0.04
ST_name_hash = random.getrandbits(32) ST_name_hash = random.getrandbits(32)

View File

@ -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>. # will send forward the number "1" for batch size with the variable <irrelevant_batch_size>.
irrelevant_batch_size = 1 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, def line_chart_fixpoints(fixpoint_counters_history: list, epochs: int, ST_steps_between_SA: int,