"""Murphy (2022), Chapter 19: Learning with Fewer Labeled Examples.

Companion code for Chapter 19 of K. P. Murphy, Probabilistic Machine Learning: An Introduction
(MIT Press, 2022; April 2025 draft), pp. 627-656, written from scratch with numpy (PyTorch for the neural
networks), to be read next to the lecture notes (../ch19-learning-with-fewer-labels.html).

Methods (all in this file)
* Section 19.1, data augmentation: bilinear random resized crops with flips (random_resized_crop), the
  Gaussian-noise augmentation = ridge experiment (aug_ridge_experiment) and the variance of the risk estimate
  under rotation augmentation (rotation_variance).
* Section 19.2, transfer learning: a self-supervised (masked reconstruction) trunk, a frozen linear probe and
  fine-tuning on noisy MNIST digits (TransferStudy); adapter parameter counts.
* Section 19.2.4, contrastive learning: the InfoNCE bound for a Gaussian pair with the optimal critic
  (info_nce_gaussian), the InfoNCE and CLIP losses (info_nce_loss, clip_loss).
* Section 19.3, semi-supervised learning: logistic regression with soft targets by Newton's method (fit_logreg),
  self-training (self_training), entropy minimisation (entropy_min), label propagation, both as printed
  (label_prop_book) and the harmonic solution (label_prop_harmonic), a semi-supervised Gaussian mixture
  classifier fitted by EM (ss_gmm_fit), the VAT power iteration (vat_power_iterations), the distillation loss.
* Section 19.4, active learning: Laplace logistic regression (laplace_logreg), the probit predictive, BALD with a
  closed-form approximation (bald_closed_form) and by Monte Carlo (bald_mc), pool-based loops (active_run).
* Section 19.5, meta-learning: second-order MAML for a 1-40-40-1 ReLU network on sinusoid tasks (maml_train),
  hand-written backpropagation (mlp_loss_grad), adaptation (adapt), and the prior precision that K gradient
  steps induce (gd_prior_precision).
* Section 19.7, weak supervision: the noisy-OR multi-instance model (noisy_or_fit).

Data. Figure 19.1 uses one of the two sample photographs shipped with scikit-learn. Figures 19.8 to 19.10 and the
supplementary figures use simulated two-dimensional data (two moons, elongated Gaussian clusters, sinusoids, bags of
points); the book's own sketches have no data. Figure 19.S2 uses MNIST (common.mnist, cached) with independent
N(0, 0.8^2) noise added to every pixel. Pre-trained and meta-trained weights are cached in code/cache/.

Figures. Reproduced here: 19.1, 19.8, 19.9, 19.10, 19.11. Skipped, with the reason (hand-drawn or architecture
diagrams, nothing is computed):
  19.2   fine-tuning a pre-trained network with a new output layer (a diagram from a textbook)
  19.3   adapter layers in a transformer and a residual network (diagrams)
  19.4   a context encoder and other proxy tasks (images from papers)
  19.5   SimCLR training and the benefit of random crops (diagrams)
  19.6   SimCLR minibatch visualisation (a diagram)
  19.7   the CLIP model (a diagram)
  19.12  the semi-supervised GAN framework (a diagram)
  19.13  self-supervised learning, fine-tuning and self-training combined (a diagram)
  19.14  a hierarchical Bayesian model for meta-learning (a graphical model)
  19.15  meta-learning for few-shot learning, 3-way 2-shot tasks (a diagram)
  19.16  a matching network (a diagram)
Supplementary figures for the gaps filled in the notes:
  19.S1  noise augmentation = ridge, and variance reduction by invariant augmentation
  19.S2  self-supervised pre-training then few labels; adapter parameter counts
  19.S3  the InfoNCE bound against the batch size, and the effect of the temperature
  19.S4  semi-supervised methods against the number of labels (moons and blobs)
  19.S5  label propagation sweep by sweep and the harmonic solution
  19.S6  VAT as a power iteration, and the logit-matching limit of distillation
  19.S7  active learning: random, maximum entropy and BALD; the BALD approximation
  19.S8  few-shot sinusoid regression with random, pre-trained and MAML initialisations
  19.S9  K gradient steps equal a Gaussian prior centred at the starting point
  19.S10 multi-instance learning with the noisy-OR likelihood
Run `python3 ch19_learning_with_fewer_labels.py --list`, `python3 ch19_learning_with_fewer_labels.py` (all
figures and the data files for the demonstrations, about a minute once the caches exist, about 4 minutes
otherwise) or `python3 ch19_learning_with_fewer_labels.py --fig 19.8`. Files go to ../img/ch19/; the data used by the
browser demonstrations go to ../data/ch19-*.json.
"""
import json
import os
import sys

os.environ.setdefault("OPENBLAS_NUM_THREADS", "1")

import numpy as np
from scipy import special, stats, optimize

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import common  # noqa: E402
from common import BLUE, CYAN, GREEN, GREY, MAGENTA, ORANGE, PURPLE, RED, panel_grid, panel_label, plt, savefig  # noqa: E402

CH = 19
HERE = os.path.dirname(os.path.abspath(__file__))
DATA_OUT = os.path.normpath(os.path.join(HERE, "..", "data"))
CACHE_DIR = os.path.join(HERE, "cache")
sigmoid = common.sigmoid
LOG2 = np.log(2.0)


def softplus(a):
    return np.logaddexp(0.0, a)


def binent(p):
    """Entropy (nats) of a Bernoulli(p), safe at 0 and 1."""
    p = np.clip(p, 1e-300, 1 - 1e-16)
    return -(p * np.log(p) + (1 - p) * np.log1p(-p))


def entropy_logit(a):
    """Entropy of Ber(sigmoid(a)) computed from the logit: softplus(a) - a sigmoid(a)."""
    a = np.asarray(a, dtype=float)
    return softplus(a) - a * sigmoid(a)


# =========================================================================== 19.1 augmentation
def bilinear_crop(img, y0, x0, h, w, out_h, out_w, flip=False):
    """Crop the box [y0, y0+h) x [x0, x0+w) of img (H, W, 3) and resize it to (out_h, out_w) by bilinear
    interpolation: a 'random resized crop' (zoom). flip mirrors left-right."""
    from scipy.ndimage import map_coordinates
    ys = y0 + (np.arange(out_h) + 0.5) * h / out_h - 0.5
    xs = x0 + (np.arange(out_w) + 0.5) * w / out_w - 0.5
    if flip:
        xs = xs[::-1]
    Y, X = np.meshgrid(ys, xs, indexing="ij")
    out = np.empty((out_h, out_w, img.shape[2]))
    for c in range(img.shape[2]):
        out[..., c] = map_coordinates(img[..., c].astype(float), [Y, X], order=1, mode="nearest")
    return np.clip(out, 0, 255).astype(np.uint8)


def random_resized_crop(img, rng, scale=(0.25, 1.0), ratio=(0.75, 1.33), out=(160, 160), p_flip=0.5):
    H, W = img.shape[:2]
    for _ in range(20):
        area = rng.uniform(*scale) * H * W
        r = np.exp(rng.uniform(np.log(ratio[0]), np.log(ratio[1])))
        w, h = np.sqrt(area * r), np.sqrt(area / r)
        if w <= W and h <= H:
            break
    else:
        w, h = min(W, H), min(W, H)
    x0, y0 = rng.uniform(0, W - w), rng.uniform(0, H - h)
    return bilinear_crop(img, y0, x0, h, w, out[0], out[1], flip=bool(rng.uniform() < p_flip))


# =========================================================================== data
def moons(n, noise, seed):
    from sklearn.datasets import make_moons
    X, y = make_moons(n_samples=n, noise=noise, random_state=seed)
    return X, y.astype(int)


def two_clusters(n_per, seed, sep=2.2):
    """Two elongated, correlated Gaussian clusters (class 0 and 1) used for the semi-supervised picture."""
    rng = np.random.default_rng(seed)
    S = np.array([[1.6, 1.3], [1.3, 1.6]]) * 0.5
    X0 = rng.multivariate_normal([-sep / 2, sep / 2], S, n_per)
    X1 = rng.multivariate_normal([sep / 2, -sep / 2], S, n_per)
    return np.vstack([X0, X1]), np.r_[np.zeros(n_per, int), np.ones(n_per, int)]


# the shared RBF feature map of the browser demonstrations: a fixed grid of centres
RBF_CX = np.linspace(-1.4, 2.4, 7)
RBF_CY = np.linspace(-0.9, 1.4, 5)
RBF_H = 0.55


def rbf_centres():
    return np.array([[cx, cy] for cx in RBF_CX for cy in RBF_CY])


def rbf_phi(X, centres=None, h=RBF_H):
    C = rbf_centres() if centres is None else centres
    d2 = ((X[:, None, :] - C[None, :, :]) ** 2).sum(-1)
    return np.hstack([np.ones((len(X), 1)), np.exp(-d2 / (2 * h * h))])


# =========================================================================== logistic regression
def fit_logreg(Phi, q, lam=1.0, sw=None, w0=None, iters=60, tol=1e-12):
    """Newton / IRLS for min_w  sum_n sw_n CE(q_n, sigmoid(phi_n w)) + (lam/2) |w_{1:}|^2 (the bias, column 0, is
    not penalised). q may be soft targets in [0, 1]."""
    n, d = Phi.shape
    sw = np.ones(n) if sw is None else sw
    R = lam * np.eye(d)
    R[0, 0] = 0.0
    w = np.zeros(d) if w0 is None else w0.copy()
    for _ in range(iters):
        p = sigmoid(Phi @ w)
        g = Phi.T @ (sw * (p - q)) + R @ w
        H = (Phi * (sw * p * (1 - p))[:, None]).T @ Phi + R + 1e-10 * np.eye(d)
        step = np.linalg.solve(H, g)
        w = w - step
        if np.max(np.abs(step)) < tol:
            break
    return w


def predict_p(Phi, w):
    return sigmoid(Phi @ w)


