TeamWork 3>

This commit is contained in:
Si11ium 2019-03-05 12:51:41 +01:00
parent 18c84d1483
commit 7766fed5ab
4 changed files with 169 additions and 122 deletions

View File

@ -1,4 +1,3 @@
import sys
import os
import time
import dill
@ -76,14 +75,18 @@ class FixpointExperiment(Experiment):
else:
self.counters['other'] += 1
class MixedFixpointExperiment(FixpointExperiment):
def run_net(self, net, trains_per_application=100, step_limit=100):
i = 0
while i < step_limit and not net.is_diverged() and not net.is_fixpoint():
net.self_attack()
for _ in tqdm(range(trains_per_application)):
loss = net.compiled().train()
with tqdm(postfix=["Loss", dict(value=0)]) as bar:
for _ in range(trains_per_application):
loss = net.compiled().train()
bar.postfix[1]["value"] = loss
bar.update()
i += 1
self.count(net)

View File

@ -1,9 +1,8 @@
import math
import copy
import os
import numpy as np
from tqdm import tqdm
from keras.models import Sequential
from keras.layers import SimpleRNN, Dense
@ -44,7 +43,7 @@ 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 not (lower_bound <= weight <= upper_bound):
if not (lower_bound <= weight and weight <= upper_bound):
return False
return True
@ -91,7 +90,7 @@ class NeuralNetwork(PrintingObject):
return self.get_model().set_weights(new_weights)
def apply_to_weights(self, old_weights):
raise NotImplementedException
raise NotImplementedError
def apply_to_network(self, other_network):
new_weights = self.apply_to_weights(other_network.get_weights())
@ -101,6 +100,10 @@ class NeuralNetwork(PrintingObject):
other_network.set_weights(self.apply_to_network(other_network))
return self
def fuck(self, other_network):
self.set_weights(self.apply_to_network(other_network))
return self
def self_attack(self, iterations=1):
for _ in range(iterations):
self.attack(self)
@ -110,10 +113,6 @@ class NeuralNetwork(PrintingObject):
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 NeuralNetwork.are_weights_diverged(self.get_weights())
@ -195,7 +194,6 @@ class WeightwiseNeuralNetwork(NeuralNetwork):
def compute_all_normal_weight_points(cls, all_weights):
return cls.compute_all_duplex_weight_points(all_weights)[1]
def apply_to_weights(self, old_weights):
new_weights = copy.deepcopy(self.get_weights())
for (weight_point, normal_weight_point) in zip(*self.__class__.compute_all_duplex_weight_points(old_weights)):
@ -209,7 +207,7 @@ class WeightwiseNeuralNetwork(NeuralNetwork):
print("updated old weight {weight}\t @ ({layer},{cell},{weight_id}) "
"to new value {new_weight}\t calling @ ({normal_layer},{normal_cell},{normal_weight_id})").format(
weight=weight, layer=layer_id, cell=cell_id, weight_id=weight_id, new_weight=new_weight,
normal_layer=normal_layer_id, normal_cell=normal_cell_id, normal_weight_id=normal_weight_id)
normal_layer=normal_layer_id, normal_cell=normal_cell_id, normal_weight_id=normal_weight_id)
return new_weights
def compute_samples(self):
@ -223,7 +221,6 @@ class WeightwiseNeuralNetwork(NeuralNetwork):
return samples_array, samples_array[:, 0]
class AggregatingNeuralNetwork(NeuralNetwork):
@staticmethod
@ -363,15 +360,13 @@ class AggregatingNeuralNetwork(NeuralNetwork):
collections, leftovers = self.__class__.collect_weights(new_weights, collection_size)
new_aggregations = [self.get_aggregator()(collection) for collection in collections]
for aggregation_id,old_aggregation in enumerate(old_aggregations):
for aggregation_id, old_aggregation in enumerate(old_aggregations):
new_aggregation = new_aggregations[aggregation_id]
if abs(new_aggregation - old_aggregation) >= epsilon:
return False, new_aggregations
return True, new_aggregations
class RecurrentNeuralNetwork(NeuralNetwork):
def __init__(self, width, depth, **kwargs):
@ -417,11 +412,10 @@ class RecurrentNeuralNetwork(NeuralNetwork):
for cell_id, cell in enumerate(layer):
for weight_id, weight in enumerate(cell):
old_weights_list += [weight]
sample = np.transpose(np.array([[[old_weights_list[i]] for i in range(len(old_weights_list))]]))
sample = np.asarray(old_weights_list)[None, ..., None]
return sample, sample
class LearningNeuralNetwork(NeuralNetwork):
@staticmethod
@ -455,7 +449,7 @@ class LearningNeuralNetwork(NeuralNetwork):
self.model.compile(**self.compile_params)
def apply_to_weights(self, old_weights):
raise NotImplementedException
raise NotImplementedError
def with_compile_params(self, **kwargs):
self.compile_params.update(kwargs)
@ -473,7 +467,6 @@ class LearningNeuralNetwork(NeuralNetwork):
bar.update()
class TrainingNeuralNetworkDecorator(NeuralNetwork):
def __init__(self, net, **kwargs):
@ -527,6 +520,12 @@ class TrainingNeuralNetworkDecorator(NeuralNetwork):
history = self.net.model.fit(x=x, y=y, verbose=0, batch_size=batchsize)
return history.history['loss'][-1]
def train_other(self, other_network, batchsize=1):
self.compiled()
other_network.compiled()
x, y = other_network.net.compute_samples()
history = self.net.model.fit(x=x, y=y, verbose=0, batch_size=batchsize)
return history.history['loss'][-1]
if __name__ == '__main__':
@ -544,7 +543,8 @@ if __name__ == '__main__':
exp.run_net(net, 100)
exp.log(exp.counters)
if False: # is_fixpoint was wrong because it trivially returned the old weights
if False:
# is_fixpoint was wrong because it trivially returned the old weights
with IdentLearningExperiment() as exp:
net = LearningNeuralNetwork(width=2, depth=2, features=2, )\
.with_keras_params(activation='sigmoid', use_bias=False, ) \
@ -559,10 +559,12 @@ if __name__ == '__main__':
net.print_weights()
time.sleep(1)
print(net.is_fixpoint(epsilon=0.1e-6))
if False: # ok so this works quite realiably
if False:
# ok so this works quite realiably
with FixpointExperiment() as exp:
run_count = 1000
net = TrainingNeuralNetworkDecorator(WeightwiseNeuralNetwork(width=2, depth=2)).with_params(epsilon=0.0001)
net = TrainingNeuralNetworkDecorator(WeightwiseNeuralNetwork(width=2, depth=2))\
.with_params(epsilon=0.0001).with_keras_params(optimizer='sgd')
for run_id in tqdm(range(run_count+1)):
loss = net.compiled().train()
if run_id % 100 == 0:
@ -571,7 +573,9 @@ if __name__ == '__main__':
print("Fixpoint? " + str(net.is_fixpoint()))
print("Loss " + str(loss))
print()
if True: # this does not work as the aggregation function screws over the fixpoint computation.... TODO: check for fixpoint in aggregated space...
if False:
# this does not work as the aggregation function screws over the fixpoint computation....
# TODO: check for fixpoint in aggregated space...
with FixpointExperiment() as exp:
run_count = 1000
net = TrainingNeuralNetworkDecorator(AggregatingNeuralNetwork(4, width=2, depth=2)).with_params(epsilon=0.1e-6)
@ -587,22 +591,28 @@ if __name__ == '__main__':
print("Fixpoint after Agg? " + str(fp))
print("Loss " + str(loss))
print()
if False: # this explodes in our faces completely... NAN everywhere TODO: Wtf is happening here?
if False:
# this explodes in our faces completely... NAN everywhere
# TODO: Wtf is happening here?
with FixpointExperiment() as exp:
run_count = 10
net = TrainingNeuralNetworkDecorator(RecurrentNeuralNetwork(width=2, depth=2)).with_params(epsilon=0.1e-6)
run_count = 10000
net = TrainingNeuralNetworkDecorator(RecurrentNeuralNetwork(width=2, depth=2))\
.with_params(epsilon=0.1e-2).with_keras_params(optimizer='sgd', activation='linear')
for run_id in tqdm(range(run_count+1)):
loss = net.compiled().train()
if run_id % 1 == 0:
if run_id % 500 == 0:
net.print_weights()
# print(net.apply_to_network(net))
print("Fixpoint? " + str(net.is_fixpoint(epsilon=0.0001)))
print("Fixpoint? " + str(net.is_fixpoint()))
print("Loss " + str(loss))
print()
if False: # and this gets somewhat interesting... we can still achieve non-trivial fixpoints over multiple applications when training enough in-between
if True:
# and this gets somewhat interesting... we can still achieve non-trivial fixpoints
# over multiple applications when training enough in-between
with MixedFixpointExperiment() as exp:
for run_id in range(1):
net = TrainingNeuralNetworkDecorator(WeightwiseNeuralNetwork(width=2, depth=2)).with_params(epsilon=0.0001)
net = TrainingNeuralNetworkDecorator(WeightwiseNeuralNetwork(width=2, depth=2))\
.with_params(epsilon=0.0001)
exp.run_net(net, 500, 10)
net.print_weights()
print("Fixpoint? " + str(net.is_fixpoint()))

