dgs.utils.torchtools.open_specified_layers

dgs.utils.torchtools.open_specified_layers(model: TorchMod | BaseMod, open_layers: str | list[str], freeze_others: bool = True, verbose: bool = False) None[source]

Opens the specified layers in the given model for training while keeping all other layers unchanged or frozen.

Parameters:
  • model – A torch module or a BaseModule containing a torch module as attribute ‘module’.

  • open_layers – Name or names of the layers to open for training.

  • freeze_others – Whether to freeze all the other modules that are not present in open_layers.

  • verbose – Whether to print some debugging information.

Examples

In the first example, open only the classifier-layer and freeze the rest of the model. Then, in the second example using the same model, open the two fc-layers while keeping the previously opened classifier open. In the third one open the fc- and classifier-layers and freeze everything else.

>>> from dgs.utils.torchtools import open_specified_layers
>>> open_specified_layers(model, open_layers='classifier')
>>> open_specified_layers(model, open_layers=['fc1', 'fc2'], freeze_others=False)
>>> open_specified_layers(other_model, open_layers=['fc', 'classifier'])
Raises:

ValueError if a value in open_layers is not an attribute of the model.