def change_trainable()

in torchmoji/finetuning.py [0:0]


def change_trainable(module, trainable, verbose=False):
    """ Helper method that freezes or unfreezes a given layer.

    # Arguments:
        module: Module to be modified.
        trainable: Whether the layer should be frozen or unfrozen.
        verbose: Verbosity flag.
    """
    
    if verbose: print('Changing MODULE', module, 'to trainable =', trainable)
    for name, param in module.named_parameters():
        if verbose: print('Setting weight', name, 'to trainable =', trainable)
        param.requires_grad = trainable

    if verbose:
        action = 'Unfroze' if trainable else 'Froze'
        if verbose: print("{} {}".format(action, module))