Module delta.extensions.callbacks
Custom callbacks that come with DELTA.
Functions
def ExponentialLRScheduler(start_epoch: int = 10, multiplier: float = 0.95)-
Schedule the learning rate exponentially.
Parameters
start_epoch:int- The epoch to begin.
multiplier:float- After
start_epoch, multiply the learning rate by this amount each epoch.
Classes
class SetTrainable (layer_name: str, epoch: int, trainable: bool = True, learning_rate: float = None)-
Changes whether a given layer is trainable during training.
This is useful for transfer learning, to do an initial training and then allow fine-tuning.
Parameters
layer_name:str- The layer to modify.
epoch:int- The change will take place at the start of this epoch (the first epoch is 1).
trainable:bool- Whether the layer will be made trainable or not trainable.
learning_rate:float- Optionally change the learning rate as well.
Expand source code
class SetTrainable(tensorflow.keras.callbacks.Callback): """ Changes whether a given layer is trainable during training. This is useful for transfer learning, to do an initial training and then allow fine-tuning. """ def __init__(self, layer_name: str, epoch: int, trainable: bool=True, learning_rate: float=None): """ Parameters ---------- layer_name: str The layer to modify. epoch: int The change will take place at the start of this epoch (the first epoch is 1). trainable: bool Whether the layer will be made trainable or not trainable. learning_rate: float Optionally change the learning rate as well. """ super().__init__() self._layer_name = layer_name self._epoch = epoch - 1 self._make_trainable = trainable self._lr = learning_rate self._triggered = False def on_epoch_begin(self, epoch, logs=None): # pylint: disable=unused-argument if epoch == self._epoch: if self._triggered: return self._triggered = True # don't repeat twice l = self.model.get_layer(self._layer_name) l.trainable = True # have to abort, recompile changed model, and continue training raise ContinueTrainingException(completed_epochs=epoch, recompile_model=True, learning_rate=self._lr)Ancestors
- keras.src.callbacks.Callback
Methods
def on_epoch_begin(self, epoch, logs=None)-
Called at the start of an epoch.
Subclasses should override for any actions to run. This function should only be called during TRAIN mode.
Args
epoch- Integer, index of epoch.
logs- Dict. Currently no data is passed to this argument for this method but that may change in the future.