File size: 5,423 Bytes
18a8899
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
857044b
18a8899
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
857044b
18a8899
 
 
 
 
 
 
 
 
857044b
18a8899
 
 
857044b
 
 
 
18a8899
857044b
18a8899
857044b
 
18a8899
 
 
 
 
 
1
2
3
4
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""Claims 2, 3 and 6.

Claim 2 (Thm 1 parameter regime).  The theorem promises F^tr = o_d(1) under
    n = Theta~(d^2 K),  m = Omega~(d^8 K^4),  eta*T = Theta(d^2).
m = d^8 K^4 is numerically unreachable (d=32, K=3 -> 8.7e13 neurons), so we
test the *asymptotic statement* instead: hold the prescribed n and eta*T
scalings, push d up, and check the forgetting decreases towards 0.  We use the
exact linear-loss solver so that the width can be set large enough for the
third (width) term of Thm 1 to be numerically negligible, isolating the
d-dependence the theorem predicts.  Controls relax each condition in turn.

Claim 3 (Thm 2).  After KT GD iterations the misclassification *train* error
and train loss are o_d(1) uniformly over all K tasks.  Checked with the hinge
loss the theorem assumes.

Claim 6 (decomposition).  Test-time forgetting <= train-time forgetting +
delayed generalization gap, and the *joint* (not individual) control by n and
m: a 2-D grid where neither large n alone nor large m alone drives forgetting
down.
"""

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  # noqa: E402
from exp2_mechanism import exact_linear_run  # noqa: E402

OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "results")
os.makedirs(OUT, exist_ok=True)

DIMS = [12, 16, 24, 32, 48, 64]
SEEDS = list(range(5))
M_BIG = 20_000            # large enough that the 1/sqrt(m) term is negligible
K = 3
C_N = 1.0                # n = C_N * d^2 * K
C_T = 0.15               # eta*T = C_T * d^2   (eta = 2 fixed, T = C_T d^2 / eta)


def regime_job(job):
    tag, d, seed = job
    n = int(round(C_N * d * d * K))
    eta = 2.0
    T = max(5, int(round(C_T * d * d / eta)))
    m = M_BIG
    if tag == "prescribed":
        pass
    elif tag == "fixed_n":            # violate n = Theta~(d^2 K): n stays small
        n = int(round(C_N * 12 * 12 * K))
    elif tag == "long_train":         # violate eta*T = Theta(d^2): eta*T ~ d^3
        T = max(5, int(round(C_T * d ** 3 / (12 * eta))))
    elif tag == "small_m":            # violate the width condition
        m = 300
    r = exact_linear_run(d=d, m=m, K=K, n=n, T=T, eta=eta, sigma_c=0.1, seed=seed)
    r["sweep"] = "regime"
    r["variant"] = tag
    return r


def claim3_job(job):
    """Hinge-loss GD; record per-task train loss / misclassification error."""
    d, m, n, K3, T, eta, seed = job
    res = C.continual_run(d=d, m=m, K=K3, n=n, T=T, eta=eta, sigma_c=0.1,
                          loss_name="hinge", seed=seed, n_test=2000)
    return dict(
        sweep="claim3", d=d, m=m, n=n, K=K3, T=T, eta=eta, seed=seed,
        loss_at=res["loss_at"].tolist(),
        err_at=res["err_at"].tolist(),
        test_loss_at=res["test_loss_at"].tolist(),
        test_err_at=res["test_err_at"].tolist(),
        forget=[C.train_forgetting(res, k) for k in range(K3)],
        test_forget=[C.test_forgetting(res, k) for k in range(K3)],
        gen_gap=[C.gen_gap(res, k) for k in range(K3)],
        dist=list(res["dist"]),
    )


def claim6_job(job):
    """(n, m) grid: joint control of forgetting."""
    n, m, seed = job
    res = C.continual_run(d=50, m=m, K=3, n=n, T=200, eta=8.0, sigma_c=0.1,
                          loss_name="hinge", seed=seed, n_test=3000)
    return dict(
        sweep="claim6", n=n, m=m, seed=seed, d=50, K=3, T=200, eta=8.0,
        loss_at=res["loss_at"].tolist(),
        test_loss_at=res["test_loss_at"].tolist(),
        forget=[C.train_forgetting(res, k) for k in range(3)],
        test_forget=[C.test_forgetting(res, k) for k in range(3)],
        gen_gap=[C.gen_gap(res, k) for k in range(3)],
        err_at=res["err_at"].tolist(),
        test_err_at=res["test_err_at"].tolist(),
    )


if __name__ == "__main__":
    t0 = time.time()
    recs = []

    jobs = [(tag, d, s)
            for tag in ["prescribed", "fixed_n", "long_train", "small_m"]
            for d in DIMS for s in SEEDS]
    with Pool(C.NPROC) as p:
        recs += list(p.imap_unordered(regime_job, jobs))
    print("regime done", f"{time.time()-t0:.0f}s", flush=True)

    # Claim 3: hinge loss, K = 6 tasks.  eta*T = 1600 = 0.64 d^2 puts the
    # network in the interpolating regime the theorem assumes; eta*T = 400 is
    # the authors' own Fig.-1 horizon and is reported for comparison.
    j3 = [(50, m, n, 6, 200, eta, s)
          for eta in [8.0, 2.0]
          for m in [500, 2000] for n in [500, 2000] for s in range(4)]
    with Pool(C.NPROC) as p:
        recs += list(p.imap_unordered(claim3_job, j3))
    print("claim3 done", f"{time.time()-t0:.0f}s", flush=True)

    # Claim 6: (n, m) grid.  The n=8000 x m=5000 corner alone costs more than
    # the rest of the grid put together (cost ~ n*m per GD step), and the
    # claim being tested is qualitative -- that neither axis alone drives
    # forgetting down -- so the grid is capped at n=4000 and 3 seeds.
    j6 = [(n, m, s)
          for n in [125, 500, 2000, 4000]
          for m in [50, 200, 1000, 5000]
          for s in range(3)]
    with Pool(C.NPROC) as p:
        recs += list(p.imap_unordered(claim6_job, j6))
    print("claim6 done", f"{time.time()-t0:.0f}s", flush=True)

    with open(os.path.join(OUT, "exp3_regime.json"), "w") as f:
        json.dump(recs, f)
    print("wrote exp3_regime.json", f"{time.time()-t0:.0f}s")