first take on making soup

This commit is contained in:
Thomas Gabor
2019-03-03 18:37:46 +01:00
parent dcfaa5ed4c
commit 025d9cc337
3 changed files with 82 additions and 10 deletions

View File

@ -76,3 +76,7 @@ class FixpointExperiment(Experiment):
self.counters['fix_sec'] += 1
else:
self.counters['other'] += 1
class SoupExperiment(Experiment):
pass

View File

@ -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,12 +308,11 @@ 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 = 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)

61
code/soup.py Normal file
View File

@ -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())