diff --git a/code/bar_plot.py b/code/bar_plot.py new file mode 100644 index 0000000..ebefa73 --- /dev/null +++ b/code/bar_plot.py @@ -0,0 +1,139 @@ +import os + +from experiment import Experiment +# noinspection PyUnresolvedReferences +from soup import Soup +from typing import List + +from collections import defaultdict + +from argparse import ArgumentParser +import numpy as np + +import plotly as pl +import plotly.graph_objs as go + +import colorlover as cl + +import dill + + +def build_args(): + arg_parser = ArgumentParser() + arg_parser.add_argument('-i', '--in_file', nargs=1, type=str) + arg_parser.add_argument('-o', '--out_file', nargs='?', default='out', type=str) + return arg_parser.parse_args() + + +def plot_histogram(bars_dict_list: List[dict], filename='histogram_plot'): + # catagorical + ryb = cl.scales['10']['div']['RdYlBu'] + + data = [] + + if bars_dict_list: + keys = bars_dict_list[0].keys() + keyDict = defaultdict(list) + else: + raise IOError('This List is empty, is this intended?') + + for key in keys: + keyDict[key] = np.mean([bars_dict[key] for bars_dict in bars_dict_list]) + + hist = go.Bar( + y=[keyDict.get(key, 0) for key in keys], + x=[key for key in keys], + showlegend=False, + marker=dict( + color=[ryb[bar_id] for bar_id in range(len(keys))] + ), + ) + data.append(hist) + + layout = dict(title='{} Histogram Plot'.format('Experiment Name Penis'), + # height=400, width=400, + # margin=dict(l=20, r=20, t=20, b=20) + ) + + fig = go.Figure(data=data, layout=layout) + pl.offline.plot(fig, auto_open=True, filename=filename) + pass + + +def line_plot(line_dict_list, filename='lineplot'): + # lines with standard deviation + # Transform data accordingly and plot it + data = [] + rdylgn = cl.scales['10']['div']['RdYlGn'] + rdylgn_background = [scale + (0.4,) for scale in cl.to_numeric(rdylgn)] + for line_id, line_dict in enumerate(line_dict_list): + name = line_dict.get('name', 'gimme a name') + + upper_bound = go.Scatter( + name='Upper Bound', + x=line_dict['x'], + y=line_dict['upper_y'], + mode='lines', + marker=dict(color="#444"), + line=dict(width=0), + fillcolor=rdylgn_background[line_id], + ) + + trace = go.Scatter( + x=line_dict['x'], + y=line_dict['main_y'], + mode='lines', + name=name, + line=dict(color=line_id), + fillcolor=rdylgn_background[line_id], + fill='tonexty') + + lower_bound = go.Scatter( + name='Lower Bound', + x=line_dict['x'], + y=line_dict['lower_y'], + marker=dict(color="#444"), + line=dict(width=0), + mode='lines') + + data.extend([upper_bound, trace, lower_bound]) + + layout=dict(title='{} Line Plot'.format('Experiment Name Penis'), + height=800, width=800, margin=dict(l=0, r=0, t=0, b=0)) + + fig = go.Figure(data=data, layout=layout) + pl.offline.plot(fig, auto_open=True, filename=filename) + pass + + +def search_and_apply(absolut_file_or_folder, plotting_function, files_to_look_for=[]): + if os.path.isdir(absolut_file_or_folder): + for sub_file_or_folder in os.scandir(absolut_file_or_folder): + search_and_apply(sub_file_or_folder.path, plotting_function, files_to_look_for=files_to_look_for) + elif absolut_file_or_folder.endswith('.dill'): + file_or_folder = os.path.split(absolut_file_or_folder)[-1] + if file_or_folder in files_to_look_for and not os.path.exists('{}.html'.format(file_or_folder[:-5])): + print('Apply Plotting function "{func}" on file "{file}"'.format(func=plotting_function.__name__, + file=absolut_file_or_folder) + ) + + with open(absolut_file_or_folder, 'rb') as in_f: + exp = dill.load(in_f) + + plotting_function(exp, filename='{}.html'.format(absolut_file_or_folder[:-5])) + + else: + pass + # This was not a file i should look for. + else: + # This was either another FilyType or Plot.html alerady exists. + pass + + +if __name__ == '__main__': + args = build_args() + in_file = args.in_file[0] + out_file = args.out_file + + search_and_apply(in_file, plot_histogram, files_to_look_for=['all_counters.dill']) + # , 'all_names.dill', 'all_notable_nets.dill']) diff --git a/code/latent_trajectory_plot.html b/code/latent_trajectory_plot.html deleted file mode 100644 index 1433788..0000000 --- a/code/latent_trajectory_plot.html +++ /dev/null @@ -1,7 +0,0 @@ -
\ No newline at end of file diff --git a/code/plot.html b/code/plot.html deleted file mode 100644 index e28ba6b..0000000 --- a/code/plot.html +++ /dev/null @@ -1,7 +0,0 @@ - \ No newline at end of file diff --git a/code/setups/fixpoint-density.py b/code/setups/fixpoint-density.py index abb9bcc..d126efd 100644 --- a/code/setups/fixpoint-density.py +++ b/code/setups/fixpoint-density.py @@ -1,6 +1,7 @@ import sys - -sys.path += ['../', './'] +import os +# Concat top Level dir to system environmental variables +sys.path += os.path.join('..', '.') from util import * from experiment import * @@ -26,7 +27,7 @@ def count(counters, net, notable_nets=[]): return counters, notable_nets with Experiment('fixpoint-density') as exp: - exp.trials = 1000 + exp.trials = 100 exp.epsilon = 1e-4 net_generators = [] for activation in ['linear', 'sigmoid', 'relu']: @@ -43,6 +44,7 @@ with Experiment('fixpoint-density') as exp: net = net_generator().with_params(epsilon=exp.epsilon) name = str(net.__class__.__name__) + " activiation='" + str(net.get_keras_params().get('activation')) + "' use_bias='" + str(net.get_keras_params().get('use_bias')) + "'" count(counters, net, notable_nets) + K.clear_session() all_counters += [counters] all_notable_nets += [notable_nets] all_names += [name] @@ -52,4 +54,6 @@ with Experiment('fixpoint-density') as exp: for exp_id, counter in enumerate(all_counters): exp.log(all_names[exp_id]) exp.log(all_counters[exp_id]) - exp.log('\n') \ No newline at end of file + exp.log('\n') + +print('Done') diff --git a/code/setups/known-fixpoint-variation.py b/code/setups/known-fixpoint-variation.py index e28fa80..ca3b066 100644 --- a/code/setups/known-fixpoint-variation.py +++ b/code/setups/known-fixpoint-variation.py @@ -1,6 +1,10 @@ import sys -sys.path += ['../', './'] +import os + +# Concat top Level dir to system environmental variables +sys.path += os.path.join('..', '.') + from util import * from experiment import * diff --git a/code/setups/mixed-self-fixpoints.py b/code/setups/mixed-self-fixpoints.py index beecc0f..0ee3c0a 100644 --- a/code/setups/mixed-self-fixpoints.py +++ b/code/setups/mixed-self-fixpoints.py @@ -1,6 +1,7 @@ import sys +import os -sys.path += ['../', './'] +from typing import Tuple from util import * from experiment import * @@ -9,10 +10,32 @@ from network import * import keras.backend +# Concat top Level dir to system environmental variables +sys.path += os.path.join('..', '.') + + def generate_counters(): + """ + Initial build of the counter dict, to store counts. + + :rtype: dict + :return: dictionary holding counter for: 'divergent', 'fix_zero', 'fix_sec', 'other' + """ return {'divergent': 0, 'fix_zero': 0, 'fix_other': 0, 'fix_sec': 0, 'other': 0} + def count(counters, net, notable_nets=[]): + """ + Count the occurences ot the types of weight trajectories. + + :param counters: A counter dictionary. + :param net: A Neural Network + :param notable_nets: A list to store and save intersting candidates + + :rtype Tuple[dict, list] + :return: Both the counter dictionary and the list of interessting nets. + """ + if net.is_diverged(): counters['divergent'] += 1 elif net.is_fixpoint(): @@ -28,6 +51,7 @@ def count(counters, net, notable_nets=[]): counters['other'] += 1 return counters, notable_nets + with Experiment('training_fixpoint') as exp: exp.trials = 20 exp.selfattacks = 4 @@ -69,4 +93,4 @@ with Experiment('training_fixpoint') as exp: for exp_id, name in enumerate(all_names): exp.log(all_names[exp_id]) exp.log(all_data[exp_id]) - exp.log('\n') \ No newline at end of file + exp.log('\n') diff --git a/code/setups/training-fixpoints.py b/code/setups/training-fixpoints.py index 9018c8d..8fe3df2 100644 --- a/code/setups/training-fixpoints.py +++ b/code/setups/training-fixpoints.py @@ -1,6 +1,8 @@ import sys +import os -sys.path += ['../', './'] +# Concat top Level dir to system environmental variables +sys.path += os.path.join('..', '.') from util import * from experiment import *