def run_by_mode(spec_file, spec_name, lab_mode): logger.info(f'Running lab in mode: {lab_mode}') spec = spec_util.get(spec_file, spec_name) info_space = InfoSpace() os.environ['PREPATH'] = util.get_prepath(spec, info_space) reload(logger) # to set PREPATH properly # expose to runtime, '@' is reserved for 'enjoy@{prepath}' os.environ['lab_mode'] = lab_mode.split('@')[0] if lab_mode == 'search': info_space.tick('experiment') Experiment(spec, info_space).run() elif lab_mode == 'train': info_space.tick('trial') Trial(spec, info_space).run() elif lab_mode.startswith('enjoy'): prepath = lab_mode.split('@')[1] spec, info_space = util.prepath_to_spec_info_space(prepath) Session(spec, info_space).run() elif lab_mode == 'generate_benchmark': benchmarker.generate_specs(spec, const='agent') elif lab_mode == 'benchmark': # TODO allow changing const to env run_benchmark(spec, const='agent') elif lab_mode == 'dev': spec = util.override_dev_spec(spec) info_space.tick('trial') Trial(spec, info_space).run() else: logger.warn( 'lab_mode not recognized; must be one of `search, train, enjoy, benchmark, dev`.' )
def plot_session_from_file(session_df_filepath): ''' Method to plot session from its session_df file @example from slm_lab.experiment import analysis filepath = 'data/reinforce_cartpole_2018_01_22_211751/reinforce_cartpole_t0_s0_session_df.csv' analysis.plot_session_from_file(filepath) ''' from slm_lab.experiment.monitor import InfoSpace spec_name = util.prepath_to_spec_name(session_df_filepath) session_spec = {'name': spec_name} session_df = util.read(session_df_filepath, header=[0, 1, 2, 3], index_col=0, dtype=np.float32) session_data = util.session_df_to_data(session_df) tn, sn = session_df_filepath.replace('_session_df.csv', '').split('_')[-2:] info_space = InfoSpace() info_space.set('experiment', 0) info_space.set('trial', int(tn[1:])) info_space.set('session', int(sn[1:])) session_fig = plot_session(session_spec, info_space, session_data) viz.save_image( session_fig, session_df_filepath.replace('_session_df.csv', '_session_graph.png'))
def run_by_mode(spec_file, spec_name, lab_mode): logger.info(f'Running lab in mode: {lab_mode}') spec = spec_util.get(spec_file, spec_name) info_space = InfoSpace() analysis.save_spec(spec, info_space, unit='experiment') # '@' is reserved for 'enjoy@{prepath}' os.environ['lab_mode'] = lab_mode.split('@')[0] os.environ['PREPATH'] = util.get_prepath(spec, info_space) reload(logger) # to set PREPATH properly if lab_mode == 'search': info_space.tick('experiment') Experiment(spec, info_space).run() elif lab_mode.startswith('train'): if '@' in lab_mode: prepath = lab_mode.split('@')[1] spec, info_space = util.prepath_to_spec_info_space(prepath) else: info_space.tick('trial') Trial(spec, info_space).run() elif lab_mode.startswith('enjoy'): prepath = lab_mode.split('@')[1] spec, info_space = util.prepath_to_spec_info_space(prepath) Session(spec, info_space).run() elif lab_mode.startswith('enjoy'): prepath = lab_mode.split('@')[1] spec, info_space = util.prepath_to_spec_info_space(prepath) Session(spec, info_space).run() elif lab_mode == 'dev': spec = util.override_dev_spec(spec) info_space.tick('trial') Trial(spec, info_space).run() else: logger.warn('lab_mode not recognized; must be one of `search, train, enjoy, benchmark, dev`.')
def run_trial_test(spec_file, spec_name=False): spec = spec_util.get(spec_file, spec_name) spec = util.override_test_spec(spec) info_space = InfoSpace() info_space.tick('trial') trial = Trial(spec, info_space) trial_data = trial.run() assert isinstance(trial_data, pd.DataFrame)
def __init__(self, spec, info_space=InfoSpace()): self.spec = spec if info_space.get('trial') is None: info_space.tick('trial') self.info_space = info_space self.coor, self.index = self.info_space.get_coor_idx(self) self.session_data_dict = {} self.data = None
def test_aeb_space(test_spec): global aeb_space if aeb_space is None: aeb_space = AEBSpace(test_spec, InfoSpace()) env_space = EnvSpace(test_spec, aeb_space) agent_space = AgentSpace(test_spec, aeb_space) aeb_space.init_body_space() return aeb_space
def run_benchmark(spec, const): benchmark_specs = benchmarker.generate_specs(spec, const) logger.info('Running benchmark') for spec_name, benchmark_spec in benchmark_specs.items(): # run only if not already exist; benchmark mode only if not any(spec_name in filename for filename in os.listdir('data')): info_space = InfoSpace() info_space.tick('experiment') Experiment(benchmark_spec, info_space).run()
def __init__(self, spec, info_space=None): info_space = info_space or InfoSpace() init_thread_vars(spec, info_space, unit='trial') self.spec = spec self.info_space = info_space self.coor, self.index = self.info_space.get_coor_idx(self) self.session_data_dict = {} self.data = None analysis.save_spec(spec, info_space, unit='trial') logger.info(f'Initialized trial {self.index}')
def test_prioritized_replay_memory(request): memspec = spec_util.get('base.json', 'base_prioritized_replay_memory') memspec = util.override_test_spec(memspec) aeb_mem_space = AEBSpace(memspec, InfoSpace()) env_space = EnvSpace(memspec, aeb_mem_space) aeb_mem_space.init_body_space() agent_space = AgentSpace(memspec, aeb_mem_space) agent = agent_space.agents[0] body = agent.nanflat_body_a[0] res = (body.memory, ) + request.param return res
def __init__(self, spec, info_space=InfoSpace()): self.spec = spec spec['meta']['train_mode'] = True if info_space.get('experiment') is None: info_space.tick('experiment') self.info_space = info_space self.coor, self.index = self.info_space.get_coor_idx(self) self.trial_data_dict = {} self.data = None SearchClass = getattr(search, 'RaySearch') self.search = SearchClass(self)
def run_trial_test(spec_file, spec_name=False, distributed=False): spec = spec_util.get(spec_file, spec_name) spec = util.override_test_spec(spec) info_space = InfoSpace() info_space.tick('trial') if distributed: spec['meta']['distributed'] = True if os.environ.get('CI') != 'true': # CI has not enough CPU spec['meta']['max_session'] = 2 trial = Trial(spec, info_space) trial_data = trial.run() assert isinstance(trial_data, pd.DataFrame)
def __init__(self, spec, info_space=None): info_space = info_space or InfoSpace() init_thread_vars(spec, info_space, unit='experiment') self.spec = spec self.info_space = info_space self.coor, self.index = self.info_space.get_coor_idx(self) self.trial_data_dict = {} self.data = None SearchClass = getattr(search, spec['meta'].get('search')) self.search = SearchClass(self) analysis.save_spec(spec, info_space, unit='experiment') logger.info(f'Initialized experiment {self.index}')
def prepath_to_info_space(prepath): '''Create info_space from prepath such that it returns the same prepath with spec''' from slm_lab.experiment.monitor import InfoSpace experiment_ts = prepath_to_experiment_ts(prepath) trial_index, session_index = prepath_to_idxs(prepath) # create info_space for prepath info_space = InfoSpace() info_space.experiment_ts = experiment_ts info_space.set('experiment', 0) info_space.set('trial', trial_index) info_space.set('session', session_index) return info_space
def __init__(self, spec, info_space=InfoSpace()): self.spec = spec if info_space.get('session') is None: info_space.tick('session') self.info_space = info_space self.coor, self.index = self.info_space.get_coor_idx(self) # TODO option to set rand_seed. also set np random seed self.torch_rand_seed = torch.initial_seed() self.data = None self.aeb_space = AEBSpace(self.spec, self.info_space) self.env_space = EnvSpace(self.spec, self.aeb_space) self.agent_space = AgentSpace(self.spec, self.aeb_space) logger.info(util.self_desc(self)) self.aeb_space.init_body_space() self.aeb_space.post_body_init()
def generic_algo_test(spec, algo_name): '''Need new InfoSpace() per trial otherwise session id doesn't tick correctly''' trial = Trial(spec, info_space=InfoSpace()) trial_data = trial.run() folders = [x for x in os.listdir('data/') if x.startswith(algo_name)] assert len(folders) == 1 path = 'data/' + folders[0] sess_data = util.read(path + '/' + algo_name + '_t0_s0_session_df.csv') rewards = sess_data['0.2'].replace("reward", -1).astype(float) print(f'rewards: {rewards}') maxr = rewards.max() '''Delete test data folder and trial''' shutil.rmtree(path) del trial return maxr
def mock_spec_info_space(predir, trial_index=None, session_index=None): '''Helper for retro analysis to build mock info_space and spec''' from slm_lab.experiment.monitor import InfoSpace _, _, _, spec_name, experiment_ts, _ = util.prepath_split(predir) info_space = InfoSpace() info_space.experiment_ts = experiment_ts info_space.set('experiment', 0) if trial_index is None: filepath = f'{predir}/{spec_name}_spec.json' else: info_space.set('trial', trial_index) filepath = f'{predir}/{spec_name}_t{trial_index}_spec.json' if session_index is not None: info_space.set('session', session_index) spec = util.read(filepath) return spec, info_space
def __init__(self, spec, info_space=None): info_space = info_space or InfoSpace() init_thread_vars(spec, info_space, unit='session') self.spec = spec self.info_space = info_space self.coor, self.index = self.info_space.get_coor_idx(self) # TODO option to set rand_seed. also set np random seed self.torch_rand_seed = torch.initial_seed() self.data = None self.aeb_space = AEBSpace(self.spec, self.info_space) self.env_space = EnvSpace(self.spec, self.aeb_space) self.agent_space = AgentSpace(self.spec, self.aeb_space) logger.info(util.self_desc(self)) self.aeb_space.init_body_space() self.aeb_space.post_body_init() logger.info(f'Initialized session {self.index}')
def run_new_mode(spec_file, spec_name, lab_mode): '''Run to generate new data with `search, train, dev`''' spec = spec_util.get(spec_file, spec_name) info_space = InfoSpace() analysis.save_spec(spec, info_space, unit='experiment') # first save the new spec if lab_mode == 'search': info_space.tick('experiment') Experiment(spec, info_space).run() elif lab_mode.startswith('train'): info_space.tick('trial') Trial(spec, info_space).run() elif lab_mode == 'dev': spec = spec_util.override_dev_spec(spec) info_space.tick('trial') Trial(spec, info_space).run() else: raise ValueError(f'Unrecognizable lab_mode not of {TRAIN_MODES}')
def __init__(self, spec, info_space=None): info_space = info_space or InfoSpace() init_thread_vars(spec, info_space, unit='session') self.spec = deepcopy(spec) self.info_space = info_space self.coor, self.index = self.info_space.get_coor_idx(self) self.random_seed = 100 * (info_space.get('trial') or 0) + self.index torch.cuda.manual_seed_all(self.random_seed) torch.manual_seed(self.random_seed) np.random.seed(self.random_seed) self.data = None self.aeb_space = AEBSpace(self.spec, self.info_space) self.env_space = EnvSpace(self.spec, self.aeb_space) self.agent_space = AgentSpace(self.spec, self.aeb_space) logger.info(util.self_desc(self)) self.aeb_space.init_body_space() self.aeb_space.post_body_init() logger.info(f'Initialized session {self.index}')
def run_trial_test_dist(spec_file, spec_name=False): spec = spec_util.get(spec_file, spec_name) spec = spec_util.override_test_spec(spec) info_space = InfoSpace() info_space.tick('trial') spec['meta']['distributed'] = True spec['meta']['max_session'] = 2 trial = Trial(spec, info_space) # manually run the logic to obtain global nets for testing to ensure global net gets updated global_nets = trial.init_global_nets() # only test first network if ps.is_list(global_nets): # multiagent only test first net = list(global_nets[0].values())[0] else: net = list(global_nets.values())[0] session_datas = trial.parallelize_sessions(global_nets) trial.session_data_dict = {data.index[0]: data for data in session_datas} trial_data = analysis.analyze_trial(trial) trial.close() assert isinstance(trial_data, pd.DataFrame)
def test_info_space(): return InfoSpace()
import os # NOTE increase if needed. Pytorch thread overusage https://github.com/pytorch/pytorch/issues/975 os.environ['OMP_NUM_THREADS'] = '1' from slm_lab.agent import Agent from slm_lab.env import OpenAIEnv from slm_lab.experiment import analysis from slm_lab.experiment.monitor import Body, InfoSpace, enable_aeb_space from slm_lab.lib import logger, util from slm_lab.spec import spec_util from irl_benchmark.rl.slm_session import Session spec = spec_util.get(spec_file='ppo.json', spec_name='ppo_mlp_shared_pendulum') info_space = InfoSpace() os.environ['PREPATH'] = util.get_prepath(spec, info_space) os.environ['lab_mode'] = 'training' spec['env'][0]['max_episode'] = 10 session = Session(spec, info_space) data, agent = session.run() print(data)