Core¶
-
class
catalyst.core.callback.
CallbackOrder
[source]¶ Bases:
enum.IntFlag
An enumeration.
-
External
= 200¶
-
Internal
= 0¶
-
Logging
= 100¶
-
Metric
= 20¶
-
MetricAggregation
= 40¶
-
Optimizer
= 60¶
-
Scheduler
= 120¶
-
Validation
= 80¶
-
-
class
catalyst.core.callback.
CallbackNode
[source]¶ Bases:
enum.IntFlag
An enumeration.
-
All
= 0¶
-
Master
= 1¶
-
Worker
= 2¶
-
-
class
catalyst.core.callback.
Callback
(order: int, node: int = <CallbackNode.All: 0>, type: int = <CallbackType.Stage: 0>)[source]¶ Bases:
object
Abstract class that all callback (e.g., Logger) classes extends from. Must be extended before usage.
usage example:
-- stage start ---- epoch start (one epoch - one run of every loader) ------ loader start -------- batch start -------- batch handler -------- batch end ------ loader end ---- epoch end -- stage end exception – if an Exception was raised
All callbacks has
order
value fromCallbackOrder
andnode
value fromCallbackNode
-
catalyst.core.registry.
Callback
(factory: Union[Type, Callable[[...], Any]] = None, *factories: Union[Type, Callable[[...], Any]], name: str = None, **named_factories: Union[Type, Callable[[...], Any]]) → Union[Type, Callable[[...], Any]]¶ Adds factory to registry with it’s
__name__
attribute or provided name. Signature is flexible.- Parameters
factory – Factory instance
factories – More instances
name – Provided name for first instance. Use only when pass single instance.
named_factories – Factory and their names as kwargs
- Returns
First factory passed
- Return type
(Factory)
-
catalyst.core.registry.
Criterion
(factory: Union[Type, Callable[[...], Any]] = None, *factories: Union[Type, Callable[[...], Any]], name: str = None, **named_factories: Union[Type, Callable[[...], Any]]) → Union[Type, Callable[[...], Any]]¶ Adds factory to registry with it’s
__name__
attribute or provided name. Signature is flexible.- Parameters
factory – Factory instance
factories – More instances
name – Provided name for first instance. Use only when pass single instance.
named_factories – Factory and their names as kwargs
- Returns
First factory passed
- Return type
(Factory)
-
catalyst.core.registry.
Optimizer
(factory: Union[Type, Callable[[...], Any]] = None, *factories: Union[Type, Callable[[...], Any]], name: str = None, **named_factories: Union[Type, Callable[[...], Any]]) → Union[Type, Callable[[...], Any]]¶ Adds factory to registry with it’s
__name__
attribute or provided name. Signature is flexible.- Parameters
factory – Factory instance
factories – More instances
name – Provided name for first instance. Use only when pass single instance.
named_factories – Factory and their names as kwargs
- Returns
First factory passed
- Return type
(Factory)
-
catalyst.core.registry.
Scheduler
(factory: Union[Type, Callable[[...], Any]] = None, *factories: Union[Type, Callable[[...], Any]], name: str = None, **named_factories: Union[Type, Callable[[...], Any]]) → Union[Type, Callable[[...], Any]]¶ Adds factory to registry with it’s
__name__
attribute or provided name. Signature is flexible.- Parameters
factory – Factory instance
factories – More instances
name – Provided name for first instance. Use only when pass single instance.
named_factories – Factory and their names as kwargs
- Returns
First factory passed
- Return type
(Factory)
-
catalyst.core.registry.
Module
(factory: Union[Type, Callable[[...], Any]] = None, *factories: Union[Type, Callable[[...], Any]], name: str = None, **named_factories: Union[Type, Callable[[...], Any]]) → Union[Type, Callable[[...], Any]]¶ Adds factory to registry with it’s
__name__
attribute or provided name. Signature is flexible.- Parameters
factory – Factory instance
factories – More instances
name – Provided name for first instance. Use only when pass single instance.
named_factories – Factory and their names as kwargs
- Returns
First factory passed
- Return type
(Factory)
-
catalyst.core.registry.
Model
(factory: Union[Type, Callable[[...], Any]] = None, *factories: Union[Type, Callable[[...], Any]], name: str = None, **named_factories: Union[Type, Callable[[...], Any]]) → Union[Type, Callable[[...], Any]]¶ Adds factory to registry with it’s
__name__
attribute or provided name. Signature is flexible.- Parameters
factory – Factory instance
factories – More instances
name – Provided name for first instance. Use only when pass single instance.
named_factories – Factory and their names as kwargs
- Returns
First factory passed
- Return type
(Factory)
-
catalyst.core.registry.
Sampler
(factory: Union[Type, Callable[[...], Any]] = None, *factories: Union[Type, Callable[[...], Any]], name: str = None, **named_factories: Union[Type, Callable[[...], Any]]) → Union[Type, Callable[[...], Any]]¶ Adds factory to registry with it’s
__name__
attribute or provided name. Signature is flexible.- Parameters
factory – Factory instance
factories – More instances
name – Provided name for first instance. Use only when pass single instance.
named_factories – Factory and their names as kwargs
- Returns
First factory passed
- Return type
(Factory)
-
catalyst.core.registry.
Transform
(factory: Union[Type, Callable[[...], Any]] = None, *factories: Union[Type, Callable[[...], Any]], name: str = None, **named_factories: Union[Type, Callable[[...], Any]]) → Union[Type, Callable[[...], Any]]¶ Adds factory to registry with it’s
__name__
attribute or provided name. Signature is flexible.- Parameters
factory – Factory instance
factories – More instances
name – Provided name for first instance. Use only when pass single instance.
named_factories – Factory and their names as kwargs
- Returns
First factory passed
- Return type
(Factory)
Callbacks¶
-
class
catalyst.core.callbacks.checkpoint.
CheckpointCallback
(save_n_best: int = 1, resume: str = None, resume_dir: str = None, metrics_filename: str = '_metrics.json')[source]¶ Bases:
catalyst.core.callbacks.checkpoint.BaseCheckpointCallback
Checkpoint callback to save/restore your model/criterion/optimizer/metrics.
-
__init__
(save_n_best: int = 1, resume: str = None, resume_dir: str = None, metrics_filename: str = '_metrics.json')[source]¶ - Parameters
save_n_best (int) – number of best checkpoint to keep
resume (str) – path to checkpoint to load and initialize runner state
metrics_filename (str) – filename to save metrics in checkpoint folder. Must ends on
.json
or.yml
-
-
class
catalyst.core.callbacks.checkpoint.
IterationCheckpointCallback
(save_n_last: int = 1, period: int = 100, stage_restart: bool = True, metrics_filename: str = '_metrics_iter.json')[source]¶ Bases:
catalyst.core.callbacks.checkpoint.BaseCheckpointCallback
Iteration checkpoint callback to save your model/criterion/optimizer
-
__init__
(save_n_last: int = 1, period: int = 100, stage_restart: bool = True, metrics_filename: str = '_metrics_iter.json')[source]¶ - Parameters
save_n_last (int) – number of last checkpoint to keep
period (int) – save the checkpoint every period
stage_restart (bool) – restart counter every stage or not
metrics_filename (str) – filename to save metrics in checkpoint folder. Must ends on
.json
or.yml
-
-
class
catalyst.core.callbacks.criterion.
CriterionCallback
(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, **metric_kwargs)[source]¶ Bases:
catalyst.core.callbacks.metrics._MetricCallback
Callback for that measures loss with specified criterion.
-
__init__
(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, **metric_kwargs)[source]¶ - Parameters
input_key (Union[str, List[str], Dict[str, str]]) – key/list/dict of keys that takes values from the input dictionary If ‘__all__’, the whole input will be passed to the criterion If None, empty dict 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 ‘__all__’, the whole output will be passed to the criterion If None, empty dict will be passed to the criterion.
prefix (str) – prefix for metrics and output key for loss in
state.batch_metrics
dictionarycriterion_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.
-
property
metric_fn
¶
-
-
class
catalyst.core.callbacks.formatters.
MetricsFormatter
(message_prefix)[source]¶ Bases:
abc.ABC
,logging.Formatter
Abstract metrics formatter
-
class
catalyst.core.callbacks.formatters.
TxtMetricsFormatter
[source]¶ Bases:
catalyst.core.callbacks.formatters.MetricsFormatter
Translate batch metrics in human-readable format.
This class is used by
logging.Logger
to make a string from record. For details refer to official docs for ‘logging’ module.Note
This is inner class used by Logger callback, no need to use it directly!
-
class
catalyst.core.callbacks.formatters.
JsonMetricsFormatter
[source]¶ Bases:
catalyst.core.callbacks.formatters.MetricsFormatter
Translate batch metrics in json format.
This class is used by
logging.Logger
to make a string from record. For details refer to official docs for ‘logging’ module.Note
This is inner class used by Logger callback, no need to use it directly!
-
class
catalyst.core.callbacks.logging.
ConsoleLogger
[source]¶ Bases:
catalyst.core.callback.Callback
Logger callback, translates
state.*_metrics
to console and text file
-
class
catalyst.core.callbacks.logging.
TensorboardLogger
(metric_names: List[str] = None, log_on_batch_end: bool = True, log_on_epoch_end: bool = True)[source]¶ Bases:
catalyst.core.callback.Callback
Logger callback, translates
state.metric_manager
to tensorboard-
__init__
(metric_names: List[str] = None, log_on_batch_end: bool = True, log_on_epoch_end: bool = True)[source]¶ - Parameters
metric_names (List[str]) – list of metric names to log, if none - logs everything
log_on_batch_end (bool) – logs per-batch metrics if set True
log_on_epoch_end (bool) – logs per-epoch metrics if set True
-
-
class
catalyst.core.callbacks.logging.
VerboseLogger
(always_show: List[str] = None, never_show: List[str] = None)[source]¶ Bases:
catalyst.core.callback.Callback
Logs the params into console
-
__init__
(always_show: List[str] = None, never_show: List[str] = None)[source]¶ - Parameters
always_show (List[str]) – list of metrics to always show if None default is
["_timer/_fps"]
to remove always_show metrics set it to an empty list[]
never_show (List[str]) – list of metrics which will not be shown
-
-
class
catalyst.core.callbacks.optimizer.
OptimizerCallback
(loss_key: str = 'loss', optimizer_key: str = None, accumulation_steps: int = 1, grad_clip_params: Dict = None, decouple_weight_decay: bool = True)[source]¶ Bases:
catalyst.core.callback.Callback
Optimizer callback, abstraction over optimizer step.
-
__init__
(loss_key: str = 'loss', optimizer_key: str = None, accumulation_steps: int = 1, grad_clip_params: Dict = None, decouple_weight_decay: bool = True)[source]¶ - Parameters
grad_clip_params (dict) – params for gradient clipping
accumulation_steps (int) – number of steps before
model.zero_grad()
optimizer_key (str) – A key to take a optimizer in case there are several of them and they are in a dictionary format.
loss_key (str) – key to get loss from
state.loss
decouple_weight_decay (bool) – If True - decouple weight decay regularization.
save_model_grads (#) – If True - State.model_grads will
contain gradients calculated (#) –
on backward propagation on current (#) –
batch (#) –
-
static
grad_step
(*, optimizer: torch.optim.optimizer.Optimizer, optimizer_wds: List[float] = 0, grad_clip_fn: Callable = None)[source]¶ Makes a gradient step for a given optimizer
- Parameters
optimizer (Optimizer) – the optimizer
optimizer_wds (List[float]) – list of weight decay parameters for each param group
grad_clip_fn (Callable) – function for gradient clipping
-
-
class
catalyst.core.callbacks.phase.
PhaseManagerCallback
(train_phases: OrderedDict[str, int] = None, valid_phases: OrderedDict[str, int] = None, valid_mode: str = None)[source]¶ Bases:
catalyst.core.callback.Callback
PhaseManagerCallback updates state.phase
-
VALIDATION_MODE_ALL
= 'all'¶
-
VALIDATION_MODE_SAME
= 'same'¶
-
allowed_valid_modes
= ['same', 'all']¶
-
-
class
catalyst.core.callbacks.scheduler.
SchedulerCallback
(scheduler_key: str = None, mode: str = None, reduced_metric: str = 'loss')[source]¶
-
class
catalyst.core.callbacks.scheduler.
LRUpdater
(optimizer_key: str = None)[source]¶ Bases:
catalyst.core.callback.Callback
Basic class that all Lr updaters inherit from
-
class
catalyst.core.callbacks.wrappers.
PhaseWrapperCallback
(base_callback: catalyst.core.callback.Callback, active_phases: List[str] = None, inactive_phases: List[str] = None)[source]¶ Bases:
catalyst.core.callback.Callback
CallbackWrapper which disables/enables handlers dependant on current phase and event type
May be useful i.e. to disable/enable optimizers & losses
-
LEVEL_BATCH
= 'batch'¶
-
LEVEL_EPOCH
= 'epoch'¶
-
LEVEL_LOADER
= 'loader'¶
-
LEVEL_STAGE
= 'stage'¶
-
TIME_END
= 'end'¶
-
TIME_START
= 'start'¶
-
-
class
catalyst.core.callbacks.wrappers.
PhaseBatchWrapperCallback
(base_callback: catalyst.core.callback.Callback, active_phases: List[str] = None, inactive_phases: List[str] = None)[source]¶ Bases:
catalyst.core.callbacks.wrappers.PhaseWrapperCallback