summaryrefslogtreecommitdiff
path: root/scripts/generate_paper_tables.py
blob: 95a67c8d00abd9f08fafc486d37a9d5a204d503b (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python3
"""Generate LaTeX table fragments for the paper appendices from row-level CSVs.

Each fragment contains only a tabular environment; captions and labels live in
the paper source. Uncertainty follows the paper's nesting convention: feedback
draws are averaged within each forward initialization first, and standard
errors are computed across forward-initialization means.
"""

from __future__ import annotations

import argparse
import csv
import math
import statistics
from collections import defaultdict
from pathlib import Path


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--depth-dir", type=Path, default=Path("outputs/aaai_depth_experiments"))
    parser.add_argument(
        "--cnn-dir", type=Path, default=Path("outputs/cnn_initialization_validation")
    )
    parser.add_argument(
        "--lr-dir", type=Path, default=Path("outputs/learning_rate_compensation")
    )
    parser.add_argument(
        "--teacher-dir", type=Path, default=Path("outputs/teacher_test_gap_sgd_T8000")
    )
    parser.add_argument("--outdir", type=Path, default=Path("outputs/tables"))
    return parser.parse_args()


def read_rows(path: Path) -> list[dict[str, str]]:
    if not path.exists():
        raise FileNotFoundError(path)
    with path.open() as handle:
        return list(csv.DictReader(handle))


def sem(values: list[float]) -> float:
    if len(values) < 2:
        return float("nan")
    return statistics.stdev(values) / math.sqrt(len(values))


def write_fragment(path: Path, lines: list[str]) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text("\n".join(lines) + "\n")
    print(f"wrote {path}")


def initialization_table(depth_dir: Path, outdir: Path) -> None:
    rows = read_rows(depth_dir / "initialization_rows.csv")
    cells: dict[tuple[int, int], dict[str, dict[str, float]]] = defaultdict(dict)
    for row in rows:
        key = (int(row["depth"]), int(row["init_seed"]))
        cells[key][row["rule"]] = {
            "pred": float(row["predicted_deficit"]),
            "mean": float(row["empirical_deficit"]),
            "se": float(row["empirical_stderr"]),
        }
    lines = [
        r"\begin{tabular}{@{}rrccc@{}}",
        r"\toprule",
        r"Depth & Init & Prediction & FA measured (SE) & DFA measured (SE) \\",
        r"\midrule",
    ]
    for (depth, init_seed), by_rule in sorted(cells.items()):
        fa, dfa = by_rule["FA"], by_rule["DFA"]
        assert abs(fa["pred"] - dfa["pred"]) < 1e-12, (depth, init_seed)
        lines.append(
            f"{depth} & {init_seed - 10_000} & {fa['pred']:.6f} & "
            f"{fa['mean']:.6f} ({fa['se']:.2g}) & "
            f"{dfa['mean']:.6f} ({dfa['se']:.2g}) \\\\"
        )
    lines += [r"\bottomrule", r"\end{tabular}"]
    write_fragment(outdir / "initialization_rows.tex", lines)


def finite_time_table(depth_dir: Path, outdir: Path) -> None:
    rows = read_rows(depth_dir / "finite_time_rows.csv")
    cells: dict[tuple[int, int], dict[int, dict[str, list[float]]]] = defaultdict(
        lambda: defaultdict(lambda: defaultdict(list))
    )
    fields = ["empirical_gap", "fixed_gap", "velocity_gap", "retangent_gap"]
    for row in rows:
        cell = cells[(int(row["depth"]), int(row["width"]))][int(row["init_seed"])]
        for field in fields:
            cell[field].append(float(row[field]))
    lines = [
        r"\begin{tabular}{@{}rrccccc@{}}",
        r"\toprule",
        r"Depth & Width & Measured & SE & Frozen & Linear & Relinearized \\",
        r"\midrule",
    ]
    for (depth, width), by_init in sorted(cells.items()):
        init_means = {
            field: [statistics.mean(values[field]) for _, values in sorted(by_init.items())]
            for field in fields
        }
        measured = statistics.mean(init_means["empirical_gap"])
        lines.append(
            f"{depth} & {width} & {measured:.6f} & {sem(init_means['empirical_gap']):.2g} & "
            f"{statistics.mean(init_means['fixed_gap']):.6f} & "
            f"{statistics.mean(init_means['velocity_gap']):.6f} & "
            f"{statistics.mean(init_means['retangent_gap']):.6f} \\\\"
        )
    lines += [r"\bottomrule", r"\end{tabular}"]
    write_fragment(outdir / "finite_time_cells.tex", lines)


def cnn_table(cnn_dir: Path, outdir: Path) -> None:
    rows = read_rows(cnn_dir / "cnn_initialization_rows.csv")
    lines = [
        r"\begin{tabular}{@{}lrccc@{}}",
        r"\toprule",
        r"Rule & Init & Prediction & Measured & SE \\",
        r"\midrule",
    ]
    for row in sorted(rows, key=lambda r: (r["rule"], int(r["init_seed"]))):
        lines.append(
            f"{row['rule']} & {int(row['init_seed']) - 10_000} & "
            f"{float(row['predicted_deficit']):.6f} & "
            f"{float(row['empirical_deficit']):.6f} & "
            f"{float(row['empirical_stderr']):.2g} \\\\"
        )
    lines += [r"\bottomrule", r"\end{tabular}"]
    write_fragment(outdir / "cnn_initialization_rows.tex", lines)


def learning_rate_table(lr_dir: Path, outdir: Path) -> None:
    rows = read_rows(lr_dir / "evaluation_rows.csv")
    regime_order = {"same_lr": 0, "initial_compensation": 1, "independently_tuned": 2}
    regime_label = {
        "same_lr": "Same rate",
        "initial_compensation": "Initial compensation",
        "independently_tuned": "Independently selected",
    }
    cells: dict[tuple[int, str], dict[int, list[float]]] = defaultdict(lambda: defaultdict(list))
    fa_rates: dict[tuple[int, str], list[float]] = defaultdict(list)
    bp_rates: dict[tuple[int, str], set[float]] = defaultdict(set)
    for row in rows:
        assert row["stable"] == "True", row
        key = (int(row["depth"]), row["regime"])
        cells[key][int(row["init_seed"])].append(float(row["gap_to_bp"]))
        fa_rates[key].append(float(row["fa_lr"]))
        bp_rates[key].add(float(row["bp_lr"]))
    lines = [
        r"\begin{tabular}{@{}rlcccc@{}}",
        r"\toprule",
        r"Depth & Regime & BP rate & Mean FA rate & Gap & SE \\",
        r"\midrule",
    ]
    for (depth, regime), by_init in sorted(
        cells.items(), key=lambda item: (item[0][0], regime_order[item[0][1]])
    ):
        init_means = [statistics.mean(values) for _, values in sorted(by_init.items())]
        (bp_rate,) = bp_rates[(depth, regime)]
        lines.append(
            f"{depth} & {regime_label[regime]} & {bp_rate:g} & "
            f"{statistics.mean(fa_rates[(depth, regime)]):.4g} & "
            f"{statistics.mean(init_means):.5f} & {sem(init_means):.2g} \\\\"
        )
    lines += [r"\bottomrule", r"\end{tabular}"]
    write_fragment(outdir / "learning_rate_regimes.tex", lines)


def teacher_table(teacher_dir: Path, outdir: Path) -> None:
    rows = [r for r in read_rows(teacher_dir / "runs.csv") if r["run_type"] == "fa"]
    by: dict[int, dict[int, dict[str, list[float]]]] = defaultdict(
        lambda: defaultdict(lambda: defaultdict(list))
    )
    for row in rows:
        cell = by[int(row["width"])][int(row["init_seed"])]
        cell["train"].append(float(row["train_gap_to_bp"]))
        cell["test"].append(float(row["test_gap_to_bp"]))
    lines = [
        r"\begin{tabular}{@{}rcc@{}}",
        r"\toprule",
        r"Width & Train difference & Test difference \\",
        r"\midrule",
    ]
    for width, by_init in sorted(by.items()):
        train_means = [statistics.mean(v["train"]) for _, v in sorted(by_init.items())]
        test_means = [statistics.mean(v["test"]) for _, v in sorted(by_init.items())]
        lines.append(
            f"{width} & \\({statistics.mean(train_means):.4f}\\pm{sem(train_means):.4f}\\) & "
            f"\\({statistics.mean(test_means):.4f}\\pm{sem(test_means):.4f}\\) \\\\"
        )
    lines += [r"\bottomrule", r"\end{tabular}"]
    write_fragment(outdir / "teacher_nested_se.tex", lines)


def main() -> None:
    args = parse_args()
    initialization_table(args.depth_dir, args.outdir)
    finite_time_table(args.depth_dir, args.outdir)
    cnn_table(args.cnn_dir, args.outdir)
    learning_rate_table(args.lr_dir, args.outdir)
    teacher_table(args.teacher_dir, args.outdir)


if __name__ == "__main__":
    main()