def create_hooks(config: dict, model: AbstractModel,
                 dataset: AbstractDataset, output_dir: str) -> Iterable[AbstractHook]:
    """
    Create hooks specified in ``config['hooks']`` list.

    Hook config entries may be one of the following types:

    .. code-block:: yaml
        :caption: A hook with default args specified only by its name as a string; e.g.

        hooks:
          - LogVariables
          - cxflow_tensorflow.WriteTensorBoard

    .. code-block:: yaml
        :caption: A hook with custom args as a dict name -> args; e.g.

        hooks:
          - StopAfter:
              n_epochs: 10

    :param config: config dict
    :param model: model object to be passed to the hooks
    :param dataset: dataset object to be passed to hooks
    :param output_dir: training output dir available to the hooks
    :return: list of hook objects
    """
    logging.info('Creating hooks')
    hooks = []
    if 'hooks' in config:
        for hook_config in config['hooks']:
            if isinstance(hook_config, str):
                hook_config = {hook_config: {}}
            assert len(hook_config) == 1, 'Hook configuration must have exactly one key (fully qualified name).'

            hook_path, hook_params = next(iter(hook_config.items()))
            if hook_params is None:
                logging.warning('\t\t Empty config of `%s` hook', hook_path)
                hook_params = {}

            # workaround for ruamel.yaml expansion bug; see #222
            hook_params = dict(hook_params.items())

            hook_module, hook_class = parse_fully_qualified_name(hook_path)
            # find the hook module if not specified
            if hook_module is None:
                hook_module = get_class_module(CXF_HOOKS_MODULE, hook_class)
                logging.debug('\tFound hook module `%s` for class `%s`', hook_module, hook_class)
                if hook_module is None:
                    raise ValueError('Can`t find hook module for hook class `{}`. '
                                     'Make sure it is defined under `{}` sub-modules.'
                                     .format(hook_class, CXF_HOOKS_MODULE))
            # create hook kwargs
            hook_kwargs = {'dataset': dataset, 'model': model, 'output_dir': output_dir, **hook_params}

            # create new hook
            try:
                hook = create_object(hook_module, hook_class, kwargs=hook_kwargs)
                hooks.append(hook)
                logging.info('\t%s created', type(hooks[-1]).__name__)
            except (ValueError, KeyError, TypeError, NameError, AttributeError, AssertionError, ImportError) as ex:
                logging.error('\tFailed to create a hook from config `%s`', hook_config)
                raise ex
    return hooks
def create_hooks ( config : dict , model : AbstractModel , dataset : AbstractDataset , output_dir : str ) -> Iterable [ AbstractHook ] : logging . info ( 'Creating hooks' ) hooks = [ ] if 'hooks' in config : for hook_config in config [ 'hooks' ] : if isinstance ( hook_config , str ) : hook_config = { hook_config : { } } assert len ( hook_config ) == 1 , 'Hook configuration must have exactly one key (fully qualified name).' hook_path , hook_params = next ( iter ( hook_config . items ( ) ) ) if hook_params is None : logging . warning ( '\t\t Empty config of `%s` hook' , hook_path ) hook_params = { } hook_params = dict ( hook_params . items ( ) ) hook_module , hook_class = parse_fully_qualified_name ( hook_path ) if hook_module is None : hook_module = get_class_module ( CXF_HOOKS_MODULE , hook_class ) logging . debug ( '\tFound hook module `%s` for class `%s`' , hook_module , hook_class ) if hook_module is None : raise ValueError ( 'Can`t find hook module for hook class `{}`. ' 'Make sure it is defined under `{}` sub-modules.' . format ( hook_class , CXF_HOOKS_MODULE ) ) hook_kwargs = { 'dataset' : dataset , 'model' : model , 'output_dir' : output_dir , ** hook_params } try : hook = create_object ( hook_module , hook_class , kwargs = hook_kwargs ) hooks . append ( hook ) logging . info ( '\t%s created' , type ( hooks [ - 1 ] ) . __name__ ) except ( ValueError , KeyError , TypeError , NameError , AttributeError , AssertionError , ImportError ) as ex : logging . error ( '\tFailed to create a hook from config `%s`' , hook_config ) raise ex return hooks
Create hooks specified in ``config['hooks']`` list.

    Hook config entries may be one of the following types:

    .. code-block:: yaml
        :caption: A hook with default args specified only by its name as a string; e.g.

        hooks:
          - LogVariables
          - cxflow_tensorflow.WriteTensorBoard

    .. code-block:: yaml
        :caption: A hook with custom args as a dict name -> args; e.g.

        hooks:
          - StopAfter:
              n_epochs: 10

    :param config: config dict
    :param model: model object to be passed to the hooks
    :param dataset: dataset object to be passed to hooks
    :param output_dir: training output dir available to the hooks
    :return: list of hook objects