# =========================================================================== 19.3 semi-supervised methods
def self_training(Phi_l, y_l, Phi_u, tau=0.9, rounds=10, lam=1.0, soft=False):
    """Offline self-training (Section 19.3.1): fit, pseudo-label the unlabeled points whose largest class
    probability is at least tau, refit on labeled + pseudo-labeled, and repeat. soft=True uses the probabilities
    themselves as targets (then every point is kept when tau <= 0.5)."""
    w = fit_logreg(Phi_l, y_l.astype(float), lam)
    for _ in range(rounds):
        p = sigmoid(Phi_u @ w)
        keep = np.maximum(p, 1 - p) >= tau
        if not keep.any():
            break
        tgt = p[keep] if soft else (p[keep] > 0.5).astype(float)
        w = fit_logreg(np.vstack([Phi_l, Phi_u[keep]]), np.r_[y_l, tgt], lam)
    return w


def entropy_min(Phi_l, y_l, Phi_u, beta=1.0, lam=1.0, eta=0.5, iters=3000, w0=None):
    """Entropy minimisation (19.17) as a regulariser: gradient descent on
    J(w) = mean_L CE + beta mean_U H(p) + (lam / 2N_L) |w_{1:}|^2 from the supervised solution."""
    nl = len(y_l)
    w = fit_logreg(Phi_l, y_l.astype(float), lam) if w0 is None else w0.copy()
    R = np.ones(Phi_l.shape[1]) * lam / nl
    R[0] = 0.0
    for _ in range(iters):
        pl = sigmoid(Phi_l @ w)
        a = Phi_u @ w
        pu = sigmoid(a)
        g = Phi_l.T @ (pl - y_l) / nl + beta * Phi_u.T @ (-a * pu * (1 - pu)) / len(a) + R * w
        w = w - eta * g
    return w


def entropy_min_objective(w, Phi_l, y_l, Phi_u, beta, lam):
    nl = len(y_l)
    a_l = Phi_l @ w
    ce = np.mean(softplus(a_l) - y_l * a_l)
    pen = lam / (2 * nl) * np.sum(w[1:] ** 2)
    return ce + beta * np.mean(entropy_logit(Phi_u @ w)) + pen


def rbf_weights(X, sigma):
    d2 = ((X[:, None, :] - X[None, :, :]) ** 2).sum(-1)
    W = np.exp(-d2 / (2 * sigma * sigma))
    np.fill_diagonal(W, 0.0)
    return W


def label_prop_harmonic(W, lab_idx, y_l):
    """Harmonic solution f_u = (I - P_uu)^{-1} P_ul f_l with P = D^{-1} W (random-walk normalisation)."""
    n = len(W)
    unl = np.setdiff1d(np.arange(n), lab_idx)
    P = W / W.sum(1, keepdims=True)
    f = np.zeros(n)
    f[lab_idx] = y_l
    A = np.eye(len(unl)) - P[np.ix_(unl, unl)]
    f[unl] = np.linalg.solve(A, P[np.ix_(unl, lab_idx)] @ y_l)
    return f


def label_prop_book(W, lab_idx, y_l, C=2, iters=500, record=None):
    """The algorithm of Section 19.3.4 as written: column-normalised T (19.26), Y <- T Y, renormalise the rows,
    clamp the labeled rows. Returns the (n, C) label matrix after `iters` sweeps."""
    n = len(W)
    T = W / W.sum(0, keepdims=True)
    Y = np.full((n, C), 1.0 / C)
    Y[lab_idx] = np.eye(C)[y_l]
    hist = []
    for it in range(iters):
        Y = T @ Y
        Y = Y / np.maximum(Y.sum(1, keepdims=True), 1e-300)
        Y[lab_idx] = np.eye(C)[y_l]
        if record is not None:
            hist.append(Y.copy())
    return (Y, hist) if record is not None else Y


def nw_extend(Xq, X, f, sigma):
    """Nadaraya-Watson extension of the node values f to new points (inductive prediction)."""
    d2 = ((Xq[:, None, :] - X[None, :, :]) ** 2).sum(-1)
    K = np.exp(-d2 / (2 * sigma * sigma))
    return (K @ f) / np.maximum(K.sum(1), 1e-300)


# semi-supervised Gaussian mixture classifier (EM with the labeled rows clamped), Section 19.3 and gap 19.6
def gmm_logpdf(X, mu, S):
    return common.gauss_logpdf(X, mu, S)


def ss_gmm_fit(Xl, yl, Xu, iters=50, shared=False, reg=1e-3):
    """Two-class Gaussian mixture classifier fitted by EM on labeled + unlabeled data. Returns the parameters
    and the trace of the observed-data log likelihood sum_L log p(x, y) + sum_U log p(x)."""
    def mstep(R_l, R_u, X, Xu_):
        Xall = np.vstack([X, Xu_])
        R = np.vstack([R_l, R_u])
        Nk = R.sum(0)
        pi = Nk / Nk.sum()
        mu = (R.T @ Xall) / Nk[:, None]
        S = []
        for k in range(2):
            d = Xall - mu[k]
            S.append((R[:, k, None] * d).T @ d / Nk[k] + reg * np.eye(Xall.shape[1]))
        if shared:
            Ssh = (Nk[0] * S[0] + Nk[1] * S[1]) / Nk.sum()
            S = [Ssh, Ssh]
        return pi, mu, S

    def loglik(pi, mu, S):
        ll = 0.0
        for k in range(2):
            m = yl == k
            ll += np.sum(np.log(pi[k]) + gmm_logpdf(Xl[m], mu[k], S[k]))
        lu = np.stack([np.log(pi[k]) + gmm_logpdf(Xu, mu[k], S[k]) for k in range(2)], 1)
        return ll + np.sum(special.logsumexp(lu, axis=1))

    R_l = np.eye(2)[yl]
    pi, mu, S = mstep(R_l, np.zeros((0, 2)), Xl, Xu[:0])      # labeled data only
    trace = [loglik(pi, mu, S)]
    pars = [(pi, mu, S)]
    for _ in range(iters):
        lu = np.stack([np.log(pi[k]) + gmm_logpdf(Xu, mu[k], S[k]) for k in range(2)], 1)
        R_u = np.exp(lu - special.logsumexp(lu, axis=1, keepdims=True))
        pi, mu, S = mstep(R_l, R_u, Xl, Xu)
        trace.append(loglik(pi, mu, S))
        pars.append((pi, mu, S))
    return dict(pi=pi, mu=mu, S=S, trace=np.array(trace), first=pars[0])


def gmm_posterior(X, pi, mu, S):
    lu = np.stack([np.log(pi[k]) + gmm_logpdf(X, mu[k], S[k]) for k in range(2)], 1)
    return np.exp(lu - special.logsumexp(lu, axis=1, keepdims=True))[:, 1]


# =========================================================================== 19.4 active learning
LAM2 = np.pi / 8.0                      # sigmoid(a) ~ Phi(sqrt(pi/8) a)
C2 = np.pi * np.log(2.0) / 2.0          # the constant C^2 of the BALD approximation


def laplace_logreg(Phi, y, alpha=1.0, iters=50):
    """Laplace approximation to the Bayesian logistic regression posterior with prior N(0, alpha^{-1} I) on all
    weights: returns the MAP w and the posterior covariance (Hessian^{-1})."""
    d = Phi.shape[1]
    w = np.zeros(d)
    for _ in range(iters):
        p = sigmoid(Phi @ w)
        g = Phi.T @ (p - y) + alpha * w
        H = (Phi * (p * (1 - p))[:, None]).T @ Phi + alpha * np.eye(d)
        step = np.linalg.solve(H, g)
        w = w - step
        if np.max(np.abs(step)) < 1e-12:
            break
    p = sigmoid(Phi @ w)
    H = (Phi * (p * (1 - p))[:, None]).T @ Phi + alpha * np.eye(d)
    return w, np.linalg.inv(H)


def logit_moments(Phi, w, Sigma):
    m = Phi @ w
    s2 = np.einsum("nd,de,ne->n", Phi, Sigma, Phi)
    return m, s2


def predictive_probit(m, s2):
    """Posterior predictive p(y=1|x, D) = sigmoid(m / sqrt(1 + pi s2 / 8)) (Section 10.5.2)."""
    return sigmoid(m / np.sqrt(1.0 + np.pi * s2 / 8.0))


BENT = 1.8521        # width of the Gaussian bump fitted to the entropy of Ber(sigmoid(a)): H(a) ~ ln2 exp(-a^2 / (2 BENT^2))


def bald_closed_form(m, s2):
    """BALD score (19.51) in nats with a closed-form expected entropy: the entropy of Ber(sigmoid(a)) is approximated by
    the Gaussian bump ln2 exp(-a^2 / (2 b^2)) (b fitted by least squares, the probit version of Houlsby et al., 2011,
    would use b^2 = 4 ln 2), so that for a ~ N(m, s2)
    E[H] ~ ln2 * b / sqrt(s2 + b^2) * exp(-m^2 / (2 (s2 + b^2))).
    The predictive entropy uses the probit predictive. The error against Monte Carlo is below 0.03 nats."""
    pbar = predictive_probit(m, s2)
    Eh = LOG2 * BENT / np.sqrt(s2 + BENT ** 2) * np.exp(-m ** 2 / (2 * (s2 + BENT ** 2)))
    return binent(pbar) - Eh


def bald_mc(m, s2, n=200000, seed=0):
    rng = np.random.default_rng(seed)
    z = rng.standard_normal(n)
    out = np.empty(len(m))
    for i in range(len(m)):
        a = m[i] + np.sqrt(s2[i]) * z
        out[i] = binent(np.mean(sigmoid(a))) - np.mean(entropy_logit(a))
    return out


def utility(kind, m, s2):
    p = predictive_probit(m, s2)
    if kind == "entropy":
        return binent(p)
    if kind == "bald":
        return bald_closed_form(m, s2)
    if kind == "margin":
        return -np.abs(2 * p - 1)
    raise ValueError(kind)


