Exemplo n.º 1
0
def _main():
    # Set random seed for training
    seed = 8964
    tf.set_random_seed(seed)
    np.random.seed(seed)
    random.seed(seed)

    pp = pprint.PrettyPrinter()
    print_flags_dict = {}
    for key in FLAGS.__flags.keys():
        print_flags_dict[key] = getattr(FLAGS, key)

    pp.pprint(print_flags_dict)

    if not os.path.exists(FLAGS.checkpoint_dir):
        os.makedirs(FLAGS.checkpoint_dir)

    assert os.path.exists(FLAGS.train_dir), "Specify training data directory!"
    assert os.path.exists(FLAGS.val_dir), "Specify validation data directory!"
    trl = Learner()
    trl.train(FLAGS)
Exemplo n.º 2
0
def run_second(cfg):

    start_time = time.time()

    try:
        if cfg.hardware.seed.get('secondary', None) is None:
            seed = random.randint(0, sys.maxint)
            cfg.hardware.seed.secondary = seed
        random.seed(cfg.hardware.seed.secondary)

        data = datafile.load_data(cfg.hardware.primaryfile)
        date, primcfg, history = data['date'], data['cfg'], data['history']

        cfg.update(primcfg, overwrite=False)
        primcfg = primcfg.copy(deep=True)

        bot = robots.build_robot(cfg.sim)

        # this should not change
        assert cfg.goals.motor.m_feats == bot.m_feats, 'expected {}, got {}'.format(
            cfg.goals.motor.m_feats, bot.m_feats)
        assert cfg.goals.motor.m_bounds == bot.m_bounds
        # sensors might change
        cfg.goals.effect.s_feats = bot.s_feats
        cfg.goals.effect.s_bounds = bot.s_bounds

        ## Configuration ##

        cfg.goals.effect.res = 10

        ## Bias loading ##

        grid_expl = GridExplorer(primcfg.goals.effect.s_feats,
                                 cfg=primcfg.goals)
        dataset = []
        for order, goal, _, effect in history:
            effect = [
                min(b_max, max(b_min, e_i))
                for e_i, (b_min,
                          b_max) in zip(effect, primcfg.goals.effect.s_bounds)
            ]
            grid_expl.add_effect(effect, goal=goal)
            dataset.append((order, effect))

        bs = bias.IMBias(dataset, effectexplorer=grid_expl)

        ## Instanciation of core modules ##

        #s_explorer = bias.GridBias(bs, cfg.effect.cfg.interest.max_deriv, s_feats, cfg = cfg.effect.cfg)
        s_explorer = BoundedRandomExplorer(bot.s_feats, cfg.goals)
        m_babble = MotorBabble(bot.m_feats, bot.m_bounds, cfg.goals)
        m_explorer = bias.MotorBias(bs, m_babble, cfg.goals)
        guide = Guide(bot.m_feats,
                      bot.s_feats,
                      bot.m_bounds,
                      cfg.goals,
                      goalexplorer=s_explorer,
                      motorbabble=m_explorer)

        learner = Learner(bot.m_feats,
                          bot.s_feats,
                          bot.m_bounds,
                          fwd='ES-LWLR',
                          inv='L-BFGS-B')

        cfg.freeze()
        print(cfg.makeReport())

        ## Running learning ##

        for i in xrange(cfg.exp.trials):
            exp.learn(bot, guide, learner)
            gfx.print_progress(i + 1,
                               cfg.exp.trials,
                               prefix='{}learning... {}'.format(
                                   gfx.purple, gfx.cyan),
                               suffix=gfx.end,
                               eta=1)
        print ''

        exp.save_data(cfg, guide)

    except Exception:
        import traceback
        traceback.print_exc()
    finally:
        bot.close()

    print("ran for {}s.".format(int(time.time() - start_time)))
