UserPreferences

DevelopmentalRoboticsExperiment1


1. Our First Experiment with Neural Networks

1.1. A Useful Template for Saving Data from a Robot

# A Pyro bare brain template

from pyrobot.brain import Brain

class MyBrain(Brain):
   def setup(self):
        self.robot.bumper[0].units = "scaled"
        self.inputFile = open("inputs.dat", "w")
        self.targetFile = open("targets.dat", "w")
        self.lastTran = 1
   def step(self):
        self.inputFile.write("%f %f " % tuple(self.robot.bumper[0].values()))
        translation = not max(self.robot.bumper[0].values())
        self.inputFile.write("%f %f\n" % (self.robot.stall, self.lastTran))
        self.targetFile.write("%f\n" % translation)
        self.lastTran = translation

def INIT(engine):
   assert(engine.robot.hasA("bumper"))
   return MyBrain('MyBrain', engine)

1.2. A Neural Network that takes Weights from a File and teaches a Robot to Move

# A Pyro bare brain template

from pyrobot.brain import Brain
from pyrobot.brain.conx import Network

class MyBrain(Brain):
   def setup(self):
      self.net = Network()
      self.net.addLayers(4, 2, 2) # ir inputs
      self.net.loadWeightsFromFile("trainedwts.wts")
      self.lastTran = 1

   def step(self):
      x = self.robot.bumper[0].values()
      x.extend([self.robot.stall, self.lastTran])
      output = self.net.propagate(input = x)
      self.robot.move((output[0]*2) - 1, (output[1]*2) - 1)
      self.lastTran = output[0]

def INIT(engine):
   return MyBrain('MyBrain', engine)

1.3. Input Training Data

1 0 0 1
1 1 0 1
0 1 0 1
1 0 1 1
1 1 1 1
0 1 1 1
0 0 1 0
0 0 0 0
0 0 0 1

1.4. Target Training Data

0 .25
0 .5
0 .75
0 .25
0 .5
0 .75
1 .5
0 .5
1 .5

2. To Train a Neural Network

What you want to do in order to train a neural network is enter the following commands:
>>> from pyrobot.brain.conx import * 
>>> net = Network()
>>> net.addLayers(4, 2, 2)
>>> net.loadInputsFromFile("inputs.dat")
>>> net.loadTargetsFromFile("targets.dat")
>>> net.train()

Then to save the weights, simply type:

>>> net.saveWeightsToFile("trainedwts.wts")