class TrainingRuns(Mapping): """A map from integers to TrainingRuns.""" def __init__(self, root_dir, src_dir, run_factory, check_commit=True): """Create TrainingRuns object. Args: root_dir (str): directory where all training run data will be stored src_dir (str): a Git repository path (used to check commits) run_factory (Callable[[Config, str], TrainingRun]): a Callable, which takes a Config and a save_dir as arguments, and creates a new TrainingRun. check_commit (bool): if True, checks that current working directory is on same commit as when the run was originally created. """ self._int_dirs = IntegerDirectories(root_dir) self._src_dir = src_dir self._run_factory = run_factory self._check_commit = check_commit def _config_path(self, save_dir): return join(save_dir, 'config.txt') def __getitem__(self, i): """Reload an existing TrainingRun.""" save_dir = self._int_dirs[i] config = Config.from_file(self._config_path(save_dir)) run = self._run_factory(config, save_dir) if self._check_commit: run.match_commit(self._src_dir) logging.info('Reloaded TrainingRun #{}'.format(i)) return run def new(self, config, name=None): """Create a new TrainingRun.""" print('TrainingRun configuration:\n{}'.format(config)) save_dir = self._int_dirs.new_dir(name=name) cfg_path = self._config_path(save_dir) config.to_file(cfg_path) # save the config run = self._run_factory(config, save_dir) try: run.record_commit(self._src_dir) run.dump_diff(self._src_dir) except InvalidGitRepositoryError: print('WARNING: could not obtain Git information.') run.metadata[ 'config'] = config._config_tree # save config in metadata, for programmatic access print('New TrainingRun created at: {}'.format(run.workspace.root)) return run def __iter__(self): return iter(self._int_dirs) def __len__(self): return len(self._int_dirs) def paths(self): return list(self._int_dirs.values())
def __init__(self, root_dir, src_dir, run_factory, check_commit=True): """Create TrainingRuns object. Args: root_dir (str): directory where all training run data will be stored src_dir (str): a Git repository path (used to check commits) run_factory (Callable[[Config, str], TrainingRun]): a Callable, which takes a Config and a save_dir as arguments, and creates a new TrainingRun. check_commit (bool): if True, checks that current working directory is on same commit as when the run was originally created. """ self._int_dirs = IntegerDirectories(root_dir) self._src_dir = src_dir self._run_factory = run_factory self._check_commit = check_commit
def load_trace_groups(run_num): """Load traces for a particular TrainingRun. Returns: trace_groups (dict[str, Traces]): map from trace type to Traces """ run_dirs = IntegerDirectories(data.workspace.experiments) traces_dir = join(run_dirs[run_num], 'traces') trace_groups = {} for trace_type in TRACE_TYPES: trace_dir = join(traces_dir, trace_type) filenames = os.listdir(trace_dir) train_step_to_trace = {} for full_name in filenames: name, ext = splitext(full_name) if ext != '.json': continue full_path = join(trace_dir, full_name) train_step = int(name) with open(full_path, 'r') as f: trace = json.load(f) train_step_to_trace[train_step] = trace trace_groups[trace_type] = Traces(train_step_to_trace) return trace_groups
def int_dirs(self, tmpdir): tmpdir.mkdir('152_blah') tmpdir.mkdir('153_woo') tmpdir.mkdir('1_') # no suffix, should still match tmpdir.mkdir('-1') # no suffix, should still match tmpdir.mkdir('_10') # prefix is not integer, ignore tmpdir.mkdir('.DS_Store') tmpdir.mkdir('other') return IntegerDirectories(str(tmpdir))
def __init__(self, root_dir, src_dir, experiment_factory, default_config_path, check_commit=True): """Create Experiments object. Args: root_dir (str): directory where all experiment data will be stored src_dir (str): a Git repository path (used to check commits) experiment_factory (Callable[[Config, str], Experiment]): a Callable, which takes a Config and a save_dir as arguments, and creates a new Experiment. default_config_path (str): path to a default config, to be used when no config is specified check_commit (bool): if True, checks that current working directory is on same commit as when the experiment was originally created. """ self._int_dirs = IntegerDirectories(root_dir) self._src_dir = src_dir self._exp_factory = experiment_factory self._check_commit = check_commit self._default_config_path = default_config_path
parser = argparse.ArgumentParser() parser.add_argument('-p', '--port', type=int, default=6006) parser.add_argument('-m', '--model-file', default='') parser.add_argument('-s', '--config-strings', action='append', default=[]) parser.add_argument('config_paths', nargs='+') args = parser.parse_args() set_log_level('WARNING') # create run config_strings = [] config_paths = args.config_paths if len(config_paths) == 1 and config_paths[0].isdigit(): # Get configs from a run run_dirname = IntegerDirectories(data.workspace.experiments)[int( config_paths[0])] with open(join(run_dirname, 'config.txt')) as fin: config_strings.append(fin.read()) else: # Merge strings to allow object overwites run_dirname = None for filename in config_paths: with open(filename) as fin: config_strings.append(fin.read()) for config_string in args.config_strings: config_strings.append(config_string) config = Config.from_str('\n'.join(config_strings)) eval_run = PhraseNodeEvalRun(config) # load model
class Experiments(Mapping): """A map from integers to Experiments.""" def __init__(self, root_dir, src_dir, experiment_factory, default_config_path, check_commit=True): """Create Experiments object. Args: root_dir (str): directory where all experiment data will be stored src_dir (str): a Git repository path (used to check commits) experiment_factory (Callable[[Config, str], Experiment]): a Callable, which takes a Config and a save_dir as arguments, and creates a new Experiment. default_config_path (str): path to a default config, to be used when no config is specified check_commit (bool): if True, checks that current working directory is on same commit as when the experiment was originally created. """ self._int_dirs = IntegerDirectories(root_dir) self._src_dir = src_dir self._exp_factory = experiment_factory self._check_commit = check_commit self._default_config_path = default_config_path def _config_path(self, save_dir): return join(save_dir, 'config.txt') def __getitem__(self, i): """Reload an existing Experiment.""" save_dir = self._int_dirs[i] config = Config.from_file(self._config_path(save_dir)) exp = self._exp_factory(config, save_dir) if self._check_commit: exp.match_commit(self._src_dir) logging.info('Reloaded experiment #{}'.format(i)) return exp def new(self, config=None, name=None): """Create a new Experiment.""" if config is None: config = Config.from_file(self._default_config_path) save_dir = self._int_dirs.new_dir(name=name) cfg_path = self._config_path(save_dir) config.to_file(cfg_path) # save the config exp = self._exp_factory(config, save_dir) exp.record_commit(self._src_dir) logging.info('New experiment created at: {}'.format( exp.workspace.root)) logging.info('Experiment configuration:\n{}'.format(config)) return exp def __iter__(self): return iter(self._int_dirs) def __len__(self): return len(self._int_dirs) def paths(self): return self._int_dirs.values()