Skip to content

run

configure_run_parser

Add a run parser to an existing argparser.

Parameters:

Name Type Description Default
subparsers _SubParsersAction

The parser object to be appended to.

required
Source code in fastestimator/fastestimator/cli/run.py
def configure_run_parser(subparsers: argparse._SubParsersAction) -> None:
    """Add a run parser to an existing argparser.

    Args:
        subparsers: The parser object to be appended to.
    """
    parser = subparsers.add_parser('run',
                                   description='Execute fastestimator_run function',
                                   formatter_class=argparse.ArgumentDefaultsHelpFormatter,
                                   allow_abbrev=False)
    # use an argument group for required flag arguments since otherwise they will show up as optional in the help
    parser.add_argument('entry_point', type=str, help='The path to the python file')
    parser.add_argument('--hyperparameters',
                        dest='hyperparameters_json',
                        type=str,
                        help="The path to the hyperparameters JSON file")
    parser.add_argument('--warmup',
                        type=literal_eval,
                        help="Warmup setting, can be True or False",
                        choices=[True, False],
                        default=True)
    parser.add_argument('--eager',
                        type=literal_eval,
                        help="Eager setting, can be True or False",
                        choices=[True, False],
                        default=False)
    parser.add_argument('--summary', type=str, help="Experiment name", default=None)
    parser.add_argument_group(
        'hyperparameter arguments',
        'Arguments to be passed through to the fastestimator_run() call. \
        Examples might look like --epochs <int>, --batch_size <int>, --optimizer <str>, etc...')
    parser.set_defaults(func=run)

run

Invoke the fastestimator_run function from a file.

Parameters:

Name Type Description Default
args Dict[str, Any]

A dictionary containing location of the FE file under the 'entry_point' key, as well as an optional 'hyperparameters_json' key if the user is storing their parameters in a file.

required
unknown Optional[List[str]]

The remainder of the command line arguments to be passed along to the fastestimator_run() method.

required
Source code in fastestimator/fastestimator/cli/run.py
def run(args: Dict[str, Any], unknown: Optional[List[str]]) -> None:
    """Invoke the fastestimator_run function from a file.

    Args:
        args: A dictionary containing location of the FE file under the 'entry_point' key, as well as an optional
            'hyperparameters_json' key if the user is storing their parameters in a file.
        unknown: The remainder of the command line arguments to be passed along to the fastestimator_run() method.
    """
    entry_point = args['entry_point']
    hyperparameters = {}
    if args['hyperparameters_json']:
        hyperparameters = os.path.abspath(args['hyperparameters_json'])
        with open(hyperparameters, 'r') as f:
            hyperparameters = json.load(f)
    hyperparameters.update(parse_cli_to_dictionary(unknown))
    module_name = os.path.splitext(os.path.basename(entry_point))[0]
    dir_name = os.path.abspath(os.path.dirname(entry_point))
    sys.path.insert(0, dir_name)
    spec_module = __import__(module_name, globals(), locals())
    if hasattr(spec_module, "fastestimator_run"):
        spec_module.fastestimator_run(**hyperparameters)
    elif hasattr(spec_module, "get_estimator"):
        est = spec_module.get_estimator(**hyperparameters)
        if "train" in est.pipeline.data:
            est.fit(summary=args['summary'], warmup=args['warmup'], eager=args['eager'])
        if "test" in est.pipeline.data:
            est.test(summary=args['summary'], eager=args['eager'])
    else:
        raise ValueError("The file {} does not have 'fastestimator_run' or 'get_estimator' defined".format(module_name))