Skip to content

convex_combinations

get_mtl_ibp_loss(hardened_model, original_model, ptb, data, target, n_classes, criterion, alpha, return_bounds=False, return_stats=False, restarts=1, step_size=0.2, n_steps=200, pgd_ptb=None, early_stopping=False, decay_checkpoints=(), decay_factor=0.1, device='cuda')

Computes the MTL-IBP loss.

Parameters:

Name Type Description Default
hardened_model BoundedModule

The bounded model to be trained.

required
original_model Module

The original model.

required
ptb PerturbationLpNorm

The perturbation applied to the input data.

required
data Tensor

Input data.

required
target Tensor

Target labels.

required
n_classes int

Number of classes.

required
criterion callable

Loss function.

required
alpha float

Weighting factor between robust loss and adversarial loss.

required
return_bounds bool

If True, returns bounds. Default is False.

False
return_stats bool

If True, returns robustness and adversarial error statistics. Default is False.

False
restarts int

Number of restarts for PGD attack. Default is 1.

1
step_size float

Step size for PGD attack. Default is 0.2.

0.2
n_steps int

Number of steps for PGD attack. Default is 200.

200
pgd_ptb object

PGD perturbation object. Default is None.

None
early_stopping bool

If True, stops early during PGD attack. Default is False.

False
decay_checkpoints tuple

Checkpoints for decay during PGD attack. Default is ().

()
decay_factor float

Decay factor for PGD attack. Default is 0.1.

0.1
device str

Device to run the computations on. Default is 'cuda'.

'cuda'

Returns:

Type Description
tuple

A tuple containing the loss, and optionally certified and adversarial error statistics.

Source code in CTRAIN/train/certified/losses/convex_combinations.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def get_mtl_ibp_loss(hardened_model, original_model, ptb, data, target, n_classes, criterion, alpha, return_bounds=False, return_stats=False, restarts=1, step_size=.2, n_steps=200, pgd_ptb=None, early_stopping=False, decay_checkpoints=(), decay_factor=.1, device='cuda'):    
    """
    Computes the MTL-IBP loss.

    Parameters:
        hardened_model (auto_LiRPA.BoundedModule): The bounded model to be trained.
        original_model (torch.nn.Module): The original model.
        ptb (autoLiRPA.PerturbationLpNorm): The perturbation applied to the input data.
        data (torch.Tensor): Input data.
        target (torch.Tensor): Target labels.
        n_classes (int): Number of classes.
        criterion (callable): Loss function.
        alpha (float): Weighting factor between robust loss and adversarial loss.
        return_bounds (bool, optional): If True, returns bounds. Default is False.
        return_stats (bool, optional): If True, returns robustness and adversarial error statistics. Default is False.
        restarts (int, optional): Number of restarts for PGD attack. Default is 1.
        step_size (float, optional): Step size for PGD attack. Default is 0.2.
        n_steps (int, optional): Number of steps for PGD attack. Default is 200.
        pgd_ptb (object, optional): PGD perturbation object. Default is None.
        early_stopping (bool, optional): If True, stops early during PGD attack. Default is False.
        decay_checkpoints (tuple, optional): Checkpoints for decay during PGD attack. Default is ().
        decay_factor (float, optional): Decay factor for PGD attack. Default is 0.1.
        device (str, optional): Device to run the computations on. Default is 'cuda'.

    Returns:
        (tuple): A tuple containing the loss, and optionally certified and adversarial error statistics.
    """
    hardened_model.eval()
    original_model.eval()
    x_adv = pgd_attack(
        model=hardened_model,
        data=data,
        target=target,
        x_L=pgd_ptb.x_L,
        x_U=pgd_ptb.x_U,
        restarts=restarts,
        step_size=step_size,
        n_steps=n_steps,
        early_stopping=early_stopping,
        device=device,
        decay_factor=decay_factor,
        decay_checkpoints=decay_checkpoints
    )

    hardened_model.train()
    original_model.train()

    adv_output = hardened_model(x_adv)
    adv_loss = criterion(adv_output, target).mean()

    robust_loss, lb, ub = get_ibp_loss(
        hardened_model=hardened_model,
        ptb=ptb,
        data=data,
        target=target,
        n_classes=n_classes,
        criterion=criterion,
        return_bounds=True
    )

    loss = alpha * robust_loss + (1 - alpha) * adv_loss

    return_tuple = (loss,)

    if return_bounds:
        assert False, "Return bounds is not implemented for MTL-IBP"
    elif return_stats:
        robust_err = torch.sum((lb < 0).any(dim=1)).item() / data.size(0)
        adv_err = torch.sum(torch.argmax(adv_output, dim=1) != target).item() / data.size(0)
        return_tuple = return_tuple + (robust_err, adv_err)

    return return_tuple