def run_by_mode(spec_file, spec_name, run_mode): spec = spec_util.get(spec_file, spec_name) # TODO remove when analysis can save all plotly plots os.environ['run_mode'] = run_mode if run_mode == 'search': Experiment(spec).run() elif run_mode == 'train': Trial(spec).run() elif run_mode == 'enjoy': # TODO turn on save/load model mode # Session(spec).run() pass elif run_mode == 'generate_benchmark': benchmarker.generate_specs(spec, const='agent') elif run_mode == 'benchmark': # TODO allow changing const to env run_benchmark(spec, const='agent') elif run_mode == 'dev': os.environ['PY_ENV'] = 'test' # to not save in viz spec = util.override_dev_spec(spec) Trial(spec).run() else: logger.warn( 'run_mode not recognized; must be one of `search, train, enjoy, benchmark, dev`.' )
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_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 ray_trainable(config, reporter): ''' Create an instance of a trainable function for ray: https://ray.readthedocs.io/en/latest/tune-usage.html#training-api Lab needs a spec and a trial_index to be carried through config, pass them with config in ray.run() like so: config = { 'spec': spec, 'trial_index': tune.sample_from(lambda spec: gen_trial_index()), ... # normal ray config with sample, grid search etc. } ''' import os os.environ.pop('CUDA_VISIBLE_DEVICES', None) # remove CUDA id restriction from ray from slm_lab.experiment.control import Trial # restore data carried from ray.run() config spec = config.pop('spec') spec = inject_config(spec, config) # tick trial_index with proper offset trial_index = config.pop('trial_index') spec['meta']['trial'] = trial_index - 1 spec_util.tick(spec, 'trial') # run SLM Lab trial metrics = Trial(spec).run() metrics.update(config) # carry config for analysis too # ray report to carry data in ray trial.last_result reporter(trial_data={trial_index: metrics})
def test_trial_demo(test_info_space): spec = spec_util.get('demo.json', 'dqn_cartpole') spec = spec_util.override_test_spec(spec) spec['env'][0]['save_frequency'] = 1 test_info_space.tick('trial') trial_data = Trial(spec, test_info_space).run() assert isinstance(trial_data, pd.DataFrame)
def retro_analyze_trials(predir): '''Retro-analyze all trial level datas.''' logger.info('Retro-analyzing trials from file') from slm_lab.experiment.control import Trial for filename in os.listdir(predir): if filename.endswith('_trial_data.json'): filepath = f'{predir}/{filename}' tn = filename.replace('_trial_data.json', '').split('_')[-1] trial_index = int(tn[1:]) # mock trial spec, info_space = mock_info_space_spec(predir, trial_index) trial = Trial(spec, info_space) session_data_dict = session_data_dict_from_file( predir, trial_index) trial.session_data_dict = session_data_dict trial_fitness_df = analyze_trial(trial) # write trial_data that was written from ray search fitness_vec = trial_fitness_df.iloc[0].to_dict() fitness = calc_fitness(trial_fitness_df) trial_data = util.read(filepath) trial_data.update({ **fitness_vec, 'fitness': fitness, 'trial_index': trial_index, }) util.write(trial_data, filepath)
def run_trial_test(spec_file, spec_name=False): spec = spec_util.get(spec_file, spec_name) spec = spec_util.override_test_spec(spec) spec_util.tick(spec, 'trial') trial = Trial(spec) trial_metrics = trial.run() assert isinstance(trial_metrics, dict)
def retro_analyze_trials(predir): '''Retro-analyze all trial level datas.''' logger.info('Retro-analyzing trials from file') from slm_lab.experiment.control import Trial filenames = ps.filter_(os.listdir(predir), lambda filename: filename.endswith('_trial_df.csv')) for idx, filename in enumerate(filenames): filepath = f'{predir}/{filename}' prepath = filepath.replace('_trial_df.csv', '') spec, info_space = util.prepath_to_spec_info_space(prepath) trial_index, _ = util.prepath_to_idxs(prepath) trial = Trial(spec, info_space) trial.session_data_dict = session_data_dict_from_file( predir, trial_index, ps.get(info_space, 'ckpt')) # zip only at the last zip = (idx == len(filenames) - 1) trial_fitness_df = analysis.analyze_trial(trial, zip) # write trial_data that was written from ray search trial_data_filepath = filepath.replace('_trial_df.csv', '_trial_data.json') if os.path.exists(trial_data_filepath): fitness_vec = trial_fitness_df.iloc[0].to_dict() fitness = analysis.calc_fitness(trial_fitness_df) trial_data = util.read(trial_data_filepath) trial_data.update({ **fitness_vec, 'fitness': fitness, 'trial_index': trial_index, }) util.write(trial_data, trial_data_filepath)
def test_trial_demo(): spec = spec_util.get('demo.json', 'dqn_cartpole') spec_util.save(spec, unit='experiment') spec = spec_util.override_spec(spec, 'test') spec_util.tick(spec, 'trial') trial_metrics = Trial(spec).run() assert isinstance(trial_metrics, dict)
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 analyze_eval_trial(spec, info_space, predir): '''Create a trial and run analysis to get the trial graph and other trial data''' from slm_lab.experiment.control import Trial trial = Trial(spec, info_space) trial.session_data_dict = session_data_dict_from_file( predir, trial.index, ps.get(info_space, 'ckpt')) # don't zip for eval analysis, slow otherwise analysis.analyze_trial(trial, zip=False)
def test_trial_demo(test_info_space): spec = spec_util.get('demo.json', 'dqn_cartpole') analysis.save_spec(spec, test_info_space, unit='experiment') spec = spec_util.override_test_spec(spec) spec['meta']['eval_frequency'] = 1 test_info_space.tick('trial') trial_data = Trial(spec, test_info_space).run() assert isinstance(trial_data, pd.DataFrame)
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 main(): # logger.set_level('DEBUG') spec = spec_util.get('dqn.json', 'dqn_cartpole') # spec = spec_util.get('dqn.json', 'dqn_cartpole_cartpole') # spec = spec_util.get('dqn.json', 'dqn_cartpole_multitask') # spec = spec_util.get('dqn.json', 'dqn_cartpole_cartpole_cartpole') # spec = spec_util.get('dqn.json', 'dqn_acrobot_cartpole') # spec = spec_util.get('dqn.json', 'dqn_2dball_cartpole') Trial(spec).run()
def train(self, specfile, specname): "enjoy + optimizes agent + periodic eval" logger.info( f'Running lab mode:train with specfile:{specfile} specname:{specname}' ) spec = spec_util.get(specfile, specname) # FIXME Why does this need to be in env? os.environ['lab_mode'] = 'train' spec_util.save(spec) # first save the new spec spec_util.tick(spec, 'trial') Trial(spec).run()
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 test_demo_performance(): spec = spec_util.get('demo.json', 'dqn_cartpole') spec_util.save(spec, unit='experiment') for env_spec in spec['env']: env_spec['max_frame'] = 2000 spec_util.tick(spec, 'trial') trial = Trial(spec) spec_util.tick(spec, 'session') session = Session(spec) session.run() last_reward = session.agent.body.train_df.iloc[-1]['total_reward'] assert last_reward > 50, f'last_reward is too low: {last_reward}'
def run_by_mode(spec_file, spec_name, run_mode): spec = spec_util.get(spec_file, spec_name) if run_mode == 'search': Experiment(spec).run() elif run_mode == 'train': Trial(spec).run() elif run_mode == 'enjoy': # TODO turn on save/load model mode # Session(spec).run() pass elif run_mode == 'benchmark': # TODO need to spread benchmark over spec on Experiment pass elif run_mode == 'dev': os.environ['PY_ENV'] = 'test' # to not save in viz logger.set_level('DEBUG') spec = util.override_dev_spec(spec) Trial(spec).run() else: logger.warn( 'run_mode not recognized; must be one of `search, train, enjoy, benchmark, dev`.' )
def dev(self, specfile, specname): "train + limit the number of trials & sessions. Useful for iterative development." logger.info( f'Running lab mode:dev with specfile:{specfile} specname:{specname}' ) spec = spec_util.get(specfile, specname) # FIXME Why does this need to be in env? os.environ['lab_mode'] = 'dev' spec_util.save(spec) # first save the new spec # spec = spec_util.override_dev_spec(spec) spec['meta']['max_session'] = 1 spec['meta']['max_trial'] = 2 spec_util.tick(spec, 'trial') Trial(spec).run()
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 run_spec(spec, lab_mode): '''Run a spec in lab_mode''' os.environ['lab_mode'] = lab_mode # set lab_mode spec = spec_util.override_spec(spec, lab_mode) # conditionally override spec if lab_mode in TRAIN_MODES: spec_util.save(spec) # first save the new spec if lab_mode == 'search': spec_util.tick(spec, 'experiment') Experiment(spec).run() else: spec_util.tick(spec, 'trial') Trial(spec).run() elif lab_mode in EVAL_MODES: Session(spec).run() else: raise ValueError(f'Unrecognizable lab_mode not of {TRAIN_MODES} or {EVAL_MODES}')
def run_trial_test_dist(spec_file, spec_name=False): spec = spec_util.get(spec_file, spec_name) spec = spec_util.override_spec(spec, 'test') spec_util.tick(spec, 'trial') spec['meta']['distributed'] = 'synced' spec['meta']['max_session'] = 2 trial = Trial(spec) # 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_metrics_list = trial.parallelize_sessions(global_nets) trial_metrics = analysis.analyze_trial(spec, session_metrics_list) trial.close() assert isinstance(trial_metrics, dict)
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_base(spec_file, spec_name): spec = spec_util.get(spec_file, spec_name) spec['meta']['train_mode'] = True trial = Trial(spec) trial_data = trial.run()
def run_trial_test(spec_file, spec_name): spec = spec_util.get(spec_file, spec_name) spec = util.override_test_spec(spec) trial = Trial(spec) trial_data = trial.run() assert isinstance(trial_data, pd.DataFrame)
def test_trial(test_spec): trial = Trial(test_spec) trial_data = trial.run() assert isinstance(trial_data, pd.DataFrame)
def test_algo(spec_file, spec_name): spec = spec_util.get(spec_file, spec_name) spec = util.override_test_spec(spec) trial = Trial(spec) trial_data = trial.run()
def test_session(test_spec): trial = Trial(test_spec) session = trial.init_session() yield session session.close()
def test_trial_demo(): spec = spec_util.get('demo.json', 'dqn_cartpole') spec = util.override_test_spec(spec) trial_data = Trial(spec).run() assert isinstance(trial_data, pd.DataFrame)
def test_trial(test_spec): spec_util.tick(test_spec, 'trial') spec_util.save(test_spec, unit='trial') trial = Trial(test_spec) trial_metrics = trial.run() assert isinstance(trial_metrics, dict)