def active_run(Phi_pool, y_pool, Phi_test, y_test, init_idx, n_queries, kind, alpha=1.0, rng=None, perm=None):
    """Pool-based active learning with a Laplace logistic model. Returns the labeled indices and the test
    accuracy and test log loss after each query (length n_queries + 1)."""
    lab = list(init_idx)
    acc, ll = [], []
    for step in range(n_queries + 1):
        w, S = laplace_logreg(Phi_pool[lab], y_pool[lab].astype(float), alpha)
        mt, s2t = logit_moments(Phi_test, w, S)
        pt = predictive_probit(mt, s2t)
        acc.append(np.mean((pt > 0.5) == y_test))
        ll.append(-np.mean(np.where(y_test == 1, np.log(pt + 1e-12), np.log(1 - pt + 1e-12))))
        if step == n_queries:
            break
        cand = np.setdiff1d(np.arange(len(y_pool)), lab)
        if kind == "random":
            if perm is not None:                       # a fixed random order (used by the browser demonstration)
                j = next(i for i in perm if i not in lab)
            else:
                j = rng.choice(cand)
        else:
            m, s2 = logit_moments(Phi_pool[cand], w, S)
            u = utility(kind, m, s2)
            j = cand[int(np.argmax(u))]
        lab.append(int(j))
    return lab, np.array(acc), np.array(ll)


# =========================================================================== 19.2.4 contrastive
def info_nce_gaussian(rho, N, n_batches, seed=0):
    """Monte Carlo estimate of log N - L_NCE for (x, y) bivariate Gaussian with correlation rho, using the optimal
    critic f(x, y) = log p(y|x)/p(y); returns the mean and standard error."""
    rng = np.random.default_rng(seed)
    out = np.empty(n_batches)
    s = 1 - rho ** 2
    for b in range(n_batches):
        x = rng.standard_normal(N)
        y = rho * x + np.sqrt(s) * rng.standard_normal(N)
        F = -0.5 * np.log(s) - (y[None, :] - rho * x[:, None]) ** 2 / (2 * s) + y[None, :] ** 2 / 2   # F[i, j] = f(x_i, y_j)
        L = -np.mean(np.diag(F) - special.logsumexp(F, axis=1))
        out[b] = np.log(N) - L
    return out.mean(), out.std() / np.sqrt(n_batches)


def info_nce_loss(F):
    """Row-wise InfoNCE loss, mean over rows, for the score matrix F (F[i, i] are the positive pairs)."""
    return -np.mean(np.diag(F) - special.logsumexp(F, axis=1))


def clip_loss(L):
    """The symmetric CLIP loss (19.12) for the logit matrix L (rows: images, columns: texts), divided by N."""
    n = len(L)
    ce_rows = -np.mean(np.diag(L) - special.logsumexp(L, axis=1))
    ce_cols = -np.mean(np.diag(L) - special.logsumexp(L, axis=0))
    return 0.5 * (ce_rows + ce_cols)


# =========================================================================== 19.3.5 consistency, 19.3.7 distillation
def softmax(z, axis=-1):
    z = z - np.max(z, axis=axis, keepdims=True)
    e = np.exp(z)
    return e / e.sum(axis=axis, keepdims=True)


def distill_loss(zs, zt, tau):
    """Cross entropy (19.45) of the student's tempered softmax against the teacher's, one example."""
    pt, ps = softmax(zt / tau), softmax(zs / tau)
    return -np.sum(pt * np.log(ps))


# =========================================================================== 19.5 meta-learning: sinusoid regression
SIN_H = 40


def sin_task_batch(rng, T, K, Kq=None, amp=(0.1, 5.0), phase=(0.0, np.pi), xr=5.0):
    """T tasks y = A sin(x - b); K support and Kq query points each, x ~ U[-xr, xr]."""
    Kq = K if Kq is None else Kq
    A = rng.uniform(*amp, T)
    b = rng.uniform(*phase, T)
    xs = rng.uniform(-xr, xr, (T, K))
    xq = rng.uniform(-xr, xr, (T, Kq))
    return A, b, xs, A[:, None] * np.sin(xs - b[:, None]), xq, A[:, None] * np.sin(xq - b[:, None])


def mlp_init(seed, H=SIN_H):
    """Initial weights of the 1-H-H-1 ReLU network (numpy arrays)."""
    r = np.random.default_rng(seed)
    def lin(i, o):
        lim = np.sqrt(1.0 / i)
        return r.uniform(-lim, lim, (i, o)), r.uniform(-lim, lim, o)
    W1, b1 = lin(1, H)
    W2, b2 = lin(H, H)
    W3, b3 = lin(H, 1)
    return [W1, b1, W2, b2, W3, b3]


def mlp_forward(P, x):
    """x: (n,) -> (n,)"""
    h1 = np.maximum(x[:, None] @ P[0] + P[1], 0)
    h2 = np.maximum(h1 @ P[2] + P[3], 0)
    return (h2 @ P[4] + P[5])[:, 0]


def mlp_loss_grad(P, x, y):
    """Mean squared error (1/n) sum (f - y)^2 and its gradient, by hand (matches the JavaScript demo)."""
    n = len(x)
    z1 = x[:, None] @ P[0] + P[1]
    h1 = np.maximum(z1, 0)
    z2 = h1 @ P[2] + P[3]
    h2 = np.maximum(z2, 0)
    f = (h2 @ P[4] + P[5])[:, 0]
    r = f - y
    loss = np.mean(r ** 2)
    d3 = (2.0 / n) * r[:, None]
    g5 = d3.sum(0)
    g4 = h2.T @ d3
    dh2 = d3 @ P[4].T
    dz2 = dh2 * (z2 > 0)
    g3 = dz2.sum(0)
    g2 = h1.T @ dz2
    dh1 = dz2 @ P[2].T
    dz1 = dh1 * (z1 > 0)
    g1 = dz1.sum(0)
    g0 = x[:, None].T @ dz1
    return loss, [g0, g1, g2, g3, g4, g5]


def adapt(P, x, y, steps, lr):
    Q = [p.copy() for p in P]
    for _ in range(steps):
        _, g = mlp_loss_grad(Q, x, y)
        Q = [q - lr * gg for q, gg in zip(Q, g)]
    return Q


def _tparams(P):
    import torch
    return [torch.tensor(p, dtype=torch.float64) for p in P]


def maml_train(seed=0, iters=3000, T=16, K=10, inner_lr=0.01, inner_steps=1, meta_lr=1e-3, H=SIN_H, first_order=False,
               pretrain=False, verbose=False):
    """Meta-train the initialisation of the ReLU network by second-order MAML (Algorithm of Finn et al. 2017):
    the meta-loss is the mean query loss after `inner_steps` gradient steps on the support set. With pretrain=True the
    network is instead trained, with the same optimiser, on the plain pooled regression loss (no inner loop)."""
    import torch
    torch.manual_seed(seed)
    P0 = mlp_init(seed, H)
    theta = [torch.tensor(p, dtype=torch.float64, requires_grad=True) for p in P0]
    opt = torch.optim.Adam(theta, lr=meta_lr)
    rng = np.random.default_rng(seed + 1000)

    def fwd(Pb, x):          # Pb: list of batched params (T, ...), x: (T, n)
        h1 = torch.relu(x[:, :, None] * Pb[0] + Pb[1][:, None, :])          # (T, n, H)
        h2 = torch.relu(torch.bmm(h1, Pb[2]) + Pb[3][:, None, :])
        return (torch.bmm(h2, Pb[4]) + Pb[5][:, None, :])[:, :, 0]

    hist = []
    for it in range(iters):
        A, b, xs, ys, xq, yq = sin_task_batch(rng, T, K)
        xs, ys, xq, yq = [torch.tensor(a, dtype=torch.float64) for a in (xs, ys, xq, yq)]
        if pretrain:
            xa, ya = torch.cat([xs, xq], 1), torch.cat([ys, yq], 1)
            Pb = [t.unsqueeze(0).expand(T, *t.shape) for t in theta]
            loss = ((fwd(Pb, xa) - ya) ** 2).mean()
        else:
            Pb = [t.unsqueeze(0).expand(T, *t.shape) for t in theta]
            for _ in range(inner_steps):
                l_in = ((fwd(Pb, xs) - ys) ** 2).mean(1).sum()
                g = torch.autograd.grad(l_in, Pb, create_graph=not first_order)
                Pb = [p - inner_lr * gg for p, gg in zip(Pb, g)]
            loss = ((fwd(Pb, xq) - yq) ** 2).mean()
        opt.zero_grad()
        loss.backward()
        opt.step()
        hist.append(float(loss))
        if verbose and (it + 1) % 500 == 0:
            print(it + 1, np.mean(hist[-200:]))
    return [t.detach().numpy().copy() for t in theta], np.array(hist)


def sin_eval(P, seed=123, n_tasks=200, K=10, steps=(0, 1, 5, 10), lr=0.01, xr=5.0):
    """Mean test MSE over tasks on a dense grid after k gradient steps on K support points."""
    rng = np.random.default_rng(seed)
    A, b, xs, ys, _, _ = sin_task_batch(rng, n_tasks, K)
    xg = np.linspace(-xr, xr, 201)
    out = {k: [] for k in steps}
    for t in range(n_tasks):
        Q = [p.copy() for p in P]
        yt = A[t] * np.sin(xg - b[t])
        for k in range(max(steps) + 1):
            if k in out:
                out[k].append(np.mean((mlp_forward(Q, xg) - yt) ** 2))
            _, g = mlp_loss_grad(Q, xs[t], ys[t])
            Q = [q - lr * gg for q, gg in zip(Q, g)]
    return {k: float(np.mean(v)) for k, v in out.items()}


def maml_meta_gradient_fd(theta, xs, ys, xq, yq, alpha, eps=1e-6):
    """Finite-difference meta-gradient of L_q(theta - alpha grad L_s(theta)) with respect to one weight array
    (used in the checks)."""
    def meta(Pm):
        _, g = mlp_loss_grad(Pm, xs, ys)
        Q = [p - alpha * gg for p, gg in zip(Pm, g)]
        return mlp_loss_grad(Q, xq, yq)[0]
    return meta


# =========================================================================== 19.2 transfer learning on digits
def mnist_small(n_train=12000, seed=0):
    X, y = common.mnist(True)
    Xt, yt = common.mnist(False)
    r = np.random.default_rng(seed)
    idx = r.permutation(len(X))[:n_train]
    return X[idx], y[idx], Xt, yt


