File size: 3,047 Bytes
1f48ccf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Quantitative audit of the Theorem 3.2 guarantee (Claim 2).

    lim_t |<theta(t), theta*>| >= 1 - C (e^{-M/2} + (d/n)^{1/5}),   n >= C M^4 d.

We run the full-batch spherical flow with the truncated activation on a grid of
(M, delta = n/d) at fixed d, and report the realised deficit 1 - |<theta_inf, theta*>|
against the theorem's rate  e^{-M/2} + (d/n)^{1/5}.  A single constant C should
dominate the whole grid inside the theorem's regime delta >= C M^4.
Also includes the M -> small control, where the guarantee degrades as predicted.
"""

from __future__ import annotations

import argparse
import csv
import math
import os
import sys
import time

import torch

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import sim


def main():
    p = argparse.ArgumentParser()
    p.add_argument("--d", type=int, default=512)
    p.add_argument("--Ms", default="1,2,4,8,16,32")
    p.add_argument("--deltas", default="8,16,32,64,128,256")
    p.add_argument("--seeds", type=int, default=8)
    p.add_argument("--act", default="trunc", choices=["trunc", "smooth", "quad"])
    p.add_argument("--eta", type=float, default=0.1)
    p.add_argument("--T", type=int, default=3000)
    p.add_argument("--out", required=True)
    args = p.parse_args()

    dev = "cuda" if torch.cuda.is_available() else "cpu"
    Ms = [float(v) for v in args.Ms.split(",")]
    deltas = [float(v) for v in args.deltas.split(",")]
    d = args.d
    rows = []
    t0 = time.time()
    for M in Ms:
        for delta in deltas:
            n = int(round(delta * d))
            for s in range(args.seeds):
                seed = 5000 + 31 * s + int(delta) + int(100 * M)
                data = sim.make_data(d, n, seed, args.act, M, dev, torch.float32)
                th0 = sim.rand_sphere(d, 600_000 + seed, dev, torch.float32)
                th, steps, _ = sim.spherical_flow(data, th0, args.act, M, eta=args.eta,
                                                  T=args.T, tol=0.0, check_every=10 ** 9)
                ov = abs(float(th @ data.theta_star))
                rate = math.exp(-M / 2) + (d / n) ** 0.2
                rows.append(dict(act=args.act, d=d, M=M, delta=delta, n=n, seed=seed,
                                 abs_overlap=round(ov, 6), deficit=round(1 - ov, 6),
                                 rate=round(rate, 6),
                                 C_implied=round((1 - ov) / rate, 6),
                                 in_regime=int(delta >= M ** 4 / 100)))
                del data
            torch.cuda.empty_cache() if dev == "cuda" else None
            sub = [r["deficit"] for r in rows if r["M"] == M and r["delta"] == delta]
            print(f"[{time.time()-t0:6.1f}s] M={M:5.1f} delta={delta:6.1f} "
                  f"mean deficit={sum(sub)/len(sub):.4f}", flush=True)

    with open(args.out, "w", newline="") as f:
        w = csv.DictWriter(f, fieldnames=list(rows[0].keys()))
        w.writeheader()
        w.writerows(rows)
    print("wrote", args.out, len(rows), "rows")


if __name__ == "__main__":
    main()