From 025d9cc337d5028d2dd462a5197d33b6d3ba040b Mon Sep 17 00:00:00 2001 From: Thomas Gabor <> Date: Sun, 3 Mar 2019 18:37:46 +0100 Subject: [PATCH] first take on making soup --- code/experiment.py | 6 ++++- code/network.py | 25 ++++++++++++------- code/soup.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 code/soup.py diff --git a/code/experiment.py b/code/experiment.py index 056174e..4fe6cc9 100644 --- a/code/experiment.py +++ b/code/experiment.py @@ -75,4 +75,8 @@ class FixpointExperiment(Experiment): elif net.is_fixpoint(2): self.counters['fix_sec'] += 1 else: - self.counters['other'] += 1 \ No newline at end of file + self.counters['other'] += 1 + + +class SoupExperiment(Experiment): + pass diff --git a/code/network.py b/code/network.py index e9d3a33..911c8de 100644 --- a/code/network.py +++ b/code/network.py @@ -92,6 +92,14 @@ class NeuralNetwork: self.attack(self) return self + def meet(self, other_network): + new_other_network = copy.deepcopy(other_network) + return self.attack(new_other_network) + + def self_meet(self, iterations=1): + new_me = copy.deepcopy(self) + return new_me.self_attack(iterations) + def is_diverged(self): return are_weights_diverged(self.get_weights()) @@ -300,13 +308,12 @@ class RecurrentNeuralNetwork(NeuralNetwork): if __name__ == '__main__': - if True: - with FixpointExperiment() as exp: - for run_id in tqdm(range(100)): - # net = WeightwiseNeuralNetwork(2, 2).with_keras_params(activation='linear') - net = AggregatingNeuralNetwork(4, 2, 2).with_keras_params(activation='linear').with_params(shuffler=AggregatingNeuralNetwork.shuffle_random, print_all_weight_updates=False) - # net = RecurrentNeuralNetwork(2, 2).with_keras_params(activation='linear').with_params(print_all_weight_updates=True) - # net.print_weights() - exp.run_net(net, 100) - exp.log(exp.counters) + with FixpointExperiment() as exp: + for run_id in tqdm(range(100)): + # net = WeightwiseNeuralNetwork(width=2, depth=2).with_keras_params(activation='linear') + net = AggregatingNeuralNetwork(aggregates=4, width=2, depth=2).with_keras_params(activation='linear').with_params(shuffler=AggregatingNeuralNetwork.shuffle_random, print_all_weight_updates=False) + # net = RecurrentNeuralNetwork(width=2, depth=2).with_keras_params(activation='linear').with_params(print_all_weight_updates=True) + # net.print_weights() + exp.run_net(net, 100) + exp.log(exp.counters) diff --git a/code/soup.py b/code/soup.py new file mode 100644 index 0000000..dc2e524 --- /dev/null +++ b/code/soup.py @@ -0,0 +1,61 @@ +import random +import copy + +from experiment import * +from network import * + +def prng(): + return random.random() + +class Soup: + + def __init__(self, size, generator, **kwargs): + self.size = size + self.generator = generator + self.particles = [] + self.params = dict(meeting_rate=0.1) + self.params.update(kwargs) + + def with_params(self, **kwargs): + self.params.update(kwargs) + return self + + def seed(self): + self.particles = [] + for _ in range(self.size): + self.particles += [self.generator()] + return self + + def evolve(self, iterations=1): + for _ in range(iterations): + for particle_id,particle in enumerate(self.particles): + if prng() < self.params.get('meeting_rate'): + other_particle_id = int(prng() * len(self.particles)) + other_particle = self.particles[other_particle_id] + particle.attack(other_particle) + + def count(self): + counters = dict(divergent=0, fix_zero=0, fix_other=0, fix_sec=0, other=0) + for particle in self.particles: + if particle.is_diverged(): + counters['divergent'] += 1 + elif particle.is_fixpoint(): + if particle.is_zero(): + counters['fix_zero'] += 1 + else: + counters['fix_other'] += 1 + elif particle.is_fixpoint(2): + counters['fix_sec'] += 1 + else: + counters['other'] += 1 + return counters + + +if __name__ == '__main__': + with SoupExperiment() as exp: + for run_id in tqdm(range(1)): + net_generator = lambda: AggregatingNeuralNetwork(4, 2, 2).with_keras_params(activation='linear').with_params(shuffler=AggregatingNeuralNetwork.shuffle_random) + soup = Soup(100, net_generator) + soup.seed() + soup.evolve(100) + exp.log(soup.count()) \ No newline at end of file