fixed soup_basin experiment
This commit is contained in:
@ -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
|
# 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>
|
# 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(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):
|
def count_fixpoints(self):
|
||||||
exp_details = f"SA steps: {self.SA_steps}; ST steps: {self.ST_steps_between_SA}"
|
exp_details = f"SA steps: {self.SA_steps}; ST steps: {self.ST_steps_between_SA}"
|
||||||
|
@ -88,8 +88,7 @@ class SoupExperiment:
|
|||||||
# Testing for fixpoints after each batch of ST steps to see relevant data
|
# Testing for fixpoints after each batch of ST steps to see relevant data
|
||||||
if i % self.ST_steps == 0:
|
if i % self.ST_steps == 0:
|
||||||
test_for_fixpoints(self.fixpoint_counters, self.population)
|
test_for_fixpoints(self.fixpoint_counters, self.population)
|
||||||
fixpoints_percentage = round((self.fixpoint_counters["fix_zero"] + self.fixpoint_counters["fix_weak"] +
|
fixpoints_percentage = round(self.fixpoint_counters["identity_func"] / self.population_size, 1)
|
||||||
self.fixpoint_counters["fix_sec"]) / self.population_size, 1)
|
|
||||||
self.fixpoint_counters_history.append(fixpoints_percentage)
|
self.fixpoint_counters_history.append(fixpoints_percentage)
|
||||||
|
|
||||||
# Resetting the fixpoint counter. Last iteration not to be reset -
|
# Resetting the fixpoint counter. Last iteration not to be reset -
|
||||||
|
@ -17,6 +17,7 @@ import pandas as pd
|
|||||||
import seaborn as sns
|
import seaborn as sns
|
||||||
from matplotlib import pyplot as plt
|
from matplotlib import pyplot as plt
|
||||||
|
|
||||||
|
|
||||||
def prng():
|
def prng():
|
||||||
return random.random()
|
return random.random()
|
||||||
|
|
||||||
@ -71,7 +72,8 @@ def distance_from_parent(nets, distance="MIM", print_it=True):
|
|||||||
elif distance in ["MAE"]:
|
elif distance in ["MAE"]:
|
||||||
matrix[idx][dist] = MAE(parent_weights, clone_weights) < pow(10, -dist)
|
matrix[idx][dist] = MAE(parent_weights, clone_weights) < pow(10, -dist)
|
||||||
elif distance in ["MIM"]:
|
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:
|
if print_it:
|
||||||
print(f"\nDistances from parent {parent.name} [{distance}]:")
|
print(f"\nDistances from parent {parent.name} [{distance}]:")
|
||||||
@ -83,6 +85,7 @@ def distance_from_parent(nets, distance="MIM", print_it=True):
|
|||||||
|
|
||||||
return list_of_matrices
|
return list_of_matrices
|
||||||
|
|
||||||
|
|
||||||
class SpawnExperiment:
|
class SpawnExperiment:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@ -144,7 +147,9 @@ class SpawnExperiment:
|
|||||||
def spawn_and_continue(self, number_clones: int = None):
|
def spawn_and_continue(self, number_clones: int = None):
|
||||||
number_clones = number_clones or self.nr_clones
|
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 every initial net {i} after populating (that is fixpoint after first epoch);
|
||||||
for i in range(self.population_size):
|
for i in range(self.population_size):
|
||||||
@ -198,7 +203,8 @@ class SpawnExperiment:
|
|||||||
f"\nMIM({i},{j}): {MIM_post}\n")
|
f"\nMIM({i},{j}): {MIM_post}\n")
|
||||||
self.nets.append(clone)
|
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.
|
# 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):
|
||||||
@ -222,11 +228,11 @@ class SpawnExperiment:
|
|||||||
self.loss_history.append(net_loss_history)
|
self.loss_history.append(net_loss_history)
|
||||||
plot_loss(self.loss_history, self.directory)
|
plot_loss(self.loss_history, self.directory)
|
||||||
|
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
pickle.dump(self, open(f"{self.directory}/experiment_pickle.p", "wb"))
|
pickle.dump(self, open(f"{self.directory}/experiment_pickle.p", "wb"))
|
||||||
print(f"\nSaved experiment to {self.directory}.")
|
print(f"\nSaved experiment to {self.directory}.")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
NET_INPUT_SIZE = 4
|
NET_INPUT_SIZE = 4
|
||||||
|
@ -124,11 +124,13 @@ class SoupSpawnExperiment:
|
|||||||
|
|
||||||
# Populating environment & evolving entities
|
# Populating environment & evolving entities
|
||||||
self.nets = []
|
self.nets = []
|
||||||
|
self.id_functions = []
|
||||||
|
self.clone_soup = []
|
||||||
self.populate_environment()
|
self.populate_environment()
|
||||||
self.evolve()
|
|
||||||
|
|
||||||
self.spawn_and_continue()
|
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.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)
|
||||||
@ -140,27 +142,35 @@ class SoupSpawnExperiment:
|
|||||||
for i in loop_population_size:
|
for i in loop_population_size:
|
||||||
loop_population_size.set_description("Populating experiment %s" % i)
|
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)
|
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)
|
self.nets.append(net)
|
||||||
|
|
||||||
def evolve(self):
|
if is_identity_function(net):
|
||||||
loop_epochs = tqdm(range(self.epochs))
|
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:
|
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
|
# A network attacking another network with a given percentage
|
||||||
if random.randint(1, 100) <= self.attack_chance:
|
if random.randint(1, 100) <= self.attack_chance:
|
||||||
random_net1, random_net2 = random.sample(range(self.population_size), 2)
|
random_net1, random_net2 = random.sample(range(len(population)), 2)
|
||||||
random_net1 = self.nets[random_net1]
|
random_net1 = population[random_net1]
|
||||||
random_net2 = self.nets[random_net2]
|
random_net2 = population[random_net2]
|
||||||
print(f"\n Attack: {random_net1.name} -> {random_net2.name}")
|
print(f"\n Attack: {random_net1.name} -> {random_net2.name}")
|
||||||
random_net1.attack(random_net2)
|
random_net1.attack(random_net2)
|
||||||
|
|
||||||
# Self-training each network in the population
|
# Self-training each network in the population
|
||||||
for j in range(self.population_size):
|
for j in range(len(population)):
|
||||||
net = self.nets[j]
|
net = population[j]
|
||||||
|
|
||||||
for _ in range(self.ST_steps):
|
for _ in range(self.ST_steps):
|
||||||
net.self_train(1, self.log_step_size, self.net_learning_rate)
|
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',
|
columns=['parent', 'MAE_pre', 'MAE_post', 'MSE_pre', 'MSE_post', 'MIM_pre', 'MIM_post', 'noise',
|
||||||
'status_post'])
|
'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 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]
|
net = self.nets[i]
|
||||||
# We set parent start_time to just before this epoch ended, so plotting is zoomed in. Comment out to
|
# 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).
|
# to see full trajectory (but the clones will be very hard to see).
|
||||||
@ -182,7 +194,6 @@ class SoupSpawnExperiment:
|
|||||||
net_input_data = net.input_weight_matrix()
|
net_input_data = net.input_weight_matrix()
|
||||||
net_target_data = net.create_target_weights(net_input_data)
|
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;
|
# Clone the fixpoint x times and add (+-)self.noise to weight-sets randomly;
|
||||||
@ -191,7 +202,7 @@ class SoupSpawnExperiment:
|
|||||||
# parent-net's weight history as well.
|
# parent-net's weight history as well.
|
||||||
for j in range(number_clones):
|
for j in range(number_clones):
|
||||||
clone = Net(net.input_size, net.hidden_size, net.out_size,
|
clone = Net(net.input_size, net.hidden_size, net.out_size,
|
||||||
f"ST_net_{str(i)}_clone_{str(j)}", start_time=self.ST_steps)
|
f"net_{str(i)}_clone_{str(j)}", start_time=self.ST_steps)
|
||||||
clone.load_state_dict(copy.deepcopy(net.state_dict()))
|
clone.load_state_dict(copy.deepcopy(net.state_dict()))
|
||||||
rand_noise = prng() * self.noise
|
rand_noise = prng() * self.noise
|
||||||
clone = self.apply_noise(clone, rand_noise)
|
clone = self.apply_noise(clone, rand_noise)
|
||||||
@ -204,10 +215,18 @@ class SoupSpawnExperiment:
|
|||||||
MSE_pre = MSE(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)
|
MIM_pre = mean_invariate_manhattan_distance(net_target_data, clone_pre_weights)
|
||||||
|
|
||||||
# Then finish training each clone {j} (for remaining epoch-1 * ST_steps) ..
|
net.children.append(clone)
|
||||||
for _ in range(self.epochs - 1):
|
self.clone_soup.append(clone)
|
||||||
for _ in range(self.ST_steps):
|
|
||||||
clone.self_train(1, self.log_step_size, self.net_learning_rate)
|
self.evolve(self.clone_soup)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
for j in range(len(net.children)):
|
||||||
|
clone = net.children[j]
|
||||||
|
|
||||||
# Post Training distances for comparison
|
# Post Training distances for comparison
|
||||||
clone_post_weights = clone.create_target_weights(clone.input_weight_matrix())
|
clone_post_weights = clone.create_target_weights(clone.input_weight_matrix())
|
||||||
@ -239,9 +258,9 @@ class SoupSpawnExperiment:
|
|||||||
|
|
||||||
self.df = df
|
self.df = df
|
||||||
|
|
||||||
def weights_evolution_3d_experiment(self):
|
def weights_evolution_3d_experiment(self, nets_population, suffix):
|
||||||
exp_name = f"soup_basins_{str(len(self.nets))}_nets_3d_weights_PCA"
|
exp_name = f"soup_basins_{str(len(self.nets))}_nets_3d_weights_PCA_{suffix}"
|
||||||
return plot_3d_soup(self.nets, exp_name, self.directory)
|
return plot_3d_soup(nets_population, exp_name, self.directory)
|
||||||
|
|
||||||
def visualize_loss(self):
|
def visualize_loss(self):
|
||||||
for i in range(len(self.nets)):
|
for i in range(len(self.nets)):
|
||||||
@ -262,12 +281,12 @@ if __name__ == "__main__":
|
|||||||
# Define number of runs & name:
|
# Define number of runs & name:
|
||||||
ST_runs = 1
|
ST_runs = 1
|
||||||
ST_runs_name = "test-27"
|
ST_runs_name = "test-27"
|
||||||
soup_ST_steps = 2500
|
soup_ST_steps = 1500
|
||||||
soup_epochs = 2
|
soup_epochs = 2
|
||||||
soup_log_step_size = 10
|
soup_log_step_size = 10
|
||||||
|
|
||||||
# Define number of networks & their architecture
|
# Define number of networks & their architecture
|
||||||
nr_clones = 15
|
nr_clones = 2
|
||||||
soup_population_size = 2
|
soup_population_size = 2
|
||||||
soup_net_hidden_size = 2
|
soup_net_hidden_size = 2
|
||||||
soup_net_learning_rate = 0.04
|
soup_net_learning_rate = 0.04
|
||||||
|
@ -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:
|
def __init__(self, i_size: int, h_size: int, o_size: int, name=None, start_time=1) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.start_time = start_time
|
self.start_time = start_time
|
||||||
|
|
||||||
self.name = name
|
self.name = name
|
||||||
|
self.children = []
|
||||||
|
|
||||||
self.input_size = i_size
|
self.input_size = i_size
|
||||||
self.hidden_size = h_size
|
self.hidden_size = h_size
|
||||||
self.out_size = o_size
|
self.out_size = o_size
|
||||||
|
@ -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,
|
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) """
|
""" Plotting the the weights of the nets in a 3d form using principal component analysis (PCA) """
|
||||||
|
|
||||||
fig = plt.figure()
|
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)
|
zdata = np.arange(start_time, len(ydata)*batch_size+start_time, batch_size)
|
||||||
|
|
||||||
ax.plot3D(xdata, ydata, zdata, label=f"net {i}")
|
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")
|
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")
|
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:
|
else:
|
||||||
plt.savefig(str(filepath))
|
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):
|
def plot_3d_self_train(nets_array: List, exp_name: str, directory: Union[str, Path], batch_size: int, plot_pca_together: bool):
|
||||||
@ -182,7 +185,7 @@ def plot_3d_self_train(nets_array: List, exp_name: str, directory: Union[str, Pa
|
|||||||
z_axis_legend = "epochs"
|
z_axis_legend = "epochs"
|
||||||
|
|
||||||
return plot_3d(matrices_weights_history, directory, len(nets_array), z_axis_legend, exp_name, "", batch_size,
|
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:
|
def plot_3d_self_application(nets_array: List, exp_name: str, directory_name: Union[str, Path], batch_size: int) -> None:
|
||||||
|
Reference in New Issue
Block a user