journal_basins.py debugged II

Questions for functionalities_test.py
corrected some fixes
Redo and implementation of everything path related now using pathlib.Path
This commit is contained in:
steffen-illium
2021-05-16 13:35:38 +02:00
parent 042188f15a
commit b1472479cb
4 changed files with 54 additions and 53 deletions

View File

@ -1,5 +1,6 @@
""" ----------------------------------------- Methods for summarizing the experiments ------------------------------------------ """
import os
from pathlib import Path
from visualization import line_chart_fixpoints, bar_chart_fixpoints
@ -52,8 +53,6 @@ def summary_fixpoint_percentage(runs, epochs, fixpoints_percentages, ST_steps, S
""" --------------------------------------------------- Miscellaneous ---------------------------------------------------------- """
def check_folder(experiment_folder: str):
if not os.path.isdir("experiments"): os.mkdir(f"experiments/")
if not os.path.isdir(f"experiments/{experiment_folder}/"): os.mkdir(f"experiments/{experiment_folder}/")
exp_path = Path('experiments') / experiment_folder
exp_path.mkdir(parents=True, exist_ok=True)

View File

@ -37,6 +37,8 @@ def is_identity_function(network: Net, epsilon=pow(10, -5)) -> bool:
def is_zero_fixpoint(network: Net, input_data: Tensor, epsilon=pow(10, -5)) -> bool:
# FIXME: Is the the correct test?
raise NotImplementedError
result = overall_fixpoint_test(network, epsilon, input_data)
return result
@ -50,6 +52,7 @@ def is_secondary_fixpoint(network: Net, input_data: Tensor, epsilon: float) -> b
first_output = network(input_data)
# Getting the second output by initializing a new net with the weights of the original net.
# FixMe: Is this correct? I Think it should be the same function thus the same network
net_copy = copy.deepcopy(network)
net_copy.apply_weights(first_output)
input_data_2 = net_copy.input_weight_matrix()
@ -57,18 +60,18 @@ def is_secondary_fixpoint(network: Net, input_data: Tensor, epsilon: float) -> b
# Calculating second output
second_output = network(input_data_2)
check_smaller_epsilon = all(epsilon > second_output)
check_greater_epsilon = all(-epsilon < second_output)
# Perform the Check:
check_abs_within_epsilon = all(epsilon > abs(input_data - second_output))
if check_smaller_epsilon and check_greater_epsilon:
return True
else:
return False
# FIXME: This is wrong, is it?
# check_smaller_epsilon = all(epsilon > second_output)
# check_greater_epsilon = all(-epsilon < second_output)
return True if check_abs_within_epsilon else False
def is_weak_fixpoint(network: Net, input_data: Tensor, epsilon: float) -> bool:
result = overall_fixpoint_test(network, epsilon, input_data)
return result
@ -80,7 +83,6 @@ def test_for_fixpoints(fixpoint_counter: Dict, nets: List, id_functions=None):
for i in range(len(nets)):
net = nets[i]
input_data = net.input_weight_matrix()
target_data = net.create_target_weights(input_data)
if is_divergent(nets[i]):
fixpoint_counter["divergent"] += 1
@ -104,5 +106,6 @@ def test_for_fixpoints(fixpoint_counter: Dict, nets: List, id_functions=None):
return id_functions
def changing_rate(x_new, x_old):
return x_new - x_old

View File

@ -67,7 +67,7 @@ class SpawnExperiment:
return network
def __init__(self, population_size, log_step_size, net_input_size, net_hidden_size, net_out_size, net_learning_rate,
epochs, st_steps, noise, directory_name) -> None:
epochs, st_steps, noise, directory) -> None:
self.population_size = population_size
self.log_step_size = log_step_size
self.net_input_size = net_input_size
@ -81,7 +81,7 @@ class SpawnExperiment:
self.noise = noise or 10e-5
print("\nNOISE:", self.noise)
self.directory = Path(directory_name)
self.directory = Path(directory)
self.directory.mkdir(parents=True, exist_ok=True)
self.populate_environment()
@ -150,13 +150,13 @@ class SpawnExperiment:
def weights_evolution_3d_experiment(self):
exp_name = f"ST_{str(len(self.nets))}_nets_3d_weights_PCA"
return plot_3d_self_train(self.nets, exp_name, self.directory.name, self.log_step_size)
return plot_3d_self_train(self.nets, exp_name, self.directory, self.log_step_size)
def visualize_loss(self):
for i in range(len(self.nets)):
net_loss_history = self.nets[i].loss_history
self.loss_history.append(net_loss_history)
plot_loss(self.loss_history, self.directory.name)
plot_loss(self.loss_history, self.directory)
if __name__ == "__main__":
@ -189,5 +189,5 @@ if __name__ == "__main__":
epochs=ST_epochs,
st_steps=ST_steps,
noise=pow(10, -noise_factor),
directory_name=f"./experiments/spawn_basin/{ST_name_hash}_10e-{noise_factor}"
directory=Path('output') / 'spawn_basin' / f'{ST_name_hash}_10e-{noise_factor}'
)

View File

