Skip to content

Robustness, label quality, and active data acquisition · Robustness, label quality, and active data acquisition · ROB002 · Training method

TRADES

Balance supervised fit with local predictive consistency using a classification loss and an adversarial divergence penalty. It explicitly exposes a robustness/accuracy trade-off.

Runs in CodeSplash Complete calculation CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page.

Do it in CodeSplash Run it on your own data, in Python or R.

Availability
Runs in CodeSplash
Code example
Complete calculation
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through learn.torch_mlp. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review, on this exact code. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Balance supervised fit with local predictive consistency using a classification loss and an adversarial divergence penalty. It explicitly exposes a robustness/accuracy trade-off.

Question / hypotheses

How does the chosen penalty alter clean and adversarial performance? The method optimizes a surrogate objective; it does not test equality of risks.

Architecture / computational structure

The same classifier processes clean and perturbed inputs. An inner optimizer seeks a perturbation that increases predictive disagreement. The outer step combines clean-label loss with the divergence between predictive distributions.

Assumptions

The perturbation set is declared and meaningful. Class probabilities must be normalized. A valid training/evaluation separation and a sufficiently strong inner optimizer are needed for useful empirical comparisons.

Formula / computational definition

L=E[−log⁡pθ(y∣x)+βmax⁡δ∈ΔDKL{pθ(⋅∣x)∥pθ(⋅∣x+δ)}].\mathcal L=\mathbb E\left[-\log p_\theta(y\mid x)+\beta\max_{\delta\in\Delta}D_{\mathrm{KL}}\{p_\theta(\cdot\mid x)\Vert p_\theta(\cdot\mid x+\delta)\}\right].

Parameters and notation

Learned: classifier θ. Hyperparameters: β, norm/radius, inner optimization steps and step size, learning rate, batch size, and schedule. This convention uses β as the divergence multiplier; some presentations use an inverse trade-off parameter. State the convention rather than copying a numeric value blindly.

Fitting, fine-tuning, and adaptation

Adapt the backbone and head jointly or specify frozen layers. Retune the penalty on development data after changing domain, label set, or normalization. Retain a clean-data and adversarial validation curve; using only training divergence can hide poor generalization.

How to use and calibrate

Sweep a prespecified small β grid under a matched training budget, select using the application trade-off, then evaluate once on held-out cases. Report clean and robust metrics together. Store the attack and normalization configurations as part of the model card.

Example application

Choose an operating point for a classifier whose input perturbations are bounded by a declared image-intensity tolerance.

Interpretation / pitfalls

Watch out

Low KL divergence alone can be achieved by an uninformative predictor; the classification term matters. Reversing KL arguments changes the objective. The toy calculation uses supplied distributions and does not find a worst-case perturbation.

Python and R code examples

Scope: Calculate the TRADES outer loss on supplied clean/adversarial probabilities. Python: NumPy; R: base R.

Validation: Python executed successfully on the supplied fixture; R reviewed but not executed.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Complete calculation — Calculate the TRADES outer loss on supplied clean/adversarial probabilities. Python: NumPy; R: base R. · needs numpy

import numpy as np
p=np.array([.8,.2]); q=np.array([.55,.45]); beta=3.
clean=-np.log(p[0]); kl=np.sum(p*np.log(p/q))
assert kl>=0 and np.isclose(p.sum(),1) and np.isclose(q.sum(),1)
print(clean,kl,clean+beta*kl)
R

Complete calculation — Calculate the TRADES outer loss on supplied clean/adversarial probabilities. Python: NumPy; R: base R.

p <- c(.8,.2); q <- c(.55,.45); beta <- 3
clean <- -log(p[1]); kl <- sum(p*log(p/q))
stopifnot(kl>=0,abs(sum(p)-1)<1e-12,abs(sum(q)-1)<1e-12)
print(c(clean=clean,KL=kl,total=clean+beta*kl))

Sources

From the CodeSplash reference, entry ROB002.