def __init__( self, input_key: Union[str, List[str], Dict[str, str]] = "targets", output_key: Union[str, List[str], Dict[str, str]] = "logits", prefix: str = "loss", criterion_key: str = None, multiplier: float = 1.0 ): """ Args: input_key (Union[str, List[str], Dict[str, str]]): key/list/dict of keys that takes values from the input dictionary If None, the whole input will be passed to the criterion. output_key (Union[str, List[str], Dict[str, str]]): key/list/dict of keys that takes values from the input dictionary If None, the whole output will be passed to the criterion. prefix (str): prefix for metrics and output key for loss in ``state.loss`` dictionary criterion_key (str): A key to take a criterion in case there are several of them and they are in a dictionary format. multiplier (float): scale factor for the output loss. """ super().__init__(CallbackOrder.Criterion) self.input_key = input_key self.output_key = output_key self.prefix = prefix self.criterion_key = criterion_key self.multiplier = multiplier self._get_input = utils.get_dictkey_auto_fn(self.input_key) self._get_output = utils.get_dictkey_auto_fn(self.output_key) kv_types = (dict, tuple, list, type(None)) # @TODO: fix to only KV usage if hasattr(self, "_compute_loss"): pass # overridden in descendants elif isinstance(self.input_key, str) \ and isinstance(self.output_key, str): self._compute_loss = self._compute_loss_value elif isinstance(self.input_key, kv_types) \ and isinstance(self.output_key, kv_types): self._compute_loss = self._compute_loss_key_value else: raise NotImplementedError()
def __init__( self, prefix: str, input_key: Union[str, List[str], Dict[str, str]] = "targets", output_key: Union[str, List[str], Dict[str, str]] = "logits", multiplier: float = 1.0, **metrics_kwargs, ): super().__init__(order=CallbackOrder.Metric, node=CallbackNode.All) self.prefix = prefix # self.metric_fn = partial(metric_fn, **metric_params) self.input_key = input_key self.output_key = output_key self.multiplier = multiplier self.metrics_kwargs = metrics_kwargs self._get_input = utils.get_dictkey_auto_fn(self.input_key) self._get_output = utils.get_dictkey_auto_fn(self.output_key) kv_types = (dict, tuple, list, type(None)) is_value_input = ( isinstance(self.input_key, str) and self.input_key != "__all__" ) is_value_output = ( isinstance(self.output_key, str) and self.output_key != "__all__" ) is_kv_input = ( isinstance(self.input_key, kv_types) or self.input_key == "__all__" ) is_kv_output = ( isinstance(self.output_key, kv_types) or self.output_key == "__all__" ) # @TODO: fix to only KV usage if hasattr(self, "_compute_metric"): pass # overridden in descendants elif is_value_input and is_value_output: self._compute_metric = self._compute_metric_value elif is_kv_input and is_kv_output: self._compute_metric = self._compute_metric_key_value else: raise NotImplementedError()
def __init__(self, batch_transform: Dict[str, str], transform_in_key: Union[str, List[str], Dict[str, str]], transform_out_key: str = None, suffixes: List[str] = None): super().__init__(order=CallbackOrder.Internal) self.transform_fn = get_batch_transform(batch_transform) self._get_input_key = get_dictkey_auto_fn(transform_in_key) self.transform_in_key = transform_in_key self.transform_out_key = transform_out_key self.suffixes = suffixes or []
def __init__(self, prefix: str, metric: Union[str, Dict[str, str]], memory_key: Union[str, List[str], Dict[str, str]], multiplier: float = 1.0, **metric_kwargs): metric_fn = get_metric(metric) self.memory_key = memory_key self._get_memory = get_dictkey_auto_fn(self.memory_key) if isinstance(self.memory_key, str): self._compute_metric = self._compute_metric_value elif isinstance(self.memory_key, (list, tuple, dict)): self._compute_metric = self._compute_metric_key_value else: raise NotImplementedError() super().__init__(prefix=prefix, metric_fn=metric_fn, multiplier=multiplier, **metric_kwargs)