View File

@ -1,19 +1,23 @@
import random
import copy
from tqdm import tqdm
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 = dict(meeting_rate=0.1, train_other_rate=0.1, train=0)
self.params.update(kwargs)
def with_params(self, **kwargs):
@ -28,17 +32,25 @@ class Soup:
def evolve(self, iterations=1):
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'):
other_particle_id = int(prng() * len(self.particles))
other_particle = self.particles[other_particle_id]
particle.attack(other_particle)
if prng() < self.params.get('train_other_rate'):
other_particle_id = int(prng() * len(self.particles))
other_particle = self.particles[other_particle_id]
particle.train_other(other_particle)
try:
for _ in range(self.params.get('train', 0)):
particle.compiled().train()
except AttributeError:
pass
if self.params.get('remove_divergent') and particle.is_diverged():
self.particles[particle_id] = self.generator()
if self.params.get('remove_zero') and particle.is_zero():
self.particles[particle_id] = self.generator()
def count(self):
counters = dict(divergent=0, fix_zero=0, fix_other=0, fix_sec=0, other=0)
for particle in self.particles:
@ -56,14 +68,37 @@ class Soup:
return counters
class LearningSoup(Soup):
def __init__(self, *args, **kwargs):
super(LearningSoup, self).__init__(**kwargs)
if __name__ == '__main__':
with SoupExperiment() as exp:
for run_id in range(1):
net_generator = lambda: WeightwiseNeuralNetwork(2, 2).with_keras_params(activation='sigmoid').with_params()
# net_generator = lambda: AggregatingNeuralNetwork(4, 2, 2).with_keras_params(activation='sigmoid').with_params(shuffler=AggregatingNeuralNetwork.shuffle_random)
# 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)):
soup.evolve()
exp.log(soup.count())
if False:
with SoupExperiment() as exp:
for run_id in range(1):
net_generator = lambda: WeightwiseNeuralNetwork(2, 2).with_keras_params(activation='linear').with_params()
# net_generator = lambda: AggregatingNeuralNetwork(4, 2, 2).with_keras_params(activation='sigmoid')\
# .with_params(shuffler=AggregatingNeuralNetwork.shuffle_random)
# 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)):
soup.evolve()
exp.log(soup.count())
if True:
with SoupExperiment() as exp:
for run_id in range(1):
net_generator = lambda: TrainingNeuralNetworkDecorator(WeightwiseNeuralNetwork(2, 2)).with_keras_params(
activation='linear')
# net_generator = lambda: AggregatingNeuralNetwork(4, 2, 2).with_keras_params(activation='sigmoid')\
# .with_params(shuffler=AggregatingNeuralNetwork.shuffle_random)
# 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)
soup.seed()
for _ in tqdm(range(10)):
soup.evolve()
exp.log(soup.count())

View File

@ -114,7 +114,6 @@ def compile_run_name(path: str) -> dict:
if __name__ == '__main__':
raise NotImplementedError()
args = build_args()
in_file = args.in_file[0]
out_file = args.out_file