fixed some stuff in the code

This commit is contained in:
Thomas Gabor
2019-03-06 03:27:31 +01:00
parent 604ad6204d
commit 2966b41baf
2 changed files with 814 additions and 802 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,104 +1,108 @@
import random import random
import copy import copy
from tqdm import tqdm from tqdm import tqdm
from experiment import * from experiment import *
from network import * from network import *
def prng(): def prng():
return random.random() return random.random()
class Soup: class Soup:
def __init__(self, size, generator, **kwargs): def __init__(self, size, generator, **kwargs):
self.size = size self.size = size
self.generator = generator self.generator = generator
self.particles = [] self.particles = []
self.params = dict(meeting_rate=0.1, train_other_rate=0.1, train=0) self.params = dict(meeting_rate=0.1, train_other_rate=0.1, train=0)
self.params.update(kwargs) self.params.update(kwargs)
def with_params(self, **kwargs): def with_params(self, **kwargs):
self.params.update(kwargs) self.params.update(kwargs)
return self return self
def seed(self): def seed(self):
self.particles = [] self.particles = []
for _ in range(self.size): for _ in range(self.size):
self.particles += [self.generator()] self.particles += [self.generator()]
return self return self
def evolve(self, iterations=1): def evolve(self, iterations=1):
for _ in range(iterations): for _ in range(iterations):
for particle_id, particle in enumerate(self.particles): for particle_id, particle in enumerate(self.particles):
if prng() < self.params.get('meeting_rate'): if prng() < self.params.get('meeting_rate'):
other_particle_id = int(prng() * len(self.particles)) other_particle_id = int(prng() * len(self.particles))
other_particle = self.particles[other_particle_id] other_particle = self.particles[other_particle_id]
particle.attack(other_particle) particle.attack(other_particle)
if prng() < self.params.get('train_other_rate'): if prng() < self.params.get('train_other_rate'):
other_particle_id = int(prng() * len(self.particles)) other_particle_id = int(prng() * len(self.particles))
other_particle = self.particles[other_particle_id] other_particle = self.particles[other_particle_id]
particle.train_other(other_particle) particle.train_other(other_particle)
try: for _ in range(self.params.get('train', 0)):
for _ in range(self.params.get('train', 0)): particle.compiled().train()
particle.compiled().train() if self.params.get('remove_divergent') and particle.is_diverged():
except AttributeError: self.particles[particle_id] = self.generator()
pass if self.params.get('remove_zero') and particle.is_zero():
if self.params.get('remove_divergent') and particle.is_diverged(): self.particles[particle_id] = self.generator()
self.particles[particle_id] = self.generator()
if self.params.get('remove_zero') and particle.is_zero(): def count(self):
self.particles[particle_id] = self.generator() counters = dict(divergent=0, fix_zero=0, fix_other=0, fix_sec=0, other=0)
for particle in self.particles:
def count(self): if particle.is_diverged():
counters = dict(divergent=0, fix_zero=0, fix_other=0, fix_sec=0, other=0) counters['divergent'] += 1
for particle in self.particles: elif particle.is_fixpoint():
if particle.is_diverged(): if particle.is_zero():
counters['divergent'] += 1 counters['fix_zero'] += 1
elif particle.is_fixpoint(): else:
if particle.is_zero(): counters['fix_other'] += 1
counters['fix_zero'] += 1 elif particle.is_fixpoint(2):
else: counters['fix_sec'] += 1
counters['fix_other'] += 1 else:
elif particle.is_fixpoint(2): counters['other'] += 1
counters['fix_sec'] += 1 return counters
else:
counters['other'] += 1 def print_all(self):
return counters for particle in self.particles:
particle.print_weights()
print(particle.is_fixpoint())
class LearningSoup(Soup):
def __init__(self, *args, **kwargs): class LearningSoup(Soup):
super(LearningSoup, self).__init__(**kwargs)
def __init__(self, *args, **kwargs):
super(LearningSoup, self).__init__(**kwargs)
if __name__ == '__main__':
if False:
with SoupExperiment() as exp: if __name__ == '__main__':
for run_id in range(1): if False:
net_generator = lambda: WeightwiseNeuralNetwork(2, 2).with_keras_params(activation='linear').with_params() with SoupExperiment() as exp:
# net_generator = lambda: AggregatingNeuralNetwork(4, 2, 2).with_keras_params(activation='sigmoid')\ for run_id in range(1):
# .with_params(shuffler=AggregatingNeuralNetwork.shuffle_random) net_generator = lambda: WeightwiseNeuralNetwork(2, 2).with_keras_params(activation='linear').with_params()
# net_generator = lambda: RecurrentNeuralNetwork(2, 2).with_keras_params(activation='linear').with_params() # net_generator = lambda: AggregatingNeuralNetwork(4, 2, 2).with_keras_params(activation='sigmoid')\
soup = Soup(100, net_generator).with_params(remove_divergent=True, remove_zero=True) # .with_params(shuffler=AggregatingNeuralNetwork.shuffle_random)
soup.seed() # net_generator = lambda: RecurrentNeuralNetwork(2, 2).with_keras_params(activation='linear').with_params()
for _ in tqdm(range(100)): soup = Soup(100, net_generator).with_params(remove_divergent=True, remove_zero=True)
soup.evolve() soup.seed()
exp.log(soup.count()) for _ in tqdm(range(100)):
soup.evolve()
if True: exp.log(soup.count())
with SoupExperiment() as exp:
for run_id in range(1): if True:
net_generator = lambda: TrainingNeuralNetworkDecorator(WeightwiseNeuralNetwork(2, 2)).with_keras_params( with SoupExperiment("soup") as exp:
activation='linear') for run_id in range(1):
# net_generator = lambda: AggregatingNeuralNetwork(4, 2, 2).with_keras_params(activation='sigmoid')\ net_generator = lambda: TrainingNeuralNetworkDecorator(WeightwiseNeuralNetwork(2, 2)).with_keras_params(
# .with_params(shuffler=AggregatingNeuralNetwork.shuffle_random) activation='sigmoid').with_params(epsilon=0.0001)
# net_generator = lambda: RecurrentNeuralNetwork(2, 2).with_keras_params(activation='linear').with_params()
soup = Soup(10, net_generator).with_params(remove_divergent=True, remove_zero=True).with_params(train=500) # net_generator = lambda: AggregatingNeuralNetwork(4, 2, 2).with_keras_params(activation='sigmoid')\
soup.seed() # .with_params(shuffler=AggregatingNeuralNetwork.shuffle_random)
for _ in tqdm(range(10)): # net_generator = lambda: RecurrentNeuralNetwork(2, 2).with_keras_params(activation='linear').with_params()
soup.evolve() soup = Soup(10, net_generator).with_params(remove_divergent=True, remove_zero=True, train=200)
exp.log(soup.count()) soup.seed()
for _ in tqdm(range(10)):
soup.evolve()
soup.print_all()
exp.log(soup.count())