Viz updates
This commit is contained in:
@ -18,13 +18,12 @@ class Experiment:
|
||||
self.base_dir = self.experiment_name
|
||||
self.next_iteration = 0
|
||||
self.log_messages = []
|
||||
self.historical_particles = dict()
|
||||
self.historical_particles = {}
|
||||
|
||||
def __enter__(self):
|
||||
self.dir = os.path.join(self.base_dir, 'experiments', 'exp-{name}-{id}-{it}'.format(
|
||||
self.dir = os.path.join('experiments', 'exp-{name}-{id}-{it}'.format(
|
||||
name=self.experiment_name, id=self.experiment_id, it=self.next_iteration)
|
||||
)
|
||||
|
||||
os.makedirs(self.dir)
|
||||
print("** created {dir} **".format(dir=self.dir))
|
||||
return self
|
||||
@ -60,10 +59,6 @@ class Experiment:
|
||||
with open(os.path.join(self.dir, "{name}.dill".format(name=name)), "wb") as dill_file:
|
||||
dill.dump(value, dill_file)
|
||||
|
||||
def add_trajectory_segment(self, run_id, trajectory):
|
||||
self.historical_particles[run_id].append(trajectory)
|
||||
return
|
||||
|
||||
|
||||
class FixpointExperiment(Experiment):
|
||||
|
||||
@ -78,8 +73,7 @@ class FixpointExperiment(Experiment):
|
||||
net.self_attack()
|
||||
i += 1
|
||||
if run_id:
|
||||
weights = net.get_weights_flat()
|
||||
self.add_trajectory_segment(run_id, weights)
|
||||
net.save_state(time=run_id)
|
||||
self.count(net)
|
||||
|
||||
def count(self, net):
|
||||
|
@ -1,5 +1,3 @@
|
||||
import math
|
||||
import copy
|
||||
import numpy as np
|
||||
|
||||
from keras.models import Sequential
|
||||
@ -47,9 +45,9 @@ class NeuralNetwork(PrintingObject):
|
||||
for layer_id, layer in enumerate(network_weights):
|
||||
for cell_id, cell in enumerate(layer):
|
||||
for weight_id, weight in enumerate(cell):
|
||||
if math.isnan(weight):
|
||||
if np.isnan(weight):
|
||||
return True
|
||||
if math.isinf(weight):
|
||||
if np.isinf(weight):
|
||||
return True
|
||||
return False
|
||||
|
||||
@ -100,13 +98,13 @@ class NeuralNetwork(PrintingObject):
|
||||
return self
|
||||
|
||||
def get_weights(self):
|
||||
return self.get_model().get_weights()
|
||||
return self.model.get_weights()
|
||||
|
||||
def get_weights_flat(self):
|
||||
return np.hstack([weight.flatten() for weight in self.get_weights()])
|
||||
|
||||
def set_weights(self, new_weights):
|
||||
return self.get_model().set_weights(new_weights)
|
||||
return self.model.set_weights(new_weights)
|
||||
|
||||
def apply_to_weights(self, old_weights):
|
||||
raise NotImplementedError
|
||||
@ -133,11 +131,11 @@ class NeuralNetwork(PrintingObject):
|
||||
return self.attack(new_other_network)
|
||||
|
||||
def is_diverged(self):
|
||||
return NeuralNetwork.are_weights_diverged(self.get_weights())
|
||||
return self.are_weights_diverged(self.get_weights())
|
||||
|
||||
def is_zero(self, epsilon=None):
|
||||
epsilon = epsilon or self.get_params().get('epsilon')
|
||||
return NeuralNetwork.are_weights_within(self.get_weights(), -epsilon, epsilon)
|
||||
return self.are_weights_within(self.get_weights(), -epsilon, epsilon)
|
||||
|
||||
def is_fixpoint(self, degree=1, epsilon=None):
|
||||
assert degree >= 1, "degree must be >= 1"
|
||||
@ -167,7 +165,7 @@ class NeuralNetwork(PrintingObject):
|
||||
def make_state(self, **kwargs):
|
||||
weights = self.get_weights_flat()
|
||||
state = {'class': self.__class__.__name__, 'weights': weights}
|
||||
if any(np.isinf(weights)):
|
||||
if any(np.isnan(weights)) or any(np.isinf(weights)):
|
||||
return None
|
||||
state.update(kwargs)
|
||||
return state
|
||||
@ -593,7 +591,8 @@ class LearningNeuralNetwork(NeuralNetwork):
|
||||
for epoch in range(epochs):
|
||||
old_weights = self.get_weights()
|
||||
x = reduction(old_weights, self.features)
|
||||
history = self.model.fit(x=x, y=x, verbose=0, batch_size=batchsize)
|
||||
savestateCallback = SaveStateCallback(self, epoch=epoch)
|
||||
history = self.model.fit(x=x, y=x, verbose=0, batch_size=batchsize, callbacks=savestateCallback)
|
||||
bar.postfix[1]["value"] = history.history['loss'][-1]
|
||||
bar.update()
|
||||
|
||||
@ -654,18 +653,20 @@ if __name__ == '__main__':
|
||||
if False:
|
||||
with FixpointExperiment() as exp:
|
||||
for run_id in tqdm(range(100)):
|
||||
net = WeightwiseNeuralNetwork(width=2, depth=2).with_keras_params(activation='linear')
|
||||
# net = WeightwiseNeuralNetwork(width=2, depth=2).with_keras_params(activation='linear')
|
||||
# net = AggregatingNeuralNetwork(aggregates=4, width=2, depth=2)\
|
||||
# net = FFTNeuralNetwork(aggregates=4, width=2, depth=2) \
|
||||
# .with_params(print_all_weight_updates=False, use_bias=False)
|
||||
net = FFTNeuralNetwork(aggregates=4, width=2, depth=2) \
|
||||
.with_params(print_all_weight_updates=False, use_bias=False)
|
||||
# net = RecurrentNeuralNetwork(width=2, depth=2).with_keras_params(activation='linear')\
|
||||
# .with_params(print_all_weight_updates=True)
|
||||
# net.print_weights()
|
||||
|
||||
# INFO Run_ID needs to be more than 0, so that exp stores the trajectories!
|
||||
exp.run_net(net, 100, run_id=run_id+1)
|
||||
exp.historical_particles[run_id] = net
|
||||
exp.log(exp.counters)
|
||||
if False:
|
||||
# TODO SI: Ich muss noch apply to weights implementieren
|
||||
# is_fixpoint was wrong because it trivially returned the old weights
|
||||
with IdentLearningExperiment() as exp:
|
||||
net = LearningNeuralNetwork(width=2, depth=2, features=2, )\
|
||||
@ -674,17 +675,13 @@ if __name__ == '__main__':
|
||||
net.print_weights()
|
||||
time.sleep(0.1)
|
||||
print(net.is_fixpoint(epsilon=0.1e-6))
|
||||
print()
|
||||
net.learn(1, reduction=LearningNeuralNetwork.fft_reduction)
|
||||
import time
|
||||
time.sleep(0.1)
|
||||
net.print_weights()
|
||||
time.sleep(0.1)
|
||||
print(net.is_fixpoint(epsilon=0.1e-6))
|
||||
if True:
|
||||
|
||||
if False:
|
||||
# ok so this works quite realiably
|
||||
with FixpointExperiment() as exp:
|
||||
for i in range(10):
|
||||
for i in range(1):
|
||||
|
||||
run_count = 1000
|
||||
net = TrainingNeuralNetworkDecorator(WeightwiseNeuralNetwork(width=2, depth=2))\
|
||||
|
@ -59,7 +59,7 @@ class Soup:
|
||||
particle.attack(other_particle)
|
||||
description['action'] = 'attacking'
|
||||
description['counterpart'] = other_particle.get_uid()
|
||||
if prng() < self.params.get('train_other_rate'):
|
||||
if prng() < self.params.get('train_other_rate') and hasattr(self, 'train_other'):
|
||||
other_particle_id = int(prng() * len(self.particles))
|
||||
other_particle = self.particles[other_particle_id]
|
||||
particle.train_other(other_particle)
|
||||
@ -150,7 +150,7 @@ class ParticleDecorator:
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if False:
|
||||
if True:
|
||||
with SoupExperiment() as exp:
|
||||
for run_id in range(1):
|
||||
net_generator = lambda: WeightwiseNeuralNetwork(2, 2).with_keras_params(activation='linear').with_params()
|
||||
@ -159,11 +159,11 @@ if __name__ == '__main__':
|
||||
# net_generator = lambda: RecurrentNeuralNetwork(2, 2).with_keras_params(activation='linear').with_params()
|
||||
soup = Soup(100, net_generator).with_params(remove_divergent=True, remove_zero=True)
|
||||
soup.seed()
|
||||
for _ in tqdm(range(100)):
|
||||
for _ in tqdm(range(1000)):
|
||||
soup.evolve()
|
||||
exp.log(soup.count())
|
||||
|
||||
if True:
|
||||
if False:
|
||||
with SoupExperiment("soup") as exp:
|
||||
for run_id in range(1):
|
||||
net_generator = lambda: TrainingNeuralNetworkDecorator(WeightwiseNeuralNetwork(2, 2)).with_keras_params(
|
||||
|
@ -14,7 +14,7 @@ import colorlover as cl
|
||||
|
||||
import dill
|
||||
|
||||
from sklearn.manifold.t_sne import TSNE
|
||||
from sklearn.manifold.t_sne import TSNE, PCA
|
||||
|
||||
|
||||
def build_args():
|
||||
@ -31,8 +31,8 @@ def build_from_soup_or_exp(soup):
|
||||
particle_dict = dict(
|
||||
trajectory=[event['weights'] for event in particle],
|
||||
time=[event['time'] for event in particle],
|
||||
action=[event['action'] for event in particle],
|
||||
counterpart=[event['counterpart'] for event in particle]
|
||||
action=[event.get('action', None) for event in particle],
|
||||
counterpart=[event.get('counterpart', None) for event in particle]
|
||||
)
|
||||
particle_list.append(particle_dict)
|
||||
return particle_list
|
||||
@ -101,15 +101,18 @@ def plot_latent_trajectories_3D(soup_or_experiment, filename='plot'):
|
||||
scale = cl.interp(bupu, len(data_list)+1) # Map color scale to N bins
|
||||
|
||||
# Fit the embedding space
|
||||
transformer = TSNE()
|
||||
transformer = PCA(n_components=2)
|
||||
|
||||
array = []
|
||||
for particle_dict in data_list:
|
||||
array = np.asarray(particle_dict['trajectory'])
|
||||
transformer.fit(array)
|
||||
array.append(particle_dict['trajectory'])
|
||||
|
||||
transformer.fit(np.vstack(array))
|
||||
|
||||
# Transform data accordingly and plot it
|
||||
data = []
|
||||
for p_id, particle_dict in enumerate(data_list):
|
||||
transformed = transformer._fit(particle_dict['trajectory'])
|
||||
transformed = transformer.transform(particle_dict['trajectory'])
|
||||
line_trace = go.Scatter3d(
|
||||
x=transformed[:, 0],
|
||||
y=transformed[:, 1],
|
||||
|
Reference in New Issue
Block a user