callbacks
shrinkai.distillation.callbacks
Ready-to-use training callbacks for DistillationEngine.fit / Distiller.fit.
A callback is any callable accepting (epoch: int, metrics: dict[str, float]), as
already supported by fit(..., callbacks=[...]). EarlyStopping additionally exposes
a stop boolean attribute: after invoking all callbacks, the training loop checks
getattr(callback, "stop", False) on each of them and breaks out of the epoch loop
if any callback requests it. Plain function callbacks are unaffected by this check.
Classes:
| Name | Description |
|---|---|
EarlyStopping |
Stops training when a monitored metric has stopped improving. |
ModelCheckpoint |
Saves a model's weights to disk during training. |
Classes
EarlyStopping
Stops training when a monitored metric has stopped improving.
Attributes:
| Name | Type | Description |
|---|---|---|
stop |
bool
|
Set to True once |
Methods:
| Name | Description |
|---|---|
__call__ |
Updates internal state and sets |
__init__ |
Initializes the EarlyStopping callback. |
Source code in src/shrinkai/distillation/callbacks.py
Methods:
__call__
Updates internal state and sets self.stop if patience is exhausted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
epoch
|
int
|
Current 1-based epoch index. |
required |
metrics
|
dict[str, float]
|
Epoch summary dict, as passed by |
required |
Source code in src/shrinkai/distillation/callbacks.py
__init__
__init__(
monitor: str = "val_loss",
patience: int = 5,
mode: Literal["min", "max"] = "min",
min_delta: float = 0.0,
) -> None
Initializes the EarlyStopping callback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
monitor
|
str
|
Metric key to watch in the epoch summary dict (e.g. "val_loss"). |
'val_loss'
|
patience
|
int
|
Number of consecutive non-improving epochs tolerated before training is stopped. |
5
|
mode
|
Literal['min', 'max']
|
"min" if lower values of |
'min'
|
min_delta
|
float
|
Minimum absolute change to qualify as an improvement. |
0.0
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/shrinkai/distillation/callbacks.py
ModelCheckpoint
Saves a model's weights to disk during training.
Holds a direct reference to the module to save (typically the student), so it
plugs into fit(callbacks=[...]) without changing the existing
(epoch, metrics) -> None callback signature.
Methods:
| Name | Description |
|---|---|
__call__ |
Saves the model's weights, respecting |
__init__ |
Initializes the ModelCheckpoint callback. |
Source code in src/shrinkai/distillation/callbacks.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
Methods:
__call__
Saves the model's weights, respecting save_best_only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
epoch
|
int
|
Current 1-based epoch index. |
required |
metrics
|
dict[str, float]
|
Epoch summary dict, as passed by |
required |
Source code in src/shrinkai/distillation/callbacks.py
__init__
__init__(
model: Module,
filepath: str | Path,
monitor: str = "val_loss",
mode: Literal["min", "max"] = "min",
save_best_only: bool = True,
) -> None
Initializes the ModelCheckpoint callback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
The module whose |
required |
filepath
|
str | Path
|
Destination path for the saved weights. |
required |
monitor
|
str
|
Metric key to watch when |
'val_loss'
|
mode
|
Literal['min', 'max']
|
"min" if lower values of |
'min'
|
save_best_only
|
bool
|
If True, only overwrite |
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |