Advertisement
Guest User

Recursive Self Improvement

a guest
Nov 20th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.18 KB | None | 0 0
  1. """
  2. Self-improvement demo.
  3.  
  4. Starts with a random collection of neural net neuron layer sizes and some
  5. random intelligence ratings.
  6.  
  7. Trains each net to reproduce the ratings for all the nets.
  8.  
  9. Assigns prediction accuracy as the new intelligence rating.
  10.  
  11. Then does some GA thingy:
  12.  
  13. Take each net topology
  14.  
  15. Have it rate some modifications of itself.
  16.  
  17. Pick the best ones and add them back to the population.
  18.  
  19. Then calculate actual prediction accuracies and truncation select or something.
  20.  
  21. Then repeat.
  22.  
  23. Doesn't actually work very well. Why?
  24.  
  25. """
  26.  
  27. from pybrain.tools.shortcuts import buildNetwork
  28. from pybrain.datasets import SupervisedDataSet
  29. from pybrain.supervised.trainers import BackpropTrainer
  30.  
  31. import random
  32. import itertools
  33. import math
  34.  
  35. # We fix the number of layers
  36. # This is not counting input and output layers
  37. num_layers = 4
  38.  
  39. # And the pop size
  40. pop_size = 100
  41.  
  42. # Truncation selection portion
  43. truncation_count = 10
  44.  
  45. # How many options each network is given for children
  46. child_count = 100
  47.  
  48. # How many children we take from each selected network.
  49. # Needs to be the right number to rebuild the population
  50. child_survival = pop_size / truncation_count
  51.  
  52. # And the training iterations
  53. training_iterations = 10
  54.  
  55. # And the generations
  56. generations = 10
  57.  
  58.  
  59.  
  60. # This holds the (net layer sizes list, fitness) lists for individuals
  61. population = []
  62.  
  63. for i in xrange(pop_size):
  64.     # Fill in the layer sizes. The first is always exactly enough to
  65.     # receive all the lauyer sizes.
  66.     layer_sizes = []
  67.     for j in xrange(num_layers):
  68.         # Work out how big the layer should be and add it
  69.         layer_sizes.append(random.randint(1, 10))
  70.     # Give it a random fitness and add it to the population
  71.     population.append([layer_sizes, random.random()])
  72.  
  73. print("Made population")
  74.  
  75. for iteration_number in xrange(generations):
  76.  
  77.     # Make a dataset of the population
  78.     dataset = SupervisedDataSet(num_layers, 1)
  79.     for net_plan, fitness in population:
  80.         # Add every network and its fitness as a training sample
  81.         # Every plan has a 1 at the end and a num_layers at the start
  82.         dataset.addSample(net_plan, [fitness])
  83.  
  84.     print("Made dataset")
  85.  
  86.     # Evaluate all the fitnesses
  87.     fitnesses = []
  88.     # Save the trained networks
  89.     trained = []
  90.     for net_plan, old_fitness in population:
  91.         # Make the network
  92.         net = buildNetwork(*([num_layers] +  net_plan + [1]))
  93.  
  94.         print("Training net {}".format(net_plan))
  95.         # Make the trainer
  96.         trainer = BackpropTrainer(net, dataset)
  97.         # Train it and get the training set error.
  98.         # TODO: use generalization error?
  99.         for i in xrange(training_iterations):
  100.             finalError = trainer.train()
  101.  
  102.         fitness = -math.log(finalError)
  103.  
  104.         print("Trained {} to fitness {}".format(net_plan, fitness))
  105.  
  106.         # New fitness is 1-error.
  107.         fitnesses.append(fitness)
  108.         # Save the trained net
  109.         trained.append(net)
  110.  
  111.     # Apply the fitnesses so we can sort everything
  112.     marked_population = []
  113.     for new_fitness, net, (net_plan, old_fitness) in itertools.izip(
  114.         fitnesses, trained, population):
  115.  
  116.         marked_population.append([net_plan, net, new_fitness])
  117.  
  118.     # Sort by fitness
  119.     population = sorted(marked_population, key=lambda x: x[2], reverse=True)
  120.  
  121.     new_population = []
  122.  
  123.     # Build a new population
  124.     for i in xrange(truncation_count):
  125.         # Take the top so many and let them choose from random modifications
  126.         # Grab the trained net
  127.         net = population[i][1]
  128.         # And its plan
  129.         net_plan = population[i][0]
  130.         # And its fitness
  131.         fitness = population[i][2]
  132.  
  133.         print("Making children for {} which scored {}".format(net_plan,
  134.             fitness))
  135.  
  136.         # holds (rating, child) pairs
  137.         rated_children = []
  138.  
  139.         # First we just make the children
  140.         children = set()
  141.  
  142.         for j in xrange(child_count):
  143.             # Make a child
  144.             child = list(net_plan)
  145.  
  146.             # Make a modification
  147.             child[random.randint(0, len(child) - 1)] += random.randint(-2, 2)
  148.  
  149.             # Clean up the child
  150.             for k in xrange(len(child)):
  151.                 if child[k] <= 0:
  152.                     # Layers must not be empty
  153.                     child[k] = random.randint(1, 10)
  154.  
  155.             # Add and deduplicate children
  156.             children.add(tuple(child))
  157.  
  158.         for child in children:
  159.             # Have the parent rate the child
  160.             rating = net.activate(child)[0]
  161.  
  162.             rated_children.append([rating, child])
  163.            
  164.         # Sort children by rating, descending
  165.         rated_children = sorted(rated_children, reverse=True)
  166.  
  167.         while len(rated_children) < child_survival:
  168.             # Add in more children
  169.             rated_children += rated_children
  170.             # And re-sort
  171.             rated_children = sorted(rated_children, reverse=True)
  172.  
  173.         # Contribute children to population, with parent rating as their
  174.         # goodness to be predicted
  175.         for j in xrange(child_survival):
  176.             print("\tContributed child: {} with rating {}".format(
  177.                 rated_children[j][1], rated_children[j][0]))
  178.  
  179.             # Train the child on the parents and calculate its fitness
  180.             # Make the network
  181.             child_net = buildNetwork(*([num_layers] +  list(rated_children[j][1]) + [1]))
  182.  
  183.             # Make the trainer
  184.             trainer = BackpropTrainer(child_net, dataset)
  185.             # Train it and get the training set error.
  186.             # TODO: use generalization error?
  187.             for i in xrange(training_iterations):
  188.                 finalError = trainer.train()
  189.  
  190.             fitness = -math.log(finalError)
  191.  
  192.             print("Trained child to fitness {}".format(fitness))
  193.  
  194.             # Child's value to be predicted is its performance on the parents'
  195.             # generation
  196.             new_population.append([list(rated_children[j][1]), fitness])
  197.  
  198.     # Replace the population
  199.     population = new_population
  200.  
  201. # Report the final population
  202. print("Final population: {}".format(population))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement