Fig. 1 in words
One budget, two changes · 540 real test digits (UCI) · same data as code 05
1 One digit, three versions
- original · net: 3 · >99.9%
- noise ±2 · net: 3 · >99.9%
- chosen ±2 · net: 8 · 99%
- test digit 29, a 3 read at >99.9%
- each pixel moved ≤ 2 steps of the 0–16 scale
- 396 of 540 digits: noise leaves them right, the chosen change flips them
2 Accuracy by change size
- Accuracy from 0 to ±4 under the attack (net, linear) and under noise
- At ±2: noise 95.4 · attack: linear 46.7 · net 21.5
- grey levels per pixel · noise: net
3 Why: signs add up
- linear model, weights of 8 minus 3: red + · blue −
- push ε · sign(w): no pixel works against it
- size of the push, same ±ε: random signs ≈ ε·√Σw² · chosen 5.43× · ceiling √64 = 8×
- chosen = ε·Σ|w| · mean of 45 digit pairs
4 Defences, tested again
- net · no defence: 21.5%
- linear · no defence: 46.7%
- linear, trained on old attacks · replayed: 88.5%
- same linear model · fresh attack: 15.9%
- net, adversarial training · fresh attack: 74.3%
- accuracy under a ±2 change · adversarial training, clean: 97.2%
01 A Chosen Change Definition
- Evasion (NIST AI 100-2): a small, deliberate change to an input that causes an error
- Named adversarial examples by Szegedy et al. (2014); Biggio et al. (2013) built them with gradients too
- Budget here: ±2 steps per pixel on the 0–16 grey scale
Older than the name: in 2004 Dalvi et al. showed spam filters can be tricked by a few chosen changes.
02 Noise vs. Attack Claim fails
- Claim: “if random noise does not hurt a model, small changes cannot”
- ±2 levels on 540 test digits: noise keeps the net at 95.4%, a chosen change of the same size leaves 21.5%
- Linear model: 95.4% against 46.7%
Fawzi et al. (2016): random noise must be about √d times the smallest attack (ℓ₂), if the boundary is gently curved.
03 Many Small Pushes Why
- A linear score moves by ε·Σ|wᵢ| when every pixel follows the sign of its weight (Goodfellow et al. 2015)
- Random signs mostly cancel: about ε·√Σwᵢ²
- Linear model's weights: the chosen push is 5.43× the random one, mean of 45 digit pairs (ceiling √64 = 8)
Goodfellow et al., listing Szegedy et al.'s findings: “shallow softmax regression models are also vulnerable”.
04 Attacks Travel Transfer
- Changes computed on the linear model alone take the net from 97.8% to 35.7%
- Szegedy et al.: the same perturbed input also fooled a net trained on another subset of the data
- Papernot et al. (2016): 96.19% and 88.94% misclassified by two commercial services after 800 queries
Hiding the weights does not hide the weakness.
05 Try It Yourself PY 3.13.18 · NUMPY 2.5.3 · SKLEARN 1.9.1
sprint42_adversarial_examples_small_changes_wrong_answers.py
import numpy as np
from sklearn.datasets import load_digits
from sklearn.linear_model import LogisticRegression as LR
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier as MLP
X, y = load_digits(return_X_y=True); X = X / 16 # grey 0..16 -> 0..1
Xa, Xt, ya, yt = train_test_split(X, y, test_size=.3, stratify=y,
random_state=42)
lin = LR(max_iter=5000).fit(Xa, ya) # linear model
net = MLP((64,), max_iter=3000, random_state=42).fit(Xa, ya)
def grad(m, X, y): # d loss / d pixel
P = m.predict_proba(X); P[np.arange(len(y)), y] -= 1
if hasattr(m, "coef_"): return P @ m.coef_
(W1, W2), b1 = m.coefs_, m.intercepts_[0]
return (P @ W2.T * (X @ W1 + b1 > 0)) @ W1.T
def attack(m, X, y, e, steps=10): # sign steps kept within ±e
A = X.copy()
for _ in range(steps):
A = A + e / 4 * np.sign(grad(m, A, y))
A = np.clip(np.clip(A, X - e, X + e), 0, 1)
return A
pct = lambda m, X: f"{100 * m.score(X, yt):.1f}%"
noise = np.random.default_rng(42).choice([-1, 1], Xt.shape)
print(len(yt), "test digits | clean: linear", pct(lin, Xt), "net",
pct(net, Xt))
for lv in range(1, 5): # change of lv grey levels
e = lv / 16; R = np.clip(Xt + e * noise, 0, 1)
print(f"±{lv} linear: noise {pct(lin, R)} attack",
pct(lin, attack(lin, Xt, yt, e)), "| net: noise",
pct(net, R), "attack", pct(net, attack(net, Xt, yt, e)))
e = 2 / 16 # the change on Fig. 1
W = lin.coef_; r = [abs(W[i] - W[j]).sum() / np.linalg.norm(
W[i] - W[j]) for i in range(10) for j in range(i)]
print(f"chosen / random push: {np.mean(r):.2f} (ceiling sqrt(64) = 8)")
A = attack(lin, Xt, yt, e) # made on the linear model
print("made on linear, given to net:", pct(net, Xt), "->", pct(net, A))
rob = LR(max_iter=5000).fit(np.vstack([Xa, attack(lin, Xa, ya, e)]),
np.r_[ya, ya]) # trained on old attacks
print("defence | old attack:", pct(rob, A), "fresh attack:",
pct(rob, attack(rob, Xt, yt, e)))
adv = MLP((64,), random_state=42)
adv.partial_fit(Xa, ya, classes=np.arange(10))
for _ in range(200): # adversarial training
adv.partial_fit(np.vstack([Xa, attack(adv, Xa, ya, e, 5)]),
np.r_[ya, ya])
print("adversarial training | clean:", pct(adv, Xt), "attack:",
pct(adv, attack(adv, Xt, yt, e)), "50 steps:",
pct(adv, attack(adv, Xt, yt, e, 50)))
Output, as printed on the image
540 test digits | clean: linear 96.7% net 97.8%
±1 linear: noise 96.3% attack 82.8% | net: noise 97.0% attack 73.7%
±2 linear: noise 95.4% attack 46.7% | net: noise 95.4% attack 21.5%
±3 linear: noise 92.2% attack 9.4% | net: noise 92.4% attack 0.2%
±4 linear: noise 89.8% attack 0.4% | net: noise 86.7% attack 0.0%
chosen / random push: 5.43 (ceiling sqrt(64) = 8)
made on linear, given to net: 97.8% -> 35.7%
defence | old attack: 88.5% fresh attack: 15.9%
adversarial training | clean: 97.2% attack: 74.3% 50 steps: 73.9%
Run it. sprint42_adversarial_examples_small_changes_wrong_answers.py · versions, data and steps
Check: ±2 = 2 ÷ 16 = 0.125 · 95.4 − 21.5 = 73.9 points · 88.5 − 15.9 = 72.6 points · 5.43 < 8 = √64
06 General Notes Practice
- Linear model trained on old attacks: 88.5% replayed · 15.9% fresh · undefended 46.7%
- Athalye et al. (2018): 7 of 9 non-certified ICLR 2018 defences relied on obfuscated gradients · 6 fully broken · 1 partly
- Adversarial training of the net (Madry et al. 2018): 74.3% · 73.9% at 50 steps
Bridge: test a defence with an attack that knows it (Carlini et al. 2019).
07 Mini Project Hands-on
- Attack in one full step (FGSM): net 28.0% against 21.5% for 10 steps
- Seeds 1–5 for the noise and the net: does the gap hold?
- Train adversarially at ±3, then report clean · ±2 · ±3
Price the defence: on clean digits the old-attack model fell from 96.7% to 93.5%.
R Trusted Sources
- Szegedy et al. (2014) ICLR
- Goodfellow, Shlens & Szegedy (2015) Explaining and Harnessing Adversarial Examples, ICLR
- Biggio et al. (2013) ECML PKDD, LNCS 8190, 387–402
- Biggio & Roli (2018) Pattern Recognit. 84, 317–331
- Dalvi et al. (2004) KDD, 99–108
- Fawzi, Moosavi-Dezfooli & Frossard (2016) NeurIPS 29, 1632–1640
- Papernot, McDaniel & Goodfellow (2016) arXiv 1605.07277
- Madry et al. (2018) ICLR
- Athalye, Carlini & Wagner (2018) ICML, PMLR 80, 274–283
- Carlini et al. (2019) arXiv 1902.06705
- Vassilev et al. (2025) NIST AI 100-2e2025
- Alpaydin & Kaynak (1998) UCI Optical Recognition of Handwritten Digits
