src.model.deeplearn.handle.dl_freeze_training_executor

Classes

DLFreezeTrainingExecutor([freeze_training])

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:

  1. 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.

  2. Schedule execution: iterate the segments, mutate the layer.trainable flags on the model, optionally reset the learning rate, and call a caller-provided fit_fn once per segment to run the actual training.

Extracting these methods to a small dedicated class lets both the standard DLTrainingHandler and the TORF-specific TransfOctoRFHandler reuse 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 that op_epoch[i] is the epoch at which the operations op[i] must be applied (with an optional initial learning rate lrs[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 layers entry of a freeze-training specification.

When the entry is the string "all" it is expanded to all layers of nn that own weights, in declaration order. When it is "all_reverse" the same list is reversed. When nn is not provided but a string entry is encountered, a src.model.deeplearn.deep_learning_exception.DeepLearningException is raised.

Parameters:
  • layers – The layers entry 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_fn runs one training pass for the requested number of epochs and returns a keras.callbacks.History instance. The executor takes care of toggling layer.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 its keras.callbacks.History.

Returns:

Combined training history across all segments.

static validate_layers(nn, ops)

Validate that every layer name referenced by ops exists in the given neural network. Raises src.model.deeplearn.deep_learning_exception.DeepLearningException when 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.