Skip to content

PGD training loss API

This loss supports native PyTorch classifiers and is used by the PGD trainer. The attack runs in evaluation mode; adversarial loss evaluation uses the caller's mode. It returns a scalar loss, or (loss, error_rate) when return_stats=True. return_bounds=True is unsupported.

The step size is relative to the attack region. These are training controls; see the evaluation guide for evaluation budgets.

Compute the mean criterion on PGD-generated adversarial inputs.

The attack runs in evaluation mode; loss evaluation uses the caller's model mode. return_bounds must be False. Returns the scalar loss, or (loss, adversarial_error) when return_stats=True.

Source code in CTRAIN/train/adversarial/losses/pgd.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def get_pgd_loss(hardened_model, ptb, data, target, n_classes, criterion, return_bounds=False, n_steps=200, step_size=0.2, restarts=1, early_stopping=True, *, decay_factor=.1, decay_checkpoints=(), return_stats=False):
    """Compute the mean criterion on PGD-generated adversarial inputs.

    The attack runs in evaluation mode; loss evaluation uses the caller's model
    mode. return_bounds must be False. Returns the scalar loss, or (loss,
    adversarial_error) when return_stats=True.
    """
    assert not return_bounds, "Return Bounds not available PGD training!"

    with preserve_model_state(hardened_model):
        hardened_model.eval()
        x_adv = pgd_attack(
            model=hardened_model, data=data, target=target,
            x_L=ptb.x_L, x_U=ptb.x_U, restarts=restarts,
            step_size=step_size, n_steps=n_steps, early_stopping=early_stopping,
            device=data.device, decay_factor=decay_factor,
            decay_checkpoints=decay_checkpoints,
        )

    adv_pred = hardened_model(x_adv)

    loss = criterion(adv_pred, target).mean()
    if return_stats:
        return loss, (adv_pred.detach().argmax(1) != target).float().mean().item()
    return loss