src.model.deeplearn.handle.dl_freeze_training_executor
Classes
|
- class src.model.deeplearn.handle.dl_freeze_training_executor.DLFreezeTrainingExecutor(freeze_training=None)
- Author:
Alberto M. Esmoris Pena
The DLFreezeTrainingExecutor class provides a reusable engine to apply freeze-training schedules to any Keras neural network. It implements two pieces of logic, both of which used to live inside
DLTrainingHandler:Schedule preparation: given a list of freeze-training specifications and a total number of training epochs, derive the epochs at which freeze/unfreeze operations must be applied, the operations themselves, and the optional initial learning rates per segment.
Schedule execution: iterate the segments, mutate the
layer.trainableflags on the model, optionally reset the learning rate, and call a caller-providedfit_fnonce per segment to run the actual training.
Extracting these methods to a small dedicated class lets both the standard
DLTrainingHandlerand the TORF-specificTransfOctoRFHandlerreuse the same freeze-training logic without code duplication.- Variables:
freeze_training (list or None) – The freeze-training specification (list of dicts or
None).
- __init__(freeze_training=None)
Initialize a freeze-training executor with the given specification.
- prepare_schedule(training_epochs, nn=None)
Derive the freeze-training schedule from the stored specification and the total number of training epochs.
- Parameters:
training_epochs (int) – Total number of training epochs.
nn – Optional Keras neural network used to expand string layer-domain references (
"all"and"all_reverse"). It is only required when the specification uses those strings.
- Returns:
Tuple
(op_epoch, op, lrs)such thatop_epoch[i]is the epoch at which the operationsop[i]must be applied (with an optional initial learning ratelrs[i]).- Return type:
tuple of (list of int, list of dict, list of float or None)
- static compute_schedule(freeze_training, training_epochs, nn=None)
Static implementation of
prepare_schedule()accepting the specification as an argument.- Parameters:
freeze_training (list) – The freeze-training specification.
training_epochs (int) – Total number of training epochs.
nn – Optional Keras neural network (see
prepare_schedule()).
- Returns:
See
prepare_schedule().- Return type:
tuple
- static resolve_layers(layers, spec_index, nn=None)
Resolve the
layersentry of a freeze-training specification.When the entry is the string
"all"it is expanded to all layers ofnnthat own weights, in declaration order. When it is"all_reverse"the same list is reversed. Whennnis not provided but a string entry is encountered, asrc.model.deeplearn.deep_learning_exception.DeepLearningExceptionis raised.- Parameters:
layers – The
layersentry from a specification.spec_index – 0-based index of the specification, used for error messages.
nn – Optional Keras neural network for string expansion.
- Returns:
List of layer names.
- Return type:
list of str
- static handle_round_robin_freezing(layers, interval, strategy)
Extract the freeze training operations and their corresponding epochs following a round-robin strategy.
- Parameters:
layers (list of str) – The names of the layers that must be considered.
interval (list of int) – The epoch interval governing the strategy.
strategy (dict) – The strategy specification (
window_size,iterative_span).
- Returns:
A tuple
(epochs, ops).- Return type:
tuple of list
- static handle_random_freezing(layers, interval, strategy)
Extract the freeze training operations and their corresponding epochs following a random strategy.
- Parameters:
layers (list of str) – The names of the layers that must be considered.
interval (list of int) – The epoch interval governing the strategy.
strategy (dict) – The strategy specification (
window_size,iterative_span).
- Returns:
A tuple
(epochs, ops).- Return type:
tuple of list
- run(nn, training_epochs, fit_fn)
Execute the freeze-training schedule on the given neural network.
The caller-provided
fit_fnruns one training pass for the requested number of epochs and returns akeras.callbacks.Historyinstance. The executor takes care of togglinglayer.trainable, setting the learning rate when the segment specifies one, and merging the per-segment histories.- Parameters:
nn – The Keras neural network to train.
training_epochs (int) – Total number of training epochs.
fit_fn – Callable
fit_fn(segment_epochs)running one training pass and returning itskeras.callbacks.History.
- Returns:
Combined training history across all segments.
- static validate_layers(nn, ops)
Validate that every layer name referenced by
opsexists in the given neural network. Raisessrc.model.deeplearn.deep_learning_exception.DeepLearningExceptionwhen a referenced layer is missing.- Parameters:
nn – The Keras neural network.
ops – List of operation dicts.
- static log_frozen_summary(nn)
Log how many layers remain frozen after the schedule completes (matching the behavior previously inlined in DLTrainingHandler.fit).
- Parameters:
nn – The Keras neural network.