def _torch_mlp(torch, sizes, seed):
    torch.manual_seed(seed)
    layers = []
    for i in range(len(sizes) - 1):
        layers.append(torch.nn.Linear(sizes[i], sizes[i + 1]))
        if i < len(sizes) - 2:
            layers.append(torch.nn.ReLU())
    return torch.nn.Sequential(*layers)


def train_sgd(torch, model, X, y, epochs, lr, batch=64, wd=0.0, seed=0, params=None, loss_fn=None, mask_p=0.0):
    g = torch.Generator().manual_seed(seed)
    opt = torch.optim.Adam(params if params is not None else model.parameters(), lr=lr, weight_decay=wd)
    n = len(X)
    for _ in range(epochs):
        perm = torch.randperm(n, generator=g)
        for i in range(0, n, batch):
            idx = perm[i:i + batch]
            xb = X[idx]
            if loss_fn is None:
                loss = torch.nn.functional.cross_entropy(model(xb), y[idx])
            else:
                loss = loss_fn(model, xb, g)
            opt.zero_grad()
            loss.backward()
            opt.step()
    return model


class TransferStudy:
    """Self-supervised pre-training and few-label fine-tuning on noisy MNIST digits. Every image gets independent
    N(0, sigma^2) noise added to each pixel (sigma = 0.8), so that labels are hard to use directly. The trunk
    (784-256-64, ReLU) is pre-trained, without labels, to reconstruct an image from a copy with half the pixels masked
    (an imputation task, Section 19.2.4.1) on 40000 unlabeled images. The target task is 10-way classification from n
    labeled images per class, evaluated on 5000 test images. Methods: raw-pixel logistic regression, an MLP trained from
    scratch, a logistic-regression probe on the frozen pre-trained features, and fine-tuning (new head first, then all
    layers with a small learning rate), Section 19.2.1."""

    def __init__(self, seed=0, sigma=0.8, n_unlab=40000, epochs_pre=8):
        self.torch = T = common.torch_setup(seed)
        X, y, Xt, yt = mnist_small(50000, seed)
        rg = np.random.default_rng(5)
        X = X + sigma * rg.standard_normal(X.shape)
        Xt = Xt + sigma * rg.standard_normal(Xt.shape)
        self.Xu = X[:n_unlab]
        self.Xl_all, self.yl_all = X[n_unlab:], y[n_unlab:]
        self.Xte, self.yte = Xt[:5000], yt[:5000]
        self.trunk = _torch_mlp(T, [784, 256, 64], seed + 1)
        dec = _torch_mlp(T, [64, 256, 784], seed + 2)
        ae = T.nn.Sequential(self.trunk, T.nn.ReLU(), dec)

        def mse_masked(model, xb, g):
            mask = (T.rand(xb.shape, generator=g) > 0.5).float()
            return ((model(xb * mask) - xb) ** 2).mean()
        train_sgd(T, ae, T.tensor(self.Xu, dtype=T.float32), None, epochs_pre, 1e-3, seed=seed, loss_fn=mse_masked)

    def feats(self, X):
        T = self.torch
        with T.no_grad():
            return T.relu(self.trunk(T.tensor(X, dtype=T.float32))).numpy()

    def run(self, n_per_class, seed, method):
        import copy
        from sklearn.linear_model import LogisticRegression
        T = self.torch
        t = lambda a, dt=T.float32: T.tensor(a, dtype=dt)
        r = np.random.default_rng(seed)
        idx = np.concatenate([r.permutation(np.where(self.yl_all == c)[0])[:n_per_class] for c in range(10)])
        Xl, yl = self.Xl_all[idx], self.yl_all[idx]
        if method == "raw":
            return float(LogisticRegression(C=1.0, max_iter=1000).fit(Xl, yl).score(self.Xte, self.yte))
        if method == "frozen":
            F, Ft = self.feats(Xl), self.feats(self.Xte)
            sc = F.std() + 1e-8
            return float(LogisticRegression(C=1.0, max_iter=2000).fit(F / sc, yl).score(Ft / sc, self.yte))
        if method == "scratch":
            net = _torch_mlp(T, [784, 256, 64, 10], 100 + seed)
            train_sgd(T, net, t(Xl), t(yl, T.long), 100, 1e-3, batch=len(Xl), seed=seed)
        elif method == "finetune":
            T.manual_seed(seed)
            head = T.nn.Linear(64, 10)
            net = T.nn.Sequential(copy.deepcopy(self.trunk), T.nn.ReLU(), head)
            train_sgd(T, net, t(Xl), t(yl, T.long), 100, 1e-2, batch=len(Xl), seed=seed, params=head.parameters())
            train_sgd(T, net, t(Xl), t(yl, T.long), 30, 1e-4, batch=len(Xl), seed=seed)
        else:
            raise ValueError(method)
        with T.no_grad():
            return float(np.mean(net(t(self.Xte)).argmax(1).numpy() == self.yte))


# =========================================================================== data sets shared by figures, checks and demos
def ssl_moons():
    """Two moons (noise 0.1): 300 unlabeled training points and 400 test points."""
    X, y = moons(700, 0.1, 3)
    return X[:300], y[:300], X[300:], y[300:]


def ssl_blobs():
    """Two elongated Gaussian clusters (separation 1.6): 300 training and 400 test points."""
    X, y = two_clusters(350, 5, 1.6)
    p = np.random.default_rng(0).permutation(700)
    X, y = X[p], y[p]
    return X[:300], y[:300], X[300:], y[300:]


def feat_linear(X):
    return np.hstack([np.ones((len(X), 1)), X])


SSL_CFG = {
    "moons": dict(data=ssl_moons, feat=rbf_phi, lam=0.1, sigma=0.25, tau=0.8, beta=0.3),
    "blobs": dict(data=ssl_blobs, feat=feat_linear, lam=0.1, sigma=0.5, tau=0.8, beta=1.0),
}


def pick_labeled(y, nl, seed):
    """nl labeled points per class: the first nl of a seeded permutation of each class (seed >= 1)."""
    r = np.random.default_rng(seed)
    idx = []
    for c in (0, 1):
        idx += list(r.permutation(np.where(y == c)[0])[:nl])
    return np.array(idx)


def run_ssl(name, method, nl, seed, tau=None, beta=None, sigma=None, rounds=10, iters=3000):
    """Returns (test accuracy, accuracy on the unlabeled training points, weight vector or node values)."""
    cfg = SSL_CFG[name]
    Xu, yu, Xt, yt = cfg["data"]()
    Pu, Pt = cfg["feat"](Xu), cfg["feat"](Xt)
    li = pick_labeled(yu, nl, seed)
    ui = np.setdiff1d(np.arange(len(yu)), li)
    tau = cfg["tau"] if tau is None else tau
    beta = cfg["beta"] if beta is None else beta
    sigma = cfg["sigma"] if sigma is None else sigma
    if method == "supervised":
        w = fit_logreg(Pu[li], yu[li].astype(float), cfg["lam"])
    elif method == "self-training":
        w = self_training(Pu[li], yu[li], Pu[ui], tau, rounds, cfg["lam"])
    elif method == "entropy":
        w = entropy_min(Pu[li], yu[li], Pu[ui], beta, cfg["lam"], 0.5, iters)
    elif method == "propagation":
        W = rbf_weights(Xu, sigma)
        f = label_prop_harmonic(W, li, yu[li].astype(float))
        ft = nw_extend(Xt, Xu, f, sigma)
        return float(np.mean((ft > 0.5) == yt)), float(np.mean((f[ui] > 0.5) == yu[ui])), f
    else:
        raise ValueError(method)
    return float(np.mean((predict_p(Pt, w) > 0.5) == yt)), float(np.mean((predict_p(Pu[ui], w) > 0.5) == yu[ui])), w


def regions(w, feat, xlim, ylim, n=60):
    gx, gy = np.linspace(*xlim, n), np.linspace(*ylim, n)
    G = np.array([[a, b] for b in gy for a in gx])
    return gx, gy, predict_p(feat(G), w).reshape(n, n)


# =========================================================================== figures
def _pl(ax, letter):
    """Panel letter (a), (b), ... above the top-left corner of the axes."""
    ax.set_title("(%s)" % letter, loc="left", fontsize=10, pad=3)


def _ax(ax, xl=None, yl=None, equal=False):
    ax.tick_params(length=2.5)
    if xl:
        ax.set_xlabel(xl)
    if yl:
        ax.set_ylabel(yl)
    if equal:
        ax.set_aspect("equal")


