File size: 4,464 Bytes
18a8899
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
857044b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18a8899
 
 
857044b
 
 
 
 
18a8899
857044b
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
"""Claim 2, internal-consistency check: how large must eta*T actually be?

Theorem 1/2 prescribe *simultaneously*
    eta*T = Theta(d^2)   and   m = Omega~(d^8 K^4).
Those two are only compatible if the eta*T needed for the network to actually
fit a task does not grow with m.  In this model gradient descent moves the
first layer multiplicatively, w_i <- (I + (eta a_i/sqrt(m)) A) w_i, so the
*effective* horizon is eta*T/sqrt(m): naively one expects the eta*T needed for
interpolation to grow like sqrt(m), which would contradict eta*T = Theta(d^2)
once m is pushed to d^8 K^4.

This script measures it directly.  For a *single* task we find the smallest
eta*T (scanning eta at fixed T) that drives the hinge training loss below a
threshold, as a function of d and m, and fits eta*T_needed ~ d^alpha m^beta.
Theorem 1's regime is self-consistent only if beta ~ 0 and alpha ~ 2.
"""

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

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

THRESH = 0.05          # "interpolating": hinge train loss below this
T_FIXED = 50

# Cost control.  Each loss evaluation here is a full T_FIXED-step GD run whose
# cost is ~4*n*d*m*T flops, so a naive 40-point linear scan over eta at the top
# of the (d, m) grid is several TFLOP *per probe*.  Three changes keep this
# tractable on a 12-core / 15 GB CPU box without changing what is measured:
#   * n = d^2 instead of 2 d^2 (still Theta(d^2), the regime the theorem asks
#     for with K = 1) and T_FIXED = 50 instead of 200 -- the quantity reported
#     is the product eta*T, and eta is what we scan, so shortening T just moves
#     the same threshold to a larger eta;
#   * a coarse geometric bracket followed by bisection in log-eta, ~7-11
#     evaluations instead of up to 40;
#   * d <= 48 and m <= 16000, which still spans 1.8 decades in m -- enough to
#     resolve the beta in etaT_needed ~ d^alpha m^beta, which is the only thing
#     this script is asked to decide.
N_COARSE = 7
N_BISECT = 4
ETA_LO, ETA_HI = 0.05, 4000.0


def _train_loss(d, m, n, eta, seed):
    r = C.continual_run(d=d, m=m, K=1, n=n, T=T_FIXED, eta=float(eta),
                        sigma_c=0.1, loss_name="hinge", seed=seed, n_test=200)
    L = float(r["loss_at"][0, 0])
    return L if np.isfinite(L) else np.inf


def probe(job):
    """Smallest eta*T (scanning eta at fixed T) that gets hinge loss < THRESH.

    Coarse geometric bracket, then bisection in log(eta).  Returns None if even
    the largest eta in the grid fails to interpolate.
    """
    d, m, seed = job
    n = int(d * d)
    rec = []

    lo_eta, hi_eta = None, None          # lo: known-failing, hi: known-passing
    for eta in np.geomspace(ETA_LO, ETA_HI, N_COARSE):
        L = _train_loss(d, m, n, eta, seed)
        rec.append((float(eta * T_FIXED), L if np.isfinite(L) else None))
        if L < THRESH:
            hi_eta = float(eta)
            break
        lo_eta = float(eta)

    if hi_eta is None:
        return dict(sweep="etaT_needed", d=d, m=m, n=n, T=T_FIXED, seed=seed,
                    etaT_needed=None, curve=rec)

    if lo_eta is not None:               # refine the bracket in log space
        for _ in range(N_BISECT):
            mid = float(np.sqrt(lo_eta * hi_eta))
            L = _train_loss(d, m, n, mid, seed)
            rec.append((float(mid * T_FIXED), L if np.isfinite(L) else None))
            if L < THRESH:
                hi_eta = mid
            else:
                lo_eta = mid

    return dict(sweep="etaT_needed", d=d, m=m, n=n, T=T_FIXED, seed=seed,
                etaT_needed=float(hi_eta * T_FIXED), curve=rec)


if __name__ == "__main__":
    jobs = [(d, m, s)
            for d in [16, 24, 32, 48]
            for m in [250, 1000, 4000, 16000]
            for s in range(2)]
    print(len(jobs), "probes", flush=True)
    t0 = time.time()
    with Pool(C.NPROC) as p:
        recs = []
        for i, r in enumerate(p.imap_unordered(probe, jobs)):
            recs.append(r)
            print(f"  {i+1}/{len(jobs)}  d={r['d']} m={r['m']} "
                  f"etaT={r['etaT_needed']}  {time.time()-t0:.0f}s", flush=True)
    with open(os.path.join(OUT, "exp5_etaT.json"), "w") as f:
        json.dump(recs, f)
    print("done", f"{time.time()-t0:.0f}s")