Exemple #1
0
 def prepare_hd(self):
     datafile.save_config(self.jobcfg,
                          filename=self.jobcfg.hardware.configfile,
                          directory=self.context.fulldir)
Exemple #2
0
def explore(cfg):
    cfg_orig = cfg._deepcopy()
    autosave.period = cfg.hardware.autosave_period

    try:
            ## Load potentially existing data ##
        history = load_existing_datafile(cfg, core_keys=('exploration', 'feedback', 'meta'))
        if history is None:
            # set a random seed if none already set.
            cfg.hardware._setdefault('seed', random.randint(0, 9223372036854775807)) # sys.maxint
        else:
            # replace config by the previous (matching) config,
            # as it contains non-reproductible explorers uuids, and provenance data.
            cfg = history.meta['jobcfg']

        random.seed(cfg.hardware.seed)


            ## Instanciating the environment ##

        env = environments.Environment.create(cfg.exploration.env)


            ## Instanciating the explorer ##

        src_datasets = load_src_files(cfg, env.m_channels)
        if history is None:
            cfg.exploration.explorer.m_channels = env.m_channels
            cfg.exploration.explorer.s_channels = env.s_channels
        explorer = explorers.Explorer.create(cfg.exploration.explorer, datasets=src_datasets)
        print('configuration:\n', cfg, '\n', sep='')


            ## Running learning ##

        prov_data = provenance.ProvenanceData.from_desc(cfg.provenance.desc, env)
        if cfg.provenance.check_dirty:
            prov_data.check_dirty()

        if history is not None:
            try:
                if cfg.provenance.check_continuity:
                    print('provenance continuity verified; proceeding from last checkpoint.')
                    assert prov_data.same_cfg(cfg.provenance.data)
            except AssertionError: # provenance changed: restarting from scratch
                print("continuity broken, restarting...")
                history = None

        if history is None:

            cfg.provenance['data'] = prov_data.cfg()

            history = chrono.ChronoHistory(cfg.hardware.datafile, cfg.hardware.logfile,
                                           meta={'jobcfg.pristine': cfg_orig,
                                                 'jobcfg': cfg,
                                                 'm_channels': env.m_channels,
                                                 's_channels': env.s_channels,
                                                 'random_state': random.getstate()},
                                           core_keys=('exploration', 'feedback', 'meta'),
                                           extralog=cfg.hardware.logs,
                                           verbose=True, load=False)

        # replaying history
        for entry in history:
            explorer.receive(entry['data']['exploration'], entry['data']['feedback'])

        # setting random state
        random.setstate(history.meta['random_state'])

        # running exploration; the next three lines are the core of the experiment.
        for t in range(len(history), cfg.exploration.steps):
            entry = exploration_step(env, explorer)
            entry = filter_entry(cfg, entry)
            history.add_entry(t, entry)
            print('step {} done'.format(t))
            if autosave.autosave(cfg.hardware.autosave_period):
                # save history at regular intervals
                history.meta['random_state'] = random.getstate()
                history.save()
                print('autosaved...')


            ## Finishing ##

        feedback_history = chrono.ChronoHistory(cfg.hardware.sensoryfile, None,
                                                core_keys=('s_signal', 'from'), load=False,
                                                meta={'jobcfg': cfg,
                                                      'm_channels': env.m_channels,
                                                      's_channels': env.s_channels})
        for t, entry in enumerate(history):
            exploration = entry['data']['exploration']
            feedback    = entry['data']['feedback']

            feedback_history.add_entry(t, {'s_signal': feedback['s_signal'], 'from': exploration['from']})
        feedback_history.save()

        history.meta['random_state'] = random.getstate()
        history.save(verbose=True, done=True)
        datafile.save_config(cfg, filename=cfg.hardware.configfile+'.done', directory='')

    except Exception:
        import traceback
        traceback.print_exc()
    finally:
        env.close()