def fig_19_1(out=None):
    """Random crops, zooms and flips of an image (data augmentation)."""
    from sklearn.datasets import load_sample_images
    img = load_sample_images().images[0]
    rng = np.random.default_rng(3)
    fig = plt.figure(figsize=(8.2, 3.3))
    gs = fig.add_gridspec(2, 5, width_ratios=[2.1, 1, 1, 1, 1], wspace=0.05, hspace=0.06)
    ax = fig.add_subplot(gs[:, 0])
    ax.imshow(img)
    ax.set_title("original", fontsize=9)
    ax.axis("off")
    for k in range(8):
        a = fig.add_subplot(gs[k // 4, 1 + k % 4])
        a.imshow(random_resized_crop(img, rng))
        a.axis("off")
    savefig(fig, CH, "fig19-1", out)


def gmm_trial(s, sep=0.9, nl=2):
    """One draw of the semi-supervised picture: 2 nl labeled and 300 - 2 nl unlabeled points, test accuracy of the
    generative (shared covariance) classifier fitted to the labeled points only and by EM with the unlabeled ones."""
    X, y = two_clusters(150, 1000 + s, sep)
    li = pick_labeled(y, nl, s + 1)
    ui = np.setdiff1d(np.arange(len(y)), li)
    r = ss_gmm_fit(X[li], y[li], X[ui], 40, shared=True)
    Xt, yt = two_clusters(2000, 5000 + s, sep)
    a0 = np.mean((gmm_posterior(Xt, *r["first"]) > 0.5) == yt)
    a1 = np.mean((gmm_posterior(Xt, r["pi"], r["mu"], r["S"]) > 0.5) == yt)
    return X, y, li, ui, r, (a0, a1)


def fig_19_8(out=None):
    """Labeled data only versus labeled plus unlabeled data (generative classifier fitted by EM)."""
    accs = np.array([gmm_trial(s)[5] for s in range(200)])
    gain = accs[:, 1] - accs[:, 0]
    s = int(np.argmin(np.abs(gain - np.median(gain))))          # the draw with the median gain (not cherry-picked)
    X, y, li, ui, r, _ = gmm_trial(s)
    fig, axes = panel_grid(1, 2, size=(3.3, 3.1))
    gx = np.linspace(-4.4, 4.4, 120)
    G = np.array([[a, b] for b in gx for a in gx])
    Xt, yt = two_clusters(2000, 5000 + s, 0.9)
    for k, ax in enumerate(axes[0]):
        pars = r["first"] if k == 0 else (r["pi"], r["mu"], r["S"])
        P = gmm_posterior(G, *pars).reshape(120, 120)
        ax.contour(gx, gx, P, [0.5], colors="k", linewidths=1.6)
        if k == 1:
            ax.scatter(X[ui, 0], X[ui, 1], s=7, c="#9a9a9a", alpha=0.7, lw=0)
        ax.scatter(X[li][y[li] == 0, 0], X[li][y[li] == 0, 1], s=46, c="k", marker="o", zorder=3)
        ax.scatter(X[li][y[li] == 1, 0], X[li][y[li] == 1, 1], s=46, c="w", ec="k", marker="o", zorder=3)
        acc = np.mean((gmm_posterior(Xt, *pars) > 0.5) == yt)
        ax.set_title(["labeled points only", "with unlabeled points"][k] + " (test acc %.3f)" % acc, fontsize=8.5)
        ax.set_xlim(-4.4, 4.4)
        ax.set_ylim(-4.4, 4.4)
        _ax(ax, equal=True)
        _pl(ax, "ab"[k])
    savefig(fig, CH, "fig19-8", out)
    return accs


def fig_19_9(out=None):
    """Entropy minimisation, self-training and sharpened (T = 1/2) losses for a binary problem."""
    a = np.linspace(-8, 8, 801)
    p = sigmoid(a)
    q = p ** 2 / (p ** 2 + (1 - p) ** 2)
    sharp = -(q * np.log(np.clip(p, 1e-300, 1)) + (1 - q) * np.log(np.clip(1 - p, 1e-300, 1)))
    fig, ax = plt.subplots(figsize=(4.4, 2.9))
    ax.plot(a, entropy_logit(a), color=BLUE, label="entropy minimisation (19.17)")
    ax.plot(a, softplus(-np.abs(a)), color=RED, label="self-training (19.16)")
    ax.plot(a, sharp, color=GREEN, ls="--", label="sharpened ($T=1/2$)")
    ax.set_ylim(0, 0.9)
    ax.set_xlim(-8, 8)
    ax.legend(loc="upper center", fontsize=7.5, frameon=False)
    _ax(ax, "logit $a$ of the model", "loss on one unlabeled point")
    savefig(fig, CH, "fig19-9", out)


def fig_19_10(out=None):
    """Entropy minimisation prefers a boundary in the low-density gap between two clusters."""
    rng = np.random.default_rng(2)
    X = np.vstack([rng.multivariate_normal([-1.6, 0], 0.30 * np.eye(2), 90), rng.multivariate_normal([1.6, 0], 0.30 * np.eye(2), 90)])
    kappa = 4.0
    specs = [("boundary through a cluster", np.array([1.0, 0.3]), 1.4), ("boundary in the gap", np.array([1.0, 0.0]), 0.0)]
    fig, axes = panel_grid(1, 2, size=(3.4, 3.1))
    gx = np.linspace(-3.2, 3.2, 100)
    G = np.array([[a, b] for b in gx for a in gx])
    for k, (title, n, b) in enumerate(specs):
        n = n / np.linalg.norm(n)
        ax = axes[0, k]
        P = sigmoid(kappa * (G @ n + b)).reshape(100, 100)
        ax.contourf(gx, gx, P, levels=[0, 0.5, 1], colors=["#ffd9d9", "#d9e4ff"], alpha=1)
        ax.contour(gx, gx, P, [0.5], colors="k", linewidths=1.4)
        pu = sigmoid(kappa * (X @ n + b))
        ax.scatter(X[:, 0], X[:, 1], s=8, c="#444444", lw=0)
        H = binent(pu).mean()
        ax.set_title("%s\nmean entropy %.3f nats" % (title, H), fontsize=8.5)
        ax.set_xlim(-3.2, 3.2)
        ax.set_ylim(-3.2, 3.2)
        _ax(ax, equal=True)
        _pl(ax, "ab"[k])
    savefig(fig, CH, "fig19-10", out)


def fig_19_11(out=None):
    """Squared-error and KL-divergence consistency losses against the logit of the perturbed prediction."""
    a = np.linspace(-5, 5, 501)
    p = sigmoid(a)
    fig, ax = plt.subplots(figsize=(4.4, 2.9))
    ax.plot(a, 2 * (1 - p) ** 2, color=BLUE, label="squared error $\\|q-p\\|^2$")
    ax.plot(a, softplus(-a), color=RED, label="KL divergence $D_{KL}(q\\|p)$")
    ax.set_ylim(0, 5)
    ax.set_xlim(-5, 5)
    ax.legend(loc="upper right", fontsize=8, frameon=False)
    _ax(ax, "logit of the perturbed input (reference output: class 1)", "loss")
    savefig(fig, CH, "fig19-11", out)


def aug_ridge_experiment(sig_list, M_list, reps=40, N=40, D=15, seed=0):
    """Test error of least squares on Gaussian-noise-augmented data against ridge with lambda = N sigma^2."""
    rng = np.random.default_rng(seed)
    out = {"ridge": np.zeros((reps, len(sig_list))), "aug": {M: np.zeros((reps, len(sig_list))) for M in M_list}}
    for r in range(reps):
        w = rng.standard_normal(D) / np.sqrt(D)
        X = rng.standard_normal((N, D))
        y = X @ w + 0.5 * rng.standard_normal(N)
        Xte = rng.standard_normal((2000, D))
        for j, s in enumerate(sig_list):
            wr = np.linalg.solve(X.T @ X + N * s * s * np.eye(D), X.T @ y)
            out["ridge"][r, j] = np.mean((Xte @ (wr - w)) ** 2)
            for M in M_list:
                Xa = np.vstack([X + s * rng.standard_normal((N, D)) for _ in range(M)])
                ya = np.tile(y, M)
                wa = np.linalg.lstsq(Xa, ya, rcond=None)[0]
                out["aug"][M][r, j] = np.mean((Xte @ (wa - w)) ** 2)
    return out


def rotation_variance(K_list, N=50, reps=4000, radius=1.2, seed=0):
    """Variance of the empirical 0-1 risk of a fixed non-invariant classifier on rotation-invariant data, plain and
    with K random rotations per training point. Returns (plain var, {K: var}, limit var(g)/N)."""
    rng = np.random.default_rng(seed)
    a = np.array([1.0, 0.4])
    c = 0.1

    def err(X, y):
        return ((X @ a + c > 0).astype(int) != y).astype(float)

    def sample(n):
        X = rng.standard_normal((n, 2))
        return X, (np.linalg.norm(X, axis=1) > radius).astype(int)

    def rot(X, th):
        cth, sth = np.cos(th), np.sin(th)
        return np.stack([cth * X[..., 0] - sth * X[..., 1], sth * X[..., 0] + cth * X[..., 1]], -1)

    plain = np.empty(reps)
    aug = {K: np.empty(reps) for K in K_list}
    for r in range(reps):
        X, y = sample(N)
        plain[r] = err(X, y).mean()
        for K in K_list:
            th = rng.uniform(0, 2 * np.pi, (N, K))
            Xr = rot(X[:, None, :], th)
            aug[K][r] = np.mean(((Xr @ a + c > 0).astype(int) != y[:, None]).astype(float))
    # variance of g(x, y) = E_theta loss, by quadrature over rotation angles
    X, y = sample(400000)
    th = np.linspace(0, 2 * np.pi, 361)[:-1]
    g = np.zeros(len(X))
    for t in th:
        g += ((rot(X, t) @ a + c > 0).astype(int) != y)
    g /= len(th)
    return plain.var(), {K: v.var() for K, v in aug.items()}, g.var() / N


def fig_19_S1(out=None):
    """Gaussian-noise augmentation is ridge regression, and invariant augmentation reduces variance."""
    sig = np.linspace(0, 0.8, 9)
    Ms = (1, 4, 50)
    R = aug_ridge_experiment(sig, Ms)
    pv, av, lim = rotation_variance((1, 2, 4, 8, 16, 64), N=50, reps=3000)
    fig, axes = panel_grid(1, 2, size=(3.5, 2.9))
    ax = axes[0, 0]
    ax.plot(sig, R["ridge"].mean(0), color="k", lw=2, label="ridge, $\\lambda=N\\sigma^2$")
    for M, c in zip(Ms, (RED, ORANGE, BLUE)):
        ax.plot(sig, R["aug"][M].mean(0), color=c, marker="o", ms=3.5, lw=1.1, label="augmented, $M=%d$ copies" % M)
    ax.legend(fontsize=7.5, frameon=False)
    _ax(ax, "noise level $\\sigma$", "excess test error")
    _pl(ax, "a")
    ax = axes[0, 1]
    Ks = sorted(av)
    ax.axhline(pv, color="k", ls="--", lw=1, label="no augmentation")
    ax.plot(Ks, [av[K] for K in Ks], color=BLUE, marker="o", ms=3.5, label="$K$ random rotations")
    ax.axhline(lim, color=RED, ls=":", lw=1.4, label="limit $\\mathrm{var}(g)/N$")
    ax.set_xscale("log", base=2)
    ax.legend(fontsize=7.5, frameon=False)
    _ax(ax, "rotations per point $K$", "variance of the risk estimate")
    _pl(ax, "b")
    savefig(fig, CH, "fig19-S1", out)


def _transfer_cache():
    path = os.path.join(CACHE_DIR, "ch19_transfer.json")
    if os.path.exists(path):
        return json.load(open(path))
    ts = TransferStudy(0)
    res = {}
    ns = (1, 2, 5, 10, 20, 50)
    for m in ("raw", "scratch", "frozen", "finetune"):
        res[m] = [[ts.run(n, s, m) for s in range(5)] for n in ns]
    res["n"] = list(ns)
    os.makedirs(CACHE_DIR, exist_ok=True)
    json.dump(res, open(path, "w"))
    return res


def fig_19_S2(out=None):
    """Self-supervised pre-training on unlabeled noisy digits, then few labels."""
    R = _transfer_cache()
    fig, axes = panel_grid(1, 2, size=(3.5, 3.0))
    ax = axes[0, 0]
    names = {"raw": ("raw pixels, logistic regression", GREY), "scratch": ("MLP from scratch", ORANGE),
             "frozen": ("frozen features + logistic", BLUE), "finetune": ("fine-tuned", RED)}
    for m, (lab, c) in names.items():
        A = np.array(R[m])
        ax.errorbar(R["n"], A.mean(1), yerr=A.std(1) / np.sqrt(A.shape[1]), color=c, marker="o", ms=3.5, lw=1.2, capsize=2, label=lab)
    ax.set_xscale("log")
    ax.set_ylim(0.1, 0.95)
    ax.legend(fontsize=6.5, frameon=False, loc="upper left")
    _ax(ax, "labeled images per class", "test accuracy (10 classes)")
    _pl(ax, "a")
    # (b) the parameter count of adapters against full fine-tuning for one transformer-sized layer
    ax = axes[0, 1]
    D = 768
    full = 12 * D * D
    M = np.array([1, 2, 4, 8, 16, 32, 64, 128, 256])
    ad = 2 * (2 * D * M + D + M)
    ax.plot(M, 100 * ad / full, color=BLUE, marker="o", ms=3.5)
    ax.axhspan(1, 10, color="#ffe9c9", zorder=0)
    ax.text(1.2, 4.5, "1 to 10 percent", fontsize=7.5)
    ax.set_xscale("log", base=2)
    ax.set_yscale("log")
    _ax(ax, "adapter bottleneck size $M$", "new parameters (%)")
    _pl(ax, "b")
    savefig(fig, CH, "fig19-S2", out)
    return R


def fig_19_S3(out=None):
    """The InfoNCE bound and the effect of the temperature."""
    Ns = [2, 4, 8, 16, 32, 64, 128, 256, 512]
    fig, axes = panel_grid(1, 2, size=(3.5, 3.0))
    ax = axes[0, 0]
    for rho, c in ((0.5, BLUE), (0.9, GREEN), (0.99, RED)):
        est = [info_nce_gaussian(rho, N, max(40, 4000 // N * 2), seed=N)[0] for N in Ns]
        mi = -0.5 * np.log(1 - rho ** 2)
        ax.plot(Ns, est, color=c, marker="o", ms=3.5, label="$\\rho=%g$" % rho)
        ax.axhline(mi, color=c, ls=":", lw=1)
    ax.plot(Ns, np.log(Ns), color="k", ls="--", lw=1, label="$\\log N$")
    ax.set_xscale("log", base=2)
    ax.set_ylim(0, 3.2)
    ax.legend(fontsize=7.5, frameon=False, loc="upper left")
    _ax(ax, "batch size $N$", "$\\log N - L_{\\mathrm{NCE}}$ (nats)")
    _pl(ax, "a")
    ax = axes[0, 1]
    rng = np.random.default_rng(0)
    s = np.sort(rng.normal(0.0, 0.35, 63))[::-1]
    for tau, c in ((1.0, BLUE), (0.3, GREEN), (0.1, ORANGE), (0.03, RED)):
        z = np.r_[0.8, s] / tau
        w = softmax(z)[1:]
        w = w / w.sum()
        ax.plot(np.arange(1, 64), w, color=c, label="$\\tau=%g$" % tau)
    ax.set_yscale("log")
    ax.legend(fontsize=7.5, frameon=False)
    _ax(ax, "negative (hardest to easiest)", "share of the negatives' gradient")
    _pl(ax, "b")
    savefig(fig, CH, "fig19-S3", out)


def fig_19_S4(out=None):
    """Semi-supervised methods on the two-moons and two-blobs data, against the number of labels."""
    fig, axes = panel_grid(1, 2, size=(3.6, 3.0))
    ref = {}
    for ax, name, lab in zip(axes[0], ("moons", "blobs"), "ab"):
        nls = [1, 2, 3, 5, 10]
        for method, c in (("supervised", GREY), ("self-training", ORANGE), ("entropy", GREEN), ("propagation", RED)):
            if name == "blobs" and method == "propagation":
                pass
            acc = np.array([[run_ssl(name, method, nl, s)[0] for s in range(1, 21)] for nl in nls])
            ref[(name, method)] = acc
            ax.errorbar(nls, acc.mean(1), yerr=acc.std(1) / np.sqrt(acc.shape[1]), color=c, marker="o", ms=3.5, lw=1.2, capsize=2, label=method)
        ax.set_xscale("log")
        ax.set_xticks(nls)
        ax.set_xticklabels([str(n) for n in nls])
        if name == "moons":
            ax.legend(fontsize=7, frameon=False, loc="lower right")
        _ax(ax, "labeled points per class", "test accuracy")
        ax.set_title({"moons": "two moons, RBF features", "blobs": "two blobs, linear model"}[name], fontsize=8.5)
        _pl(ax, lab)
    savefig(fig, CH, "fig19-S4", out)
    return ref


def fig_19_S5(out=None):
    """Label propagation: the algorithm of Section 19.3.4 sweep by sweep, and the harmonic solution."""
    Xu, yu, Xt, yt = ssl_moons()
    li = pick_labeled(yu, 2, 1)
    W = rbf_weights(Xu, 0.25)
    Y, hist = label_prop_book(W, li, yu[li], 2, 400, record=True)
    f = label_prop_harmonic(W, li, yu[li].astype(float))
    ui = np.setdiff1d(np.arange(len(yu)), li)
    fig, axes = panel_grid(2, 3, size=(2.45, 2.2))
    for ax, t in zip(axes.ravel()[:4], (2, 10, 40, 400)):
        P = hist[t - 1][:, 1]
        ax.scatter(Xu[:, 0], Xu[:, 1], c=P, cmap="coolwarm", vmin=0, vmax=1, s=9, lw=0)
        ax.scatter(Xu[li, 0], Xu[li, 1], c=yu[li], cmap="coolwarm", vmin=0, vmax=1, s=60, ec="k", marker="s")
        ax.set_title("after %d sweeps" % t, fontsize=8.5)
        ax.set_xticks([])
        ax.set_yticks([])
    ax = axes[1, 1]
    ax.scatter(Xu[:, 0], Xu[:, 1], c=f, cmap="coolwarm", vmin=0, vmax=1, s=9, lw=0)
    ax.scatter(Xu[li, 0], Xu[li, 1], c=yu[li], cmap="coolwarm", vmin=0, vmax=1, s=60, ec="k", marker="s")
    ax.set_title("harmonic solution", fontsize=8.5)
    ax.set_xticks([])
    ax.set_yticks([])
    ax = axes[1, 2]
    acc = [np.mean((h[ui, 1] > 0.5) == yu[ui]) for h in hist]
    dif = [np.abs(hist[i][:, 1] - hist[i - 1][:, 1]).max() for i in range(1, len(hist))]
    ax.plot(np.arange(1, len(hist) + 1), acc, color=RED, label="accuracy on unlabeled")
    ax.axhline(np.mean((f[ui] > 0.5) == yu[ui]), color="k", ls="--", lw=1, label="harmonic")
    ax.set_xscale("log")
    ax.set_ylim(0.4, 1.02)
    ax.legend(fontsize=7, frameon=False, loc="lower right")
    _ax(ax, "sweeps", "")
    savefig(fig, CH, "fig19-S5", out)


def vat_setup(seed=0, d=6, h=12, C=4):
    import torch
    torch.manual_seed(seed)
    W1 = torch.randn(h, d, dtype=torch.float64) * 0.8
    W2 = torch.randn(C, h, dtype=torch.float64) * 0.8
    x = torch.randn(d, dtype=torch.float64)

    def logits(z):
        return W2 @ torch.tanh(W1 @ z)
    return torch, logits, x


def vat_hessian(seed=0):
    """Hessian at delta = 0 of KL(p(.|x) || p(.|x + delta)) for a small tanh network with a softmax output."""
    torch, logits, x = vat_setup(seed)
    p0 = torch.softmax(logits(x), 0).detach()

    def kl(delta):
        lp = torch.log_softmax(logits(x + delta), 0)
        return (p0 * (torch.log(p0) - lp)).sum()
    H = torch.autograd.functional.hessian(kl, torch.zeros_like(x))
    return torch, kl, H.numpy(), x


def vat_power_iterations(seed=0, steps=12, xi=1e-4):
    """The VAT recursion (19.28): d <- grad_delta KL(p(.|x) || p(.|x + xi d)), normalised each time. Returns the
    |cosine| between d and the top eigenvector of the Hessian after each step."""
    torch, kl, H, x = vat_hessian(seed)
    w, V = np.linalg.eigh(H)
    top = V[:, -1]
    rng = np.random.default_rng(seed)
    d = rng.standard_normal(len(x))
    cos = []
    for _ in range(steps):
        dd = torch.tensor(d * xi, dtype=torch.float64, requires_grad=True)
        g = torch.autograd.grad(kl(dd), dd)[0].numpy()
        d = g / np.linalg.norm(g)
        cos.append(abs(float(d @ top)))
    return np.array(cos), w


def fig_19_S6(out=None):
    """Virtual adversarial training is a power iteration; distillation with a temperature."""
    fig, axes = panel_grid(1, 2, size=(3.5, 2.9))
    ax = axes[0, 0]
    for seed, c in zip((0, 1, 2), (BLUE, GREEN, RED)):
        cos, w = vat_power_iterations(seed, 10)
        ratio = abs(w[-2] / w[-1])
        ax.plot(np.arange(1, 11), 1 - cos, color=c, marker="o", ms=3.5, label="network %d, $\\lambda_2/\\lambda_1=%.2f$" % (seed + 1, ratio))
    ax.set_yscale("log")
    ax.legend(fontsize=6.3, frameon=False)
    _ax(ax, "power-iteration step", "1 - |cosine| with top eigenvector")
    _pl(ax, "a")
    ax = axes[0, 1]
    rng = np.random.default_rng(1)
    zt = rng.standard_normal(10) * 2.0
    zs = rng.standard_normal(10) * 2.0
    taus = np.array([0.5, 1, 2, 4, 8, 16, 32])
    ex, ap = [], []
    for tau in taus:
        g = (softmax(zs / tau) - softmax(zt / tau)) / tau
        g2 = ((zs - zs.mean()) - (zt - zt.mean())) / (len(zs) * tau ** 2)
        ex.append(np.linalg.norm(g))
        ap.append(np.linalg.norm(g - g2) / np.linalg.norm(g))
    ax.plot(taus, ap, color=BLUE, marker="o", ms=3.5)
    ax.set_xscale("log", base=2)
    ax.set_yscale("log")
    _ax(ax, "temperature $\\tau$", "relative error")
    _pl(ax, "b")
    savefig(fig, CH, "fig19-S6", out)


# ---- active learning data
def al_pool(noise=0.25, n_pool=300, n_test=1000, seed=7):
    X, y = moons(n_pool + n_test, noise, seed)
    return X[:n_pool], y[:n_pool], X[n_pool:], y[n_pool:]


def al_patch_pool(seed=7):
    X, y = moons(200, 0.12, seed)
    r = np.random.default_rng(seed + 100)
    Xp = np.array((1.0, 0.25)) + 0.25 * r.standard_normal((100, 2))
    yp = r.integers(0, 2, 100)
    flag = np.r_[np.zeros(200, int), np.ones(100, int)]
    Xt, yt = moons(1000, 0.12, 99)
    return np.vstack([X, Xp]), np.r_[y, yp], flag, Xt, yt


def al_curves(kinds, n_queries=30, reps=20, patch=False):
    out = {}
    if patch:
        Xa, ya, flag, Xt, yt = al_patch_pool()
    else:
        Xa, ya, Xt, yt = al_pool()
        flag = np.zeros(len(ya), int)
    Pp, Pt = rbf_phi(Xa), rbf_phi(Xt)
    for kind in kinds:
        accs, lls, npatch = [], [], []
        for s in range(reps):
            r = np.random.default_rng(s)
            init = [int(r.choice(np.where((ya == c) & (flag == 0))[0])) for c in (0, 1)]
            lab, a, l = active_run(Pp, ya, Pt, yt, init, n_queries, kind, 1.0, r)
            accs.append(a)
            lls.append(l)
            npatch.append(flag[lab[2:]].sum())
        out[kind] = (np.mean(accs, 0), np.mean(lls, 0), float(np.mean(npatch)), np.std(accs, 0) / np.sqrt(reps))
    return out


def fig_19_S7(out=None):
    """Pool-based active learning: random, maximum entropy and BALD on a Laplace logistic model."""
    kinds = ("random", "entropy", "bald")
    cols = {"random": GREY, "entropy": ORANGE, "bald": BLUE}
    A = al_curves(kinds)
    B = al_curves(kinds, patch=True)
    fig, axes = panel_grid(1, 3, size=(3.1, 2.8))
    for ax, R, ttl, lab in ((axes[0, 0], A, "two moons", "a"), (axes[0, 1], B, "moons + ambiguous patch", "b")):
        for k in kinds:
            ax.plot(range(len(R[k][0])), R[k][0], color=cols[k], label="%s%s" % (k if k != "bald" else "BALD", "" if ttl == "two moons" else " (%.1f in patch)" % R[k][2]))
            ax.fill_between(range(len(R[k][0])), R[k][0] - R[k][3], R[k][0] + R[k][3], color=cols[k], alpha=0.18, lw=0)
        ax.legend(fontsize=7, frameon=False, loc="lower right")
        ax.set_title(ttl, fontsize=8.5)
        _ax(ax, "queries", "test accuracy")
        _pl(ax, lab)
    ax = axes[0, 2]
    m = np.linspace(-4, 4, 17)
    for s2, c in ((0.25, BLUE), (4.0, GREEN), (25.0, RED)):
        ax.plot(m, bald_mc(m, np.full_like(m, s2), 60000), color=c, lw=1, label="Monte Carlo, $s^2=%g$" % s2)
        ax.plot(m, bald_closed_form(m, np.full_like(m, s2)), color=c, ls="--", lw=1.6)
    ax.legend(fontsize=6.5, frameon=False, loc="upper right")
    _ax(ax, "mean $m$ of the logit", "BALD score (nats)")
    _pl(ax, "c")
    savefig(fig, CH, "fig19-S7", out)
    return A, B


# ---- MAML
def _maml_cache(iters=6000):
    path = os.path.join(CACHE_DIR, "ch19_maml.npz")
    if os.path.exists(path):
        z = np.load(path)
        return [z["m%d" % i] for i in range(6)], [z["p%d" % i] for i in range(6)]
    P_m, _ = maml_train(0, iters=iters, T=16, K=10)
    P_p, _ = maml_train(0, iters=iters, T=16, K=10, pretrain=True)
    os.makedirs(CACHE_DIR, exist_ok=True)
    np.savez(path, **{"m%d" % i: p for i, p in enumerate(P_m)}, **{"p%d" % i: p for i, p in enumerate(P_p)})
    return P_m, P_p


def fig_19_S8(out=None):
    """Few-shot sinusoid regression: random, pre-trained and MAML initialisations."""
    P_m, P_p = _maml_cache()
    P_r = mlp_init(0)
    inits = [("random initialisation", P_r, GREY), ("pre-trained on all tasks", P_p, ORANGE), ("MAML", P_m, BLUE)]
    rng = np.random.default_rng(55)
    A, b, xs, ys, _, _ = sin_task_batch(rng, 1, 10)
    A, b, xs, ys = A[0], b[0], xs[0], ys[0]
    xg = np.linspace(-5, 5, 300)
    fig, axes = panel_grid(1, 4, size=(2.55, 2.5))
    for ax, (name, P, c), lab in zip(axes[0, :3], inits, "abc"):
        ax.plot(xg, A * np.sin(xg - b), color="k", lw=1.2, label="truth")
        ax.plot(xs, ys, "k^", ms=5)
        for steps, ls in ((0, ":"), (1, "--"), (10, "-")):
            Q = adapt(P, xs, ys, steps, 0.01)
            ax.plot(xg, mlp_forward(Q, xg), color=c, ls=ls, lw=1.4, label="%d step%s" % (steps, "" if steps == 1 else "s"))
        ax.set_ylim(-5.5, 5.5)
        ax.set_title(name, fontsize=8.2)
        if lab == "a":
            ax.legend(fontsize=6.5, frameon=False, loc="lower left")
        _ax(ax, "$x$", "")
        _pl(ax, lab)
    ax = axes[0, 3]
    ks = tuple(range(0, 11))
    for name, P, c in inits:
        e = sin_eval(P, steps=ks)
        ax.plot(ks, [e[k] for k in ks], color=c, marker="o", ms=3, label=name)
    ax.set_yscale("log")
    ax.legend(fontsize=6.3, frameon=False)
    _ax(ax, "gradient steps on 10 shots", "mean test MSE")
    _pl(ax, "d")
    savefig(fig, CH, "fig19-S8", out)


def gd_prior_precision(h, eta, K):
    """Prior precision Lambda such that MAP with prior N(xi, 1/Lambda) along an eigen-direction of Hessian eigenvalue h
    equals K steps of gradient descent with step eta started at xi: Lambda = h / ((1 - eta h)^{-K} - 1)."""
    return h / ((1.0 - eta * h) ** (-K) - 1.0)


def fig_19_S9(out=None):
    """K steps of gradient descent from xi equal a Gaussian prior centred at xi."""
    h = np.linspace(0.02, 0.95, 200)
    eta = 1.0
    fig, axes = panel_grid(1, 2, size=(3.5, 2.9))
    ax = axes[0, 0]
    for K, c in zip((1, 3, 10, 30), (RED, ORANGE, GREEN, BLUE)):
        shrink = (1 - eta * h) ** K
        Lam = gd_prior_precision(h, eta, K)
        ax.plot(h, shrink, color=c, lw=1.8, label="$K=%d$" % K)
        ax.plot(h[::12], (Lam / (h + Lam))[::12], "k.", ms=4)
    ax.plot([], [], "k.", label="MAP, prior $\\Lambda_K$")
    ax.legend(fontsize=7, frameon=False)
    _ax(ax, "Hessian eigenvalue $h$ of the task loss", "fraction of $\\xi$ kept in the estimate")
    _pl(ax, "a")
    ax = axes[0, 1]
    Ks = np.arange(1, 16)
    for hh, c in zip((0.05, 0.1, 0.2, 0.4), (BLUE, GREEN, ORANGE, RED)):
        ax.plot(Ks, gd_prior_precision(hh, eta, Ks), color=c, lw=1.8, label="$h=%g$" % hh)
    ax.set_yscale("log")
    ax.legend(fontsize=7, frameon=False)
    _ax(ax, "number of gradient steps $K$", "prior precision $\\Lambda_K(h)$")
    _pl(ax, "b")
    savefig(fig, CH, "fig19-S9", out)


def mil_bags(n_bags, B, seed, q=0.3, sep=2.5):
    """Bags of B instances in the plane: negative instances N(0, I), positive instances N((sep, sep)/sqrt2 ...)."""
    r = np.random.default_rng(seed)
    mu = np.array([sep, sep]) / np.sqrt(2.0)
    bag_y = r.integers(0, 2, n_bags)
    inst_y = np.zeros((n_bags, B), int)
    for i in range(n_bags):
        if bag_y[i]:
            inst_y[i] = (r.uniform(size=B) < q).astype(int)
            if inst_y[i].sum() == 0:
                inst_y[i, r.integers(B)] = 1
    X = r.standard_normal((n_bags, B, 2)) + inst_y[:, :, None] * mu
    return X, bag_y, inst_y


def noisy_or_fit(X, bag_y, iters=3000, eta=0.5, lam=1e-2):
    """Logistic instance model with the noisy-OR bag likelihood; gradient descent on the mean negative log likelihood."""
    n, B, d = X.shape
    Phi = np.concatenate([np.ones((n, B, 1)), X], 2)
    w = np.zeros(d + 1)
    for _ in range(iters):
        p = sigmoid(Phi @ w)                      # (n, B)
        Q = np.prod(1 - p, 1)                     # P(bag negative)
        # d/dw of -log P(bag label)
        g = np.zeros_like(w)
        neg = bag_y == 0
        g += np.einsum("nb,nbd->d", p[neg], Phi[neg])             # -d/dw sum log(1 - p)
        pos = ~neg
        # d/dw (-log(1 - Q)) = -(Q / (1 - Q)) sum_b p_b Phi_b  (since dQ/dw = -Q sum_b p_b Phi_b)
        coef = Q[pos] / np.maximum(1 - Q[pos], 1e-12)
        g -= np.einsum("n,nb,nbd->d", coef, p[pos], Phi[pos])
        w -= eta * (g / n + lam * np.r_[0, w[1:]] / n)
    return w


def fig_19_S10(out=None):
    """Multi-instance learning: instance classifier from bag labels, noisy-OR against labelling every instance by its bag."""
    X, by, iy = mil_bags(150, 5, 0)
    Xt, byt, iyt = mil_bags(300, 5, 1)
    Xf, yf = X.reshape(-1, 2), np.repeat(by, 5)
    naive = fit_logreg(feat_linear(Xf), yf.astype(float), 1.0)
    wn = noisy_or_fit(X, by)
    Pt = feat_linear(Xt.reshape(-1, 2))
    acc_naive = np.mean((predict_p(Pt, naive) > 0.5) == iyt.reshape(-1))
    acc_nor = np.mean((predict_p(Pt, wn) > 0.5) == iyt.reshape(-1))
    fig, axes = panel_grid(1, 2, size=(3.4, 3.1))
    gx = np.linspace(-3.5, 5, 100)
    for ax, w, ttl, lab in ((axes[0, 0], naive, "bag label for every instance: %.3f" % acc_naive, "a"), (axes[0, 1], wn, "noisy-OR likelihood: %.3f" % acc_nor, "b")):
        ax.scatter(Xt.reshape(-1, 2)[:, 0], Xt.reshape(-1, 2)[:, 1], c=np.where(iyt.reshape(-1) == 1, RED, BLUE), s=5, lw=0, alpha=0.6)
        ax.plot(gx, -(w[0] + w[1] * gx) / w[2], "k-", lw=1.5)
        ax.set_xlim(-3.5, 5)
        ax.set_ylim(-3.5, 5)
        ax.set_title(ttl, fontsize=8, loc="center", pad=14)
        _ax(ax, equal=True)
        _pl(ax, lab)
    savefig(fig, CH, "fig19-S10", out)
    return acc_naive, acc_nor


# =========================================================================== data exported to the browser demonstrations
def r4(a, d=4):
    return np.round(np.asarray(a, dtype=float), d).tolist()


SSL_PRESET_CONFIGS = [
    dict(data="moons", method="supervised", nl=2, seed=1),
    dict(data="moons", method="self-training", nl=2, seed=1, tau=0.8),
    dict(data="moons", method="entropy", nl=2, seed=1, beta=0.3),
    dict(data="moons", method="propagation", nl=2, seed=1, sigma=0.25),
    dict(data="moons", method="propagation", nl=2, seed=1, sigma=0.6),
    dict(data="blobs", method="supervised", nl=1, seed=4),
    dict(data="blobs", method="self-training", nl=1, seed=4, tau=0.8),
    dict(data="blobs", method="entropy", nl=1, seed=4, beta=1.0),
]


def export_ssl():
    out = {"datasets": {}, "ref": []}
    for name, cfg in SSL_CFG.items():
        Xu, yu, Xt, yt = cfg["data"]()
        orders = {}
        for s in range(1, 6):
            r = np.random.default_rng(s)
            orders[str(s)] = [r.permutation(np.where(yu == c)[0]).tolist() for c in (0, 1)]
        out["datasets"][name] = dict(Xu=r4(Xu), yu=yu.tolist(), Xt=r4(Xt), yt=yt.tolist(), orders=orders,
                                     lam=cfg["lam"], sigma=cfg["sigma"], tau=cfg["tau"], beta=cfg["beta"])
    out["rbf"] = dict(cx=r4(RBF_CX), cy=r4(RBF_CY), h=RBF_H)
    for c in SSL_PRESET_CONFIGS:
        kw = {k: v for k, v in c.items() if k not in ("data", "method", "nl", "seed")}
        te, un, _ = run_ssl(c["data"], c["method"], c["nl"], c["seed"], **kw)
        out["ref"].append(dict(c, test_acc=round(te, 6), unl_acc=round(un, 6)))
    for name in SSL_CFG:                       # default parameters, seeds 1 and 2, several label counts
        for method in ("supervised", "self-training", "entropy", "propagation"):
            for nl in (1, 2, 5):
                for seed in (1, 2):
                    te, un, _ = run_ssl(name, method, nl, seed)
                    out["ref"].append(dict(data=name, method=method, nl=nl, seed=seed, test_acc=round(te, 6), unl_acc=round(un, 6)))
    # book-literal label propagation on the moons: accuracy on the unlabeled points after t sweeps
    Xu, yu, Xt, yt = ssl_moons()
    li = pick_labeled(yu, 2, 1)
    ui = np.setdiff1d(np.arange(300), li)
    Y, hist = label_prop_book(rbf_weights(Xu, 0.25), li, yu[li], 2, 300, record=True)
    out["lp_book_acc"] = {str(t): round(float(np.mean((hist[t - 1][ui, 1] > 0.5) == yu[ui])), 6) for t in (1, 2, 5, 10, 20, 50, 100, 300)}
    with open(os.path.join(DATA_OUT, "ch19-ssl.json"), "w") as f:
        json.dump(out, f, separators=(",", ":"))


def export_active():
    out = {"datasets": {}, "ref": []}
    for name in ("moons", "patch"):
        if name == "moons":
            Xa, ya, Xt, yt = al_pool()
            flag = np.zeros(len(ya), int)
        else:
            Xa, ya, flag, Xt, yt = al_patch_pool()
        Xa, Xt = np.round(Xa, 4), np.round(Xt, 4)     # the browser reads the rounded coordinates: run on exactly those
        Pp, Pt = rbf_phi(Xa), rbf_phi(Xt)
        inits, perms = {}, {}
        for s in range(1, 6):
            r = np.random.default_rng(s)
            inits[str(s)] = [int(r.choice(np.where((ya == c) & (flag == 0))[0])) for c in (0, 1)]
            perms[str(s)] = np.random.default_rng(100 + s).permutation(len(ya)).tolist()
        out["datasets"][name] = dict(X=r4(Xa, 4), y=ya.tolist(), flag=flag.tolist(), Xt=r4(Xt, 4), yt=yt.tolist(), inits=inits, perms=perms)
        for kind in ("random", "entropy", "bald"):
            for s in (1, 2, 3):
                lab, a, l = active_run(Pp, ya, Pt, yt, inits[str(s)], 30, kind, 1.0, None, perms[str(s)])
                out["ref"].append(dict(data=name, kind=kind, seed=s, acc=r4(a, 6), ll=r4(l, 6), queried=lab[2:12]))
    out["alpha"] = 1.0
    with open(os.path.join(DATA_OUT, "ch19-active.json"), "w") as f:
        json.dump(out, f, separators=(",", ":"))


def export_maml():
    P_m, P_p = _maml_cache()
    P_r = mlp_init(0)
    flat = lambda P: [r4(p.ravel(), 5) for p in P]
    rng = np.random.default_rng(2024)
    xs_pool = rng.uniform(-5, 5, 10)
    xg = np.linspace(-5, 5, 201)
    out = {"H": SIN_H, "inits": {"random": flat(P_r), "pretrained": flat(P_p), "maml": flat(P_m)}, "xs": r4(xs_pool, 5), "lr": 0.01, "ref": []}
    for (A, b, K, steps) in ((5.0, 0.0, 10, 1), (5.0, 0.0, 10, 10), (2.0, 1.5, 5, 1), (2.0, 1.5, 5, 10), (0.5, 0.8, 3, 5), (3.5, 2.5, 10, 0)):
        row = dict(A=A, b=b, K=K, steps=steps)
        ys = A * np.sin(xs_pool[:K] - b)
        yg = A * np.sin(xg - b)
        for name, P in (("random", P_r), ("pretrained", P_p), ("maml", P_m)):
            Q = adapt(P, xs_pool[:K], ys, steps, 0.01)
            row[name] = round(float(np.mean((mlp_forward(Q, xg) - yg) ** 2)), 6)
        out["ref"].append(row)
    e = {n: sin_eval(P, steps=(0, 1, 5, 10)) for n, P in (("random", P_r), ("pretrained", P_p), ("maml", P_m))}
    out["mean_mse"] = {n: {str(k): round(v, 6) for k, v in d.items()} for n, d in e.items()}
    with open(os.path.join(DATA_OUT, "ch19-maml.json"), "w") as f:
        json.dump(out, f, separators=(",", ":"))


def export_json():
    os.makedirs(DATA_OUT, exist_ok=True)
    export_ssl()
    export_active()
    export_maml()
    print("wrote ch19-*.json")


FIGURES = {"19.1": fig_19_1, "19.8": fig_19_8, "19.9": fig_19_9, "19.10": fig_19_10, "19.11": fig_19_11,
           "19.S1": fig_19_S1, "19.S2": fig_19_S2, "19.S3": fig_19_S3, "19.S4": fig_19_S4, "19.S5": fig_19_S5,
           "19.S6": fig_19_S6, "19.S7": fig_19_S7, "19.S8": fig_19_S8, "19.S9": fig_19_S9, "19.S10": fig_19_S10}


if __name__ == "__main__":
    if "--json" in sys.argv:
        export_json()
    else:
        common.run(FIGURES, CH)
        if "--fig" not in sys.argv and "--list" not in sys.argv:
            export_json()
