Skip to content

l1

L1 parameter regularisation for certified training.

get_l1_reg(model, device='cuda')

Sum absolute parameter values to obtain the L1 training regulariser.

Parameters:

Name Type Description Default
model Module

The neural network model to regularize. IMPORTANT: Don't pass the bounded model here.

required
device str

The device to perform the computation on.

'cuda'

Returns:

Type Description
Tensor

The L1 regularisation loss.

Source code in CTRAIN/train/certified/regularisers/l1.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def get_l1_reg(model, device='cuda'):
    """Sum absolute parameter values to obtain the L1 training regulariser.

    Args:
        model (torch.nn.Module): The neural network model to regularize. IMPORTANT: Don't
            pass the bounded model here.
        device (str, optional): The device to perform the computation on.

    Returns:
        (torch.Tensor): The L1 regularisation loss.
    """
    loss = torch.zeros(()).to(device)
    # only regularise Linear and Convolutional layers
    for module in model.modules():
        if isinstance(module, nn.Linear):
            loss += torch.abs(module.weight).sum()
        elif isinstance(module, nn.Conv2d):
            loss += torch.abs(module.weight).sum()
    return loss