Exemple #3
0
def explore(cfg):
    cfg_orig = cfg._deepcopy()
    autosave.period = cfg.hardware.autosave_period

    try:
        ## Load potentially existing data ##
        history = load_existing_datafile(cfg,
                                         core_keys=('exploration', 'feedback',
                                                    'meta'))
        if history is None:
            # set a random seed if none already set.
            cfg.hardware._setdefault('seed',
                                     random.randint(
                                         0, 9223372036854775807))  # sys.maxint
        else:
            # replace config by the previous (matching) config,
            # as it contains non-reproductible explorers uuids, and provenance data.
            cfg = history.meta['jobcfg']

        random.seed(cfg.hardware.seed)

        ## Instanciating the environment ##

        env = environments.Environment.create(cfg.exploration.env)

        ## Instanciating the explorer ##

        src_datasets = load_src_files(cfg, env.m_channels)
        if history is None:
            cfg.exploration.explorer.m_channels = env.m_channels
            cfg.exploration.explorer.s_channels = env.s_channels
        explorer = explorers.Explorer.create(cfg.exploration.explorer,
                                             datasets=src_datasets)
        print('configuration:\n', cfg, '\n', sep='')

        ## Running learning ##

        prov_data = provenance.ProvenanceData.from_desc(
            cfg.provenance.desc, env)
        if cfg.provenance.check_dirty:
            prov_data.check_dirty()

        if history is not None:
            try:
                if cfg.provenance.check_continuity:
                    print(
                        'provenance continuity verified; proceeding from last checkpoint.'
                    )
                    assert prov_data.same_cfg(cfg.provenance.data)
            except AssertionError:  # provenance changed: restarting from scratch
                print("continuity broken, restarting...")
                history = None

        if history is None:

            cfg.provenance['data'] = prov_data.cfg()

            history = chrono.ChronoHistory(cfg.hardware.datafile,
                                           cfg.hardware.logfile,
                                           meta={
                                               'jobcfg.pristine': cfg_orig,
                                               'jobcfg': cfg,
                                               'm_channels': env.m_channels,
                                               's_channels': env.s_channels,
                                               'random_state':
                                               random.getstate()
                                           },
                                           core_keys=('exploration',
                                                      'feedback', 'meta'),
                                           extralog=cfg.hardware.logs,
                                           verbose=True,
                                           load=False)

        # replaying history
        for entry in history:
            explorer.receive(entry['data']['exploration'],
                             entry['data']['feedback'])

        # setting random state
        random.setstate(history.meta['random_state'])

        # running exploration; the next three lines are the core of the experiment.
        for t in range(len(history), cfg.exploration.steps):
            entry = exploration_step(env, explorer)
            entry = filter_entry(cfg, entry)
            history.add_entry(t, entry)
            print('step {} done'.format(t))
            if autosave.autosave(cfg.hardware.autosave_period):
                # save history at regular intervals
                history.meta['random_state'] = random.getstate()
                history.save()
                print('autosaved...')

            ## Finishing ##

        feedback_history = chrono.ChronoHistory(cfg.hardware.sensoryfile,
                                                None,
                                                core_keys=('s_signal', 'from'),
                                                load=False,
                                                meta={
                                                    'jobcfg': cfg,
                                                    'm_channels':
                                                    env.m_channels,
                                                    's_channels':
                                                    env.s_channels
                                                })
        for t, entry in enumerate(history):
            exploration = entry['data']['exploration']
            feedback = entry['data']['feedback']

            feedback_history.add_entry(t, {
                's_signal': feedback['s_signal'],
                'from': exploration['from']
            })
        feedback_history.save()

        history.meta['random_state'] = random.getstate()
        history.save(verbose=True, done=True)
        datafile.save_config(cfg,
                             filename=cfg.hardware.configfile + '.done',
                             directory='')

    except Exception:
        import traceback
        traceback.print_exc()
    finally:
        env.close()
Exemple #4
0
 def prepare_hd(self):
     datafile.save_config(self.jobcfg, filename=self.jobcfg.hardware.configfile,
                                       directory=self.context.fulldir)