コード例 #1
0
def _unfreeze_and_add_param_group(module: torch.nn.Module,
                                  optimizer: Optimizer,
                                  lr: Optional[float] = None,
                                  train_bn: bool = True):
    """Unfreezes a module and adds its parameters to an optimizer."""
    _make_trainable(module)
    params_lr = optimizer.param_groups[0]['lr'] if lr is None else float(lr)
    optimizer.add_param_group({
        'params':
        filter_params(module=module, train_bn=train_bn),
        'lr':
        params_lr / 10.,
    })
コード例 #2
0
def unfreeze(module: Sequential, optimizer: Optimizer, unfreeze_from: int,
             unfreeze_to: int, **kwargs):
    """
    Unfreeze a model from ind

    :param module:
    :param optimizer
    :param unfreeze_from:
    :param unfreeze_to:
    :return:
    """
    for ind in range(len(module)):
        submodule = module._modules[str(ind)]
        if ind < unfreeze_from:
            for param in submodule.parameters():
                param.requires_grad = False
        elif ind < unfreeze_to:
            for param in submodule.parameters():
                param.requires_grad = True
            optimizer.add_param_group({
                'params': submodule.parameters(),
                **kwargs
            })