@ -7,7 +7,6 @@ import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import numpy as np
from sklearn.decomposition import PCA
import os.path
import random
import string
@ -20,7 +19,7 @@ def plot_output(output):
plt.show()
def plot_loss(loss_array, directory_name, batch_size=1):
def plot_loss(loss_array, directory, batch_size=1):
""" Plotting the evolution of the loss function."""
fig = plt.figure()
@ -34,15 +33,15 @@ def plot_loss(loss_array, directory_name, batch_size=1):
plt.xlabel("Epochs")
plt.ylabel("Loss")
filepath = f"./{directory_name}"
filename = f"{filepath}/_nets_loss_function.png"
plt.savefig(f"{filename}")
directory = Path(directory)
filename = "nets_loss_function.png"
file_path = directory / filename
plt.savefig(str(file_path))
# plt.show()
plt.clf()
def bar_chart_fixpoints(fixpoint_counter: Dict, population_size: int, directory_name: String, learning_rate: float,
def bar_chart_fixpoints(fixpoint_counter: Dict, population_size: int, directory: String, learning_rate: float,
exp_details: String, source_check=None):
""" Plotting the number of fixpoints in a barchart. """
@ -66,15 +65,15 @@ def bar_chart_fixpoints(fixpoint_counter: Dict, population_size: int, directory_
plt.bar(range(len(fixpoint_counter)), list(fixpoint_counter.values()), align='center')
plt.xticks(range(len(fixpoint_counter)), list(fixpoint_counter.keys()))
filepath = f"./{directory_name}"
filename = f"{filepath}/{str(population_size)}_nets_fixpoints_barchart.png"
plt.savefig(f"{filename}")
directory = Path(directory)
filename = f"{str(population_size)}_nets_fixpoints_barchart.png"
filepath = directory / filename
plt.savefig(str(filepath))
plt.clf()
# plt.show()
def plot_3d(matrices_weights_history, folder_name, population_size, z_axis_legend, exp_name="experiment", is_trained="",
def plot_3d(matrices_weights_history, directory, population_size, z_axis_legend, exp_name="experiment", is_trained="",
batch_size=1):
""" Plotting the the weights of the nets in a 3d form using principal component analysis (PCA) """
@ -121,10 +120,10 @@ def plot_3d(matrices_weights_history, folder_name, population_size, z_axis_legen
ax.set_zlabel(f"Epochs")
# FIXME: Replace this kind of operation with pathlib.Path() object interactions
folder = Path(folder_name)
folder.mkdir(parents=True, exist_ok=True)
directory = Path(directory)
directory.mkdir(parents=True, exist_ok=True)
filename = f"{exp_name}{is_trained}.png"
filepath = folder / filename
filepath = directory / filename
if filepath.exists():
letters = string.ascii_lowercase
random_letters = ''.join(random.choice(letters) for _ in range(5))
@ -133,10 +132,9 @@ def plot_3d(matrices_weights_history, folder_name, population_size, z_axis_legen
plt.savefig(str(filepath))
plt.show()
#plt.clf()
def plot_3d_self_train(nets_array: List, exp_name: String, directory_name: String, batch_size: int):
def plot_3d_self_train(nets_array: List, exp_name: String, directory: String, batch_size: int):
""" Plotting the evolution of the weights in a 3D space when doing self training. """
matrices_weights_history = []
@ -149,7 +147,7 @@ def plot_3d_self_train(nets_array: List, exp_name: String, directory_name: Strin
z_axis_legend = "epochs"
return plot_3d(matrices_weights_history, directory_name, 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)
def plot_3d_self_application(nets_array: List, exp_name: String, directory_name: String, batch_size: int) -> None:
@ -168,23 +166,23 @@ def plot_3d_self_application(nets_array: List, exp_name: String, directory_name:
else:
is_trained = "_not_trained"
# Fixme: Are the both following lines on the correct intendation? -> Value of "is_trained" changes multiple times!
z_axis_legend = "epochs"
plot_3d(matrices_weights_history, directory_name, len(nets_array), z_axis_legend, exp_name, is_trained, batch_size)
def plot_3d_soup(nets_list, exp_name, directory_name):
def plot_3d_soup(nets_list, exp_name, directory):
""" Plotting the evolution of the weights in a 3D space for the soup environment. """
# This batch size is not relevant for soups. 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>.
irrelevant_batch_size = 1
plot_3d_self_train(nets_list, exp_name, directory_name, irrelevant_batch_size)
plot_3d_self_train(nets_list, exp_name, directory, irrelevant_batch_size)
def line_chart_fixpoints(fixpoint_counters_history: list, epochs: int, ST_steps_between_SA: int,
SA_steps, directory_name: String, population_size: int):
SA_steps, directory: String, population_size: int):
""" Plotting the percentage of fixpoints after each iteration of SA & ST steps. """
fig = plt.figure()
@ -205,15 +203,15 @@ def line_chart_fixpoints(fixpoint_counters_history: list, epochs: int, ST_steps_
plt.plot(ST_steps_per_SA, fixpoint_counters_history, color="green", marker="o")
filepath = f"./{directory_name}"
filename = f"{filepath}/{str(population_size)}_nets_fixpoints_linechart.png"
plt.savefig(f"{filename}")
directory = Path(directory)
filename = f"{str(population_size)}_nets_fixpoints_linechart.png"
filepath = directory / filename
plt.savefig(str(filepath))
plt.clf()
# plt.show()
def box_plot(data, directory_name, population_size):
def box_plot(data, directory, population_size):
fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(10, 7))
# ax = fig.add_axes([0, 0, 1, 1])
@ -226,16 +224,17 @@ def box_plot(data, directory_name, population_size):
axs[1].boxplot(data)
axs[1].set_title('Box plot')
filepath = f"./{directory_name}"
filename = f"{filepath}/{str(population_size)}_nets_fixpoints_barchart.png"
plt.savefig(f"{filename}")
directory = Path(directory)
filename = f"{str(population_size)}_nets_fixpoints_barchart.png"
filepath = directory / filename
# plt.show()
plt.savefig(str(filepath))
plt.clf()
def write_file(text, directory_name):
filepath = f"./{directory_name}"
f = open(f"{filepath}/experiment.txt", "w+")
f.write(text)
f.close()
def write_file(text, directory):
directory = Path(directory)
filepath = directory / 'experiment.txt'
with filepath.open('w+') as f:
f.write(text)
f.close()