Exemplo n.º 3
0
def run_tests(cfg):

    start_time = time.time()

    try:
        cfg.update(defaultcfg, overwrite=False)

        data = datafile.load_data(cfg.hardware.datafile)
        date, expcfg, history = data['date'], data['cfg'], data['history']
        cfg.test.setdefault('max', value=len(history))
        cfg.test.max = min(len(history), cfg.test.max)

        cfg.update(expcfg, overwrite=False)

        bot = robots.build_robot(cfg.sim)

        assert cfg.goals.effect.s_feats == bot.s_feats
        assert cfg.goals.effect.s_bounds == bot.s_bounds
        assert cfg.goals.motor.m_feats == bot.m_feats
        assert cfg.goals.motor.m_bounds == bot.m_bounds

        learner = Learner(bot.m_feats,
                          bot.s_feats,
                          bot.m_bounds,
                          fwd='ES-LWLR',
                          inv='L-BFGS-B')
        if cfg.test.testsetfile is None:
            testset = exp.create_tests(bot.s_bounds,
                                       cfg.test.res,
                                       fixed_dim=cfg.test.fixed_dim)
        else:
            testset = datafile.load_testset(cfg.test.testsetfile)

        print(cfg.makeReport())

        ticks, results, averages, stds = [], [], [], []
        for counter, (order, goal, _, effect) in enumerate(history):
            if (counter) % cfg.test.freq == 0:
                ticks.append(counter)

                result = exp.test_results(bot, learner, testset)
                avg, std = exp.test_perfs(testset, result)

                results.append(result)
                averages.append(avg)
                stds.append(std)
                print("{}{:5d}: {}{:6.4f}{}".format(gfx.purple, counter,
                                                    gfx.cyan, avg, gfx.end))

            if counter > cfg.test.max:
                break

            learner.add_xy(order, effect)

        exp.save_test(cfg, date, tuple(testset), tuple(ticks), tuple(results),
                      tuple(averages), tuple(stds))

    except Exception:
        import traceback
        traceback.print_exc()
    finally:
        bot.close()

    print("ran for {}s.".format(int(time.time() - start_time)))
Exemplo n.º 4
0
def run_first(cfg):

    start_time = time.time()

    try:
        if cfg.hardware.seed.get('primary', None) is None:
            seed = random.randint(0, sys.maxint)
            cfg.hardware.seed.primary   = seed
        random.seed(cfg.hardware.seed.primary)

        bot = robots.build_robot(cfg.sim)

            ## Configuration ##

        goalscfg = treedict.TreeDict()

        goalscfg.effect.explorer = BoundedRandomExplorer

        cfg.goals.update(goalscfg, overwrite = False)

        cfg.goals.effect.s_feats  = s_feats  = bot.s_feats
        cfg.goals.effect.s_bounds = s_bounds = bot.s_bounds
        cfg.goals.motor.m_feats   = m_feats  = bot.m_feats
        cfg.goals.motor.m_bounds  = m_bounds = bot.m_bounds

            ## Instanciation of core modules ##

        s_explorer = BoundedRandomExplorer(s_feats, cfg.goals)
        if 'explorename' in cfg.goals.effect:
            if cfg.goals.effect.explorername == 'cluster':
                s_explorer = cluster.ClusterExplorer(s_feats, cfg.goals)
            elif cfg.goals.effect.explorername == 'cluster_im':
                s_explorer = cluster.ClusterIMExplorer(s_feats, cfg.goals)

        m_explorer = MotorBabble(bot.m_feats, bot.m_bounds, cfg.goals)
        guide      = Guide(m_feats, s_feats, m_bounds, cfg.goals,
                           goalexplorer = s_explorer, motorbabble = m_explorer)
        learner    = Learner(m_feats, s_feats, m_bounds, fwd = 'ES-LWLR', inv = 'L-BFGS-B')

        cfg.freeze()
        print(cfg.makeReport())

            ## Runing learning ##

        for i in xrange(cfg.exp.trials):
            exp.learn(bot, guide, learner)
            if not cfg.sim.verbose:
                gfx.print_progress(i+1, cfg.exp.trials,
                                   prefix = '{}learning... {}'.format(gfx.purple, gfx.cyan),
                                   suffix = gfx.end, eta = 1)
        print ''

        exp.save_data(cfg, guide)

    except Exception:
        import traceback
        traceback.print_exc()
    finally:
        bot.close()

    print("ran for {}s.".format(int(time.time() - start_time)))
Exemplo n.º 5
0
 def create_learner(self):
     learner = Learner(self, self.generate_id())
     self.learners.append(learner)