| """Claims 4 and 5: the delayed generalization gap (Theorems 3 and 4). |
| |
| Theorem 3 (1-Lipschitz, 1-smooth loss): |
| F^gen_{k,K} = E_{D_k}[ F_k(w_K) - Fhat_k(w_K) ] <~ eta*T*exp(eta*T*(K-k+1)/sqrt(m)) / n |
| |
| Theorem 4 (additionally self-bounded loss, e.g. logistic): |
| F^gen_{k,K} <~ (eta/n) * E[ exp((eta/sqrt(m)) c_{k,K}) * sum_{t<T} Fhat_k(w_k^{(t)}) ] |
| with c_{k,K} = O( sum_{j>k} sum_{t<T} Fhat_j(w_j^{(t)}) ). |
| |
| Both are *expectations over the draw of D_k*, so each measurement point |
| averages over many independent dataset draws; F_k is estimated on a large |
| fresh test set. We use the logistic loss (1-Lipschitz, 1/4-smooth, |
| self-bounded) so that both theorems apply to the same runs and can be compared |
| head to head. |
| |
| The headline comparison for Claim 5 is the T-sweep: Theorem 3's right-hand |
| side grows *linearly* in T, Theorem 4's grows like the cumulative training |
| loss sum_t Fhat_k(w_k^{(t)}), which for a learnable task is poly-logarithmic |
| in T. We fit one global constant per bound and check (i) both remain valid |
| upper bounds and (ii) Theorem 4's is far tighter and grows sub-linearly. |
| """ |
|
|
| import json |
| import os |
| import sys |
| import time |
| from multiprocessing import Pool |
|
|
| import numpy as np |
|
|
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) |
| import clcore as C |
|
|
| OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "results") |
| os.makedirs(OUT, exist_ok=True) |
|
|
| BASE = dict(d=50, m=1000, K=3, n=400, T=200, eta=100.0, sigma_c=0.1, |
| loss_name="logistic", n_test=4000) |
| |
| |
| |
| |
| |
| |
| |
| SEEDS = list(range(10)) |
|
|
|
|
| def one(job): |
| sweep, override, seed = job |
| cfg = dict(BASE) |
| cfg.update(override) |
| cfg["seed"] = seed |
| r = C.continual_run(**cfg) |
| K, T, eta, n, m = cfg["K"], cfg["T"], cfg["eta"], cfg["n"], cfg["m"] |
| k = 0 |
| rec = dict(sweep=sweep, |
| **{kk: vv for kk, vv in cfg.items() if kk != "n_test"}) |
| rec["gen_gap"] = C.gen_gap(r, k) |
| rec["gen_gap_per_k"] = [C.gen_gap(r, kk) for kk in range(K)] |
| rec["train_forget"] = C.train_forgetting(r, k) |
| rec["test_forget"] = C.test_forgetting(r, k) |
| rec["cum_train_loss"] = list(r["cum_train_loss"]) |
| rec["train_loss_end"] = float(r["loss_at"][K - 1, k]) |
| rec["test_loss_end"] = float(r["test_loss_at"][K - 1, k]) |
| rec["err_at"] = r["err_at"].tolist() |
| rec["test_err_at"] = r["test_err_at"].tolist() |
| rec["dist"] = list(r["dist"]) |
| |
| |
| |
| ck = float(sum(r["cum_train_loss"][k + 1:])) |
| rec["c_kK"] = ck |
| rec["exponent_thm3"] = float(eta * T * (K - k) / np.sqrt(m)) |
| rec["exponent_thm4"] = float(eta * ck / np.sqrt(m)) |
| rec["rhs_thm3_core"] = float(eta * T / n) |
| rec["rhs_thm4_core"] = float((eta / n) * r["cum_train_loss"][k]) |
| rec["rhs_thm3"] = float(rec["rhs_thm3_core"] |
| * np.exp(min(rec["exponent_thm3"], 700.0))) |
| rec["rhs_thm4"] = float(rec["rhs_thm4_core"] |
| * np.exp(min(rec["exponent_thm4"], 700.0))) |
| return rec |
|
|
|
|
| def build(): |
| jobs = [] |
| |
| for n in [50, 100, 200, 400, 800, 1600, 3200]: |
| for s in SEEDS: |
| jobs.append(("n", dict(n=n), s)) |
| |
| for T in [25, 50, 100, 200, 400, 800, 1600]: |
| for s in SEEDS: |
| jobs.append(("T", dict(T=T), s)) |
| |
| for m in [100, 300, 1000, 3000, 10000]: |
| for s in SEEDS: |
| jobs.append(("m", dict(m=m), s)) |
| |
| for K in [2, 3, 4, 6]: |
| for s in SEEDS: |
| jobs.append(("K", dict(K=K), s)) |
| return jobs |
|
|
|
|
| if __name__ == "__main__": |
| jobs = build() |
| print(len(jobs), "runs", flush=True) |
| t0 = time.time() |
| with Pool(C.NPROC) as p: |
| recs = [] |
| for i, r in enumerate(p.imap_unordered(one, jobs)): |
| recs.append(r) |
| if (i + 1) % 50 == 0: |
| print(f" {i+1}/{len(jobs)} {time.time()-t0:.0f}s", flush=True) |
| with open(os.path.join(OUT, "exp4_gengap.json"), "w") as f: |
| json.dump(recs, f) |
| print("wrote exp4_gengap.json", f"{time.time()-t0:.0f}s") |
|
|