Runs a given LBANN model once on the given inputs and returns its forward propagation outputs. All tensors are numpy arrays. :param model: The LBANN model or layer graph to evaluate. :param inputs: A tensor with the inputs to the model. It will be mapped to the `
(
model: Union[lbann.Model, List[lbann.Layer]],
inputs: npt.NDArray,
outputs: Optional[List[str]] = None,
extra_callbacks: Optional[List[lbann.Callback]] = None,
training: bool = False,
**kwargs,
)
| 38 | |
| 39 | |
| 40 | def evaluate( |
| 41 | model: Union[lbann.Model, List[lbann.Layer]], |
| 42 | inputs: npt.NDArray, |
| 43 | outputs: Optional[List[str]] = None, |
| 44 | extra_callbacks: Optional[List[lbann.Callback]] = None, |
| 45 | training: bool = False, |
| 46 | **kwargs, |
| 47 | ) -> Union[npt.NDArray, Tuple[npt.NDArray]]: |
| 48 | """ |
| 49 | Runs a given LBANN model once on the given inputs and returns its forward |
| 50 | propagation outputs. All tensors are numpy arrays. |
| 51 | |
| 52 | :param model: The LBANN model or layer graph to evaluate. |
| 53 | :param inputs: A tensor with the inputs to the model. It will be mapped |
| 54 | to the ``lbann.Input`` with the ``samples`` data field. |
| 55 | :param outputs: An optional list of layer names to output as the return |
| 56 | value. If not given, returns all layers without children. |
| 57 | :param extra_callbacks: If given, uses additional callbacks in the evaluated |
| 58 | model. |
| 59 | :param training: If True, evaluates in training mode. Otherwise, evaluates |
| 60 | in testing mode. |
| 61 | :param kwargs: Additional keyword arguments to pass onto ``lbann.run`` |
| 62 | :return: Output tensor or tensors of the LBANN model. |
| 63 | """ |
| 64 | |
| 65 | ######################## |
| 66 | # Canonicalize arguments |
| 67 | |
| 68 | # Set model to always be an lbann.Model |
| 69 | if isinstance(model, (list, tuple, set, lbann.Layer)): |
| 70 | if isinstance(model, lbann.Layer): |
| 71 | model = [model] |
| 72 | model = lbann.Model(0, model) |
| 73 | |
| 74 | # Obtain outputs if not given |
| 75 | if not outputs: |
| 76 | outputs = [l.name for l in model.layers if not l.children] |
| 77 | |
| 78 | extra_callbacks = extra_callbacks or [] |
| 79 | ##################### |
| 80 | if 'job_name' not in kwargs: |
| 81 | kwargs['job_name'] = 'lbann_evaluate' |
| 82 | |
| 83 | workdir = make_timestamped_work_dir(**kwargs) |
| 84 | fmt = 'npy' if lbann.has_feature('CNPY') else 'csv' |
| 85 | |
| 86 | # Reset fields for evaluation |
| 87 | old_epochs = model.epochs |
| 88 | old_callbacks = model.callbacks |
| 89 | old_metrics = model.metrics |
| 90 | |
| 91 | try: |
| 92 | model.epochs = 1 if training else 0 |
| 93 | model.callbacks = [ |
| 94 | lbann.CallbackDumpOutputs( |
| 95 | batch_interval=1, |
| 96 | execution_modes='train' if training else 'test', |
| 97 | directory=workdir, |
no test coverage detected