def run_experiment_here( experiment_function, variant, seed=None, use_gpu=True, gpu_id=0, # Logger params: snapshot_mode='last', snapshot_gap=1, force_randomize_seed=False, log_dir=None, ): """ Run an experiment locally without any serialization. :param experiment_function: Function. `variant` will be passed in as its only argument. :param exp_prefix: Experiment prefix for the save file. :param variant: Dictionary passed in to `experiment_function`. :param exp_id: Experiment ID. Should be unique across all experiments. Note that one experiment may correspond to multiple seeds,. :param seed: Seed used for this experiment. :param use_gpu: Run with GPU. By default False. :param script_name: Name of the running script :param log_dir: If set, set the log directory to this. Otherwise, the directory will be auto-generated based on the exp_prefix. :return: """ torch.set_num_threads(1) if force_randomize_seed or seed is None: seed = random.randint(0, 100000) variant['seed'] = str(seed) log_dir = variant['log_dir'] # The logger's default mode is to # append to the text file if the file already exists # So this would not override and erase any existing # log file in the same log dir. logger.reset() setup_logger( snapshot_mode=snapshot_mode, snapshot_gap=snapshot_gap, log_dir=log_dir, ) # Assume this file is at the top level of the repo git_infos = get_git_infos([osp.dirname(__file__)]) run_experiment_here_kwargs = dict( variant=variant, seed=seed, use_gpu=use_gpu, snapshot_mode=snapshot_mode, snapshot_gap=snapshot_gap, git_infos=git_infos, ) exp_setting = dict( run_experiment_here_kwargs=run_experiment_here_kwargs ) exp_setting_pkl_path = osp.join(log_dir, 'experiment.pkl') # Check if existing result exists prev_exp_state = None if osp.isfile(exp_setting_pkl_path): # Sanity check to make sure the experimental setting # of the saved data and the current experiment run is the same prev_exp_setting = load_pkl(exp_setting_pkl_path) logger.log(f'Log dir is not empty: {os.listdir(log_dir)}') if prev_exp_setting != exp_setting: logger.log("""Previous experimental setting is not the same as the current experimental setting. Very risky to try to reload the previous state. Exitting""") logger.log(f'Previous: {prev_exp_setting}') logger.log(f'Current: {exp_setting}') exit(1) try: prev_exp_state = load_gzip_pickle( osp.join(log_dir, 'params.zip_pkl')) logger.log('Trying to restore the state of the experiment program') except FileNotFoundError: logger.log("""There is no previous experiment state available. Do not try to restore.""") prev_exp_state = None # Log the variant logger.log("Variant:") logger.log(json.dumps(dict_to_safe_json(variant), indent=2)) variant_log_path = osp.join(log_dir, 'variant.json') logger.log_variant(variant_log_path, variant) # Save the current experimental setting dump_pkl(exp_setting_pkl_path, exp_setting) log_git_infos(git_infos, log_dir) logger.log(f'Seed: {seed}') set_seed(seed) logger.log(f'Using GPU: {use_gpu}') set_gpu_mode(use_gpu, gpu_id) return experiment_function(variant, prev_exp_state)
def create_simple_exp_name(): """ Create a unique experiment name with a timestamp """ now = datetime.datetime.now(dateutil.tz.tzlocal()) timestamp = now.strftime('%Y_%m_%d_%H_%M_%S') return timestamp std_threshold = 0.1 in_mdp_batch_size = 128 eval_statistics = OrderedDict() logger.reset() setup_logger( log_dir=osp.join('./tune_threshold_loggings', create_simple_exp_name())) filename = f'./goals/ant-dir-normal-goals.pkl' train_goals, wd_goals, ood_goals = pickle.load(open(filename, 'rb')) env = env_producer('ant-dir', 0, train_goals[0]) for epoch in range(200): file_name = osp.join('./data_reward_predictions', f'params_{epoch}.pkl') params = pickle.load(open(file_name, "rb")) obs = params['obs'] actions = params['actions']