def main(): options, args = make_optparse().parse_args() print "Loading data" data = load_dataset(options.datapath) print "Building network" net = rnn.RecurrentNetwork(74, options.n_hidden, 1, outfunc='sig') print "Number of parameters:", len(net.parameters) objfunc = make_objective_func(net, data, rnn.SumOfSquares()) x0 = scipy.random.standard_normal(len(net.parameters)) * 0.1 optimizer = PGPE(objfunc, x0, minimize=True) optimizer.maxEvaluations = options.maxevals print "First fitness:", objfunc(x0) print "Optimizing..." params, fitness = optimizer.learn() print "Last fitness:", fitness true_positives, total = stats(net, data) print "Total positives found: %i (%.2f)" % (true_positives, true_positives / total) return 0
env = None for runs in range(numbExp): # create environment #Options: Bool(OpenGL), Bool(Realtime simu. while client is connected), ServerIP(default:localhost), Port(default:21560) if env != None: env.closeSocket() env = ShipSteeringEnvironment() # create task task = GoNorthwardTask(env, maxsteps=500) # create controller network net = buildNetwork(task.outdim, task.indim, outclass=TanhLayer) # create agent with controller and learner (and its options) agent = OptimizationAgent( net, PGPE(learningRate=0.3, sigmaLearningRate=0.15, momentum=0.0, epsilon=2.0, rprop=False, storeAllEvaluations=True)) et.agent = agent #create experiment experiment = EpisodicExperiment(task, agent) #Do the experiment for updates in range(epis): for i in range(prnts): experiment.doEpisodes(batch) et.printResults((agent.learner._allEvaluations)[-50:-1], runs, updates) et.addExps() et.showExps() #To view what the simulation is doing at the moment set the environment with True, go to pybrain/rl/environments/ode/ and start viewer.py (python-openGL musst be installed, see PyBrain documentation)
exp_desciptions = [] for i in range(num_experiments): # set up environment, task, neural net, agent, and experiment env = InfoMaxEnv(object_names, action_names, num_objects, False) task = InfoMaxTask(env, max_steps=max_steps) net = buildNetwork(task.outdim, task.indim, bias=True, outclass=SoftmaxLayer) if algorithm == 'pgpe': agent = OptimizationAgent( net, PGPE(storeAllEvaluations=True, minimize=False, verbose=False)) elif algorithm == 'cmaes': agent = OptimizationAgent(net, CMAES(minimize=False, verbose=False)) experiment = EpisodicExperiment(task, agent) exp_desciptions.append([i, agent, experiment, task]) pool = Pool(processes=num_cpus) res = [] if algorithm == 'pgpe': res = pool.map(run_experiment, exp_desciptions) elif algorithm == 'cmaes': for desc in exp_desciptions:
def main(): if len(sys.argv) != 2: print 'Please provide a path to a model data directory.' print ('The script will load the newest model data from the directory,' 'then continue to improve that model') sys.exit(0) model_directory = sys.argv[1] existing_models = sorted(glob(os.path.join(model_directory, '*.rlmdl'))) if existing_models: newest_model_name = existing_models[-1] iteration_count = int(newest_model_name[-12:-6]) + 1 print 'Loading model {}'.format(newest_model_name) newest_model = open(newest_model_name, 'r') agent = pickle.load(newest_model) else: net = buildNetwork(Environment.outdim, Environment.outdim + Environment.indim, Environment.indim) agent = OptimizationAgent(net, PGPE()) iteration_count = 1 environment = Environment(LOCAL_HOST, PORT, PATH_TO_SCENE) task = Task(environment) experiment = EpisodicExperiment(task, agent) def signal_handler(signal, frame): print 'Exiting gracefully' environment.teardown() sys.exit(0) signal.signal(signal.SIGINT, signal_handler) while True: time.sleep(1) print '>>>>> Running iteration {}'.format(iteration_count) # NOTE this weird stuff is hacky, but we need it to plug in our autosave # stuff properly. Took a long time to figure this out. experiment.optimizer.maxEvaluations = experiment.optimizer.numEvaluations + experiment.optimizer.batchSize try: experiment.doEpisodes() except Exception as e: print 'ERROR RUNNING SIMULATION: \n{}'.format(e) environment.teardown() else: if iteration_count % AUTOSAVE_INTERVAL == 0: filename = str(iteration_count).zfill(6) + '.rlmdl' filename = os.path.join(model_directory, filename) f = open(filename, 'w+') print 'Saving model to {}'.format(filename) pickle.dump(agent, f) iteration_count += 1 print 'Iteration finished <<<<<'
def PGPE_Factory(): return PGPE()
numbExp = 10 #number of experiments et = ExTools(batch, prnts) #tool for printing and plotting env = None for runs in range(numbExp): # create environment #Options: Bool(OpenGL), Bool(Realtime simu. while client is connected), ServerIP(default:localhost), Port(default:21560) if env != None: env.closeSocket() env = JohnnieEnvironment() # create task task = StandingTask(env) # create controller network net = buildNetwork(len(task.getObservation()), hiddenUnits, env.actLen, outclass=TanhLayer) # create agent with controller and learner (and its options) agent = OptimizationAgent(net, PGPE(storeAllEvaluations=True)) et.agent = agent # create the experiment experiment = EpisodicExperiment(task, agent) #Do the experiment for updates in range(epis): for i in range(prnts): experiment.doEpisodes(batch) et.printResults((agent.learner._allEvaluations)[-50:-1], runs, updates) et.addExps() et.showExps() #To view what the simulation is doing at the moment, go to pybrain/rl/environments/ode/ and start viewer.py (python-openGL musst be installed, see PyBrain documentation)