def test_crossing_networks(): network_params = (10, 7, 4) net1 = Network(*network_params) net2 = Network(*network_params) rng = Random("tests") net1.initialise(rng) net2.initialise(rng) net3 = Network(*network_params) net3.cross(rng, net1, net2)
def main(args): rng = Random(args.seed) network_parameters = (105, 103, 100) trainer = Trainer( battleship_fitness, args.class_size, network_parameters, args.cream, rng, args.threads ) if args.class_location: _, _, files = next(os.walk(args.class_location)) networks = [os.path.join(args.class_location, f) for f in files if f.endswith(".network")] trainer.stable = [Network.load_from_file(f) for f in networks] else: trainer.spawn_class() for n in range(args.rounds): print "Beginning round", n trainer.run_round(battleship_trainer) if args.output_location: generation_dir = os.path.join( args.output_location, "gen_{}".format(max(n.generation for n in trainer.stable)) ) os.makedirs(generation_dir) for network in trainer.stable: network.save_to_file(path=generation_dir) network, score = trainer.best network.save_to_file(filename="best_gen{}_{}.network".format( network.generation, score), path=args.output_location)
def run_round(self, task): results = zip(self.stable, self.pool.map(task, self.stable)) if self.best: results.append(self.best) print "Ranking winners" ranked_results = sorted( results, key=lambda (n, r): self.fitness_function(r), reverse=True ) best_result = ranked_results[0] self.best = best_result bn, best = best_result wn, worst = ranked_results[-1] print "Best score was", best, bn.name print "Worst score was", worst, wn.name cream = ranked_results[:self.cream] cream = [n for n, s in cream] print "Selected", len(cream), "best networks" new_class = [] for _ in range(self.cream): new_blood = Network(*self.network_parameters) new_blood.initialise(self.rng) new_class.append(new_blood) while len(new_class) < self.class_size: new_network = Network(*self.network_parameters) new_network.cross(self.rng, *cream) new_class.append(new_network) self.stable = new_class print "Produced", len(self.stable), "new networks"
def initialize_network(self, params): allBut1 = schemes.get("allBut1") all2all = schemes.get("all2all") Input = population.Image_Input(28, 28) c_params = { "eta": params.get("eta", 0.0005), "mu": params.get("mu", 0.05), } L1 = population.Population(num_neurons=params.get("num_neurons", 16), v_init=params.get("v_init", -65), v_decay=params.get("v_decay", .99), v_reset=params.get("v_reset", -65), min_volt=params.get("v_rest", -65), t_init=params.get("min_volt", -50), min_thresh=params.get("min_thresh", -52), t_bias=params.get("t_bias", 0.25), t_decay=params.get("t_decay", .9999999), trace_decay=params.get("trace_decay", 0.95), refrac=params.get("refrac", 5), one_spike=params.get("one_spike", True)) inh = params.get("inh", -120) C1 = Connection(Input, L1, 0.3 * all2all(Input.num_neurons, L1.num_neurons), params, rule="PreAndPost", wmin=0, wmax=1) C2 = Connection(L1, L1, allBut1(L1.num_neurons) * inh, params, rule="static", wmin=inh, wmax=0) self.network = Network([ Input, L1, ], [ C1, C2, ])
C2 = Connection(L1, L2, one2one(L1.num_neurons) * exc, params, rule="static", wmin=0, wmax=exc) C3 = Connection(L2, L1, allBut1(L1.num_neurons) * inh, params, rule="static", wmin=inh, wmax=0) network = Network([Input, L1, L2], [C1, C2, C3]) plotter = Plotter(["voltage", "trace", "activation"]) outer = tqdm(total=100, desc='Epochs', position=0) for i in range(100): train.all_at_once(network, x_train[1000 * i:1000 * (i + 1)], y_train[1000 * i:1000 * (i + 1)], 10, 300, draw_weights=True, plot=plotter) outer.update(1)
wmin=0, wmax=1, norm=78.4) C2 = Connection(L1, L1, allBut1(L1.num_neurons) * inh, params, rule="static", wmin=inh, wmax=0) network = Network([ Input, L1, ], [ C1, C2, ]) plotter = Plotter(["trace"]) # outer = tqdm(total = 100, desc = 'Epochs', position = 0) for i in range(100): # train.all_at_once(network, x_train[500 * i: 500 * (i+1)], y_train[500 * i: 500 * (i+1)], 10, 250, draw_weights = True) print("Training", i) train.train(network, x_train[500 * i:500 * (i + 1)], 250, save_weights=True, plot=plotter)
def create_network((rng, parameters)): print "Creating a network with parameters", parameters rng.jumpahead(int(time())) network = Network(*parameters) network.initialise(rng) return network
0.3 * all2all(Input.num_neurons, L1.num_neurons), params, rule = "static", wmin = 0, wmax = 1, norm = 78.4) C2 = Connection(L1, L1, allBut1(L1.num_neurons) * inh, params, rule = "static", wmin = inh, wmax = 0) network = Network([Input, L1], [C1, C2]) ''' Import and load saved weights ''' weight_file = open('weights.pickle', 'rb') weights = pickle.load(weight_file) weight_file.close() network.set_weights(weights) ''' Record spiking activity of Layer 1 while showing it a bunch of images ''' print("Recording Spikes") num_images = 1 time_step = 50 spikes = [] for i in tqdm(range(num_images)): network.populations[0].set_input(x_train[i]) spikes.append(network.run(time_step, learning=False, record_spikes=True, pop_index=1).get("spikes"))