Пример #1
0
    def get_callbacks(self, stage: str) -> "OrderedDict[str, Callback]":
        """Prepares the callbacks for selected stage.

        Args:
            stage: stage name

        Returns:
            dictionary with stage callbacks
        """
        callbacks = super().get_callbacks(stage=stage)
        is_callback_exists = lambda callback_fn: any(
            callback_isinstance(x, callback_fn) for x in callbacks.values()
        )
        if isinstance(self._criterion, Criterion) and not is_callback_exists(ICriterionCallback):
            callbacks["_criterion"] = CriterionCallback(
                input_key=self._output_key, target_key=self._target_key, metric_key=self._loss_key,
            )
        if isinstance(self._optimizer, Optimizer) and not is_callback_exists(IOptimizerCallback):
            callbacks["_optimizer"] = OptimizerCallback(metric_key=self._loss_key)
        if isinstance(self._scheduler, (Scheduler, ReduceLROnPlateau)) and not is_callback_exists(
            ISchedulerCallback
        ):
            callbacks["_scheduler"] = SchedulerCallback(
                loader_key=self._valid_loader, metric_key=self._valid_metric
            )
        return callbacks
Пример #2
0
 def get_callbacks(self) -> "OrderedDict[str, Callback]":
     """Returns the callbacks for the experiment."""
     callbacks = sort_callbacks_by_order(super().get_callbacks())
     callback_exists = lambda callback_fn: any(
         callback_isinstance(x, callback_fn) for x in callbacks.values())
     if isinstance(
             self._criterion,
             TorchCriterion) and not callback_exists(ICriterionCallback):
         callbacks["_criterion"] = CriterionCallback(
             input_key=self._output_key,
             target_key=self._target_key,
             metric_key=self._loss_key,
         )
     if isinstance(
             self._optimizer,
             TorchOptimizer) and not callback_exists(IBackwardCallback):
         callbacks["_backward"] = BackwardCallback(
             metric_key=self._loss_key)
     if isinstance(
             self._optimizer,
             TorchOptimizer) and not callback_exists(IOptimizerCallback):
         callbacks["_optimizer"] = OptimizerCallback(
             metric_key=self._loss_key)
     if isinstance(
             self._scheduler,
             TorchScheduler) and not callback_exists(ISchedulerCallback):
         callbacks["_scheduler"] = SchedulerCallback(
             loader_key=self._valid_loader, metric_key=self._valid_metric)
     return callbacks
Пример #3
0
    def get_callbacks(self) -> "OrderedDict[str, Callback]":
        """Prepares the callbacks for selected stage.

        Args:
            stage: stage name

        Returns:
            dictionary with stage callbacks
        """
        callbacks = super().get_callbacks()
        callback_exists = lambda callback_fn: any(
            callback_isinstance(x, callback_fn) for x in callbacks.values())
        if isinstance(
                self._criterion,
                TorchCriterion) and not callback_exists(ICriterionCallback):
            callbacks["_criterion"] = CriterionCallback(
                input_key=f"{self.loss_mode_prefix}_left",
                target_key=f"{self.loss_mode_prefix}_right",
                metric_key=self._loss_key,
            )
        if isinstance(
                self._optimizer,
                TorchOptimizer) and not callback_exists(IBackwardCallback):
            callbacks["_backward"] = BackwardCallback(
                metric_key=self._loss_key)
        if isinstance(
                self._optimizer,
                TorchOptimizer) and not callback_exists(IOptimizerCallback):
            callbacks["_optimizer"] = OptimizerCallback(
                metric_key=self._loss_key)
        if isinstance(
                self._scheduler,
                TorchScheduler) and not callback_exists(ISchedulerCallback):
            callbacks["_scheduler"] = SchedulerCallback(
                loader_key=self._valid_loader, metric_key=self._valid_metric)
        return callbacks