Repository navigation
Expand file tree
/
Copy pathvisualization.html
More file actions
1568 lines (1510 loc) · 93.5 KB
/
Copy pathvisualization.html
File metadata and controls
1568 lines (1510 loc) · 93.5 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>ΔL per Step — Full-Batch GD vs Mini-Batch SGD · Noise Scale Verification</title>
<style>
:root{
--bg:#f8f9fb;
--panel:#ffffff;
--panel-2:#f8fafc;
--text:#0f172a;
--muted:#64748b;
--border:#e2e8f0;
--accent:#2563eb;
--gd:#2563eb;
--sgd:#ec4899;
--grid:#eef2f7;
--shadow:0 1px 2px rgba(15,23,42,.06), 0 12px 32px rgba(15,23,42,.07);
--radius:16px;
--ok:#059669;
--warn:#d97706;
}
*{box-sizing:border-box}
html,body{margin:0;padding:0;background:var(--bg);color:var(--text);font-family:Inter, ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial; -webkit-font-smoothing:antialiased}
.wrap{max-width:980px;margin:0 auto;padding:32px 20px 44px}
header{margin-bottom:18px}
.eyebrow{font-size:11px;letter-spacing:.14em;text-transform:uppercase;color:var(--muted);font-weight:700}
h1{margin:8px 0 8px;font-size:28px;line-height:1.15;letter-spacing:-.02em;font-weight:800}
h1 span{color:var(--accent)}
.sub{color:var(--muted);font-size:13.5px;line-height:1.65;max-width:760px;margin:0}
.formula-bar{
margin:16px 0 14px;
background:var(--panel);
border:1px solid var(--border);
border-radius:14px;
box-shadow:var(--shadow);
padding:12px 14px;
display:flex;flex-wrap:wrap;gap:10px;align-items:center;justify-content:space-between
}
.formulas{display:flex;flex-wrap:wrap;gap:8px;align-items:center}
.formula{font-family:ui-monospace, SFMono-Regular, Menlo, monospace;font-size:11.5px;background:var(--panel-2);border:1px solid var(--border);border-radius:10px;padding:9px 11px;line-height:1.5}
.formula b{color:var(--accent)}
.formula small{color:var(--muted)}
.pill{font-size:11px;letter-spacing:.05em;text-transform:uppercase;background:#eff6ff;border:1px solid #dbeafe;color:#1e40af;padding:6px 10px;border-radius:999px;font-weight:700;white-space:nowrap}
.card{background:var(--panel);border:1px solid var(--border);border-radius:var(--radius);box-shadow:var(--shadow)}
.controls{ display:flex;flex-wrap:wrap;gap:14px;align-items:end;padding:16px 16px 14px}
.field{flex:1;min-width:170px}
.field label{display:block;font-size:11px;letter-spacing:.07em;text-transform:uppercase;color:var(--muted);font-weight:700;margin-bottom:8px}
.field .value{font-family:ui-monospace,monospace;font-size:12px;background:var(--panel-2);border:1px solid var(--border);padding:5px 8px;border-radius:8px;display:inline-block;min-width:56px;text-align:center;margin-left:8px;font-weight:700}
.slider{width:100%;accent-color:var(--accent)}
.scale{font-size:11px;color:var(--muted);display:flex;justify-content:space-between;margin-top:4px}
.btn{appearance:none;border:0;background:var(--text);color:white;padding:10px 16px;border-radius:10px;font-weight:700;font-size:13px;cursor:pointer;white-space:nowrap}
.btn:active{transform:translateY(1px)}
.batch-row{display:flex;gap:8px;flex-wrap:wrap;margin-top:2px}
.chip{appearance:none;border:1.5px solid var(--border);background:var(--panel);padding:7px 12px;border-radius:999px;font-size:12px;cursor:pointer;font-weight:700;color:var(--muted)}
.chip.active{background:var(--sgd);border-color:var(--sgd);color:white;box-shadow:0 4px 14px rgba(236,72,153,.28)}
.main{padding:16px 16px 12px}
.chart-head{display:flex;align-items:flex-start;justify-content:space-between;gap:12px;margin-bottom:6px}
.chart-head h3{margin:0;font-size:11px;letter-spacing:.08em;text-transform:uppercase;color:var(--muted);font-weight:700}
.chart-head h2{margin:5px 0 0;font-size:17px;letter-spacing:-.015em;font-weight:800}
.chart-head p{margin:6px 0 0;color:var(--muted);font-size:12.5px;line-height:1.55;max-width:640px}
.badge{font-size:11px;font-weight:700;letter-spacing:.06em;text-transform:uppercase;padding:6px 9px;border-radius:999px;border:1px solid var(--border);background:var(--panel-2);color:var(--muted);white-space:nowrap}
canvas{width:100%;display:block}
#cLoss{height:300px}
#cDeltaGD{height:320px}
#cDeltaSGD{height:320px}
#cScatterGD{height:240px}
#cScatterSGD{height:240px}
#cSteps{height:330px}
#cSchedule{height:340px}
.legend{display:flex;gap:14px;flex-wrap:wrap;align-items:center;margin:10px 0 2px;line-height:1.5;overflow:visible;padding-top:2px}
.legend-item{display:flex;align-items:center;gap:7px;font-size:12.5px;font-weight:650;color:var(--muted);line-height:1.4;overflow:visible}
.dot{width:11px;height:11px;border-radius:50%}
.two-col{display:grid;grid-template-columns:1fr 1fr;gap:14px;margin-top:14px}
@media(max-width:800px){.two-col{grid-template-columns:1fr}}
.kpis{display:grid;grid-template-columns:1fr 1fr 1fr;gap:12px;margin-top:14px}
@media(max-width:720px){.kpis{grid-template-columns:1fr}}
.kpi{border:1px solid var(--border);border-radius:14px;padding:14px;background:var(--panel)}
.kpi.gd{border-color:#bfdbfe;background:#f8fbff}
.kpi.sgd{border-color:#fbcfe8;background:#fff7fb}
.kpi .label{font-size:11px;letter-spacing:.08em;text-transform:uppercase;color:var(--muted);font-weight:700;display:flex;align-items:center;gap:6px}
.kpi .num{font-size:24px;font-weight:800;letter-spacing:-.03em;margin-top:6px;line-height:1}
.kpi .hint{font-size:12px;color:var(--muted);margin-top:6px;line-height:1.5}
.kpi .steps{font-family:ui-monospace,monospace}
.bar{height:6px;background:#f1f5f9;border-radius:999px;overflow:hidden;margin-top:8px}
.bar > div{height:100%;border-radius:999px}
.foot{margin-top:16px;color:var(--muted);font-size:12px;line-height:1.65;text-align:center;max-width:760px;margin-left:auto;margin-right:auto}
.note{background:#fffbeb;border:1px solid #fde68a;border-radius:12px;padding:12px 14px;margin-top:12px;display:flex;gap:10px;align-items:flex-start}
.note b{color:#92400e}
.note p{margin:0;color:#78350f;font-size:12.5px;line-height:1.6}
.verif-grid{display:grid;grid-template-columns:1fr 1fr;gap:14px;margin-top:14px}
@media(max-width:760px){.verif-grid{grid-template-columns:1fr}}
.verif{border:1px solid var(--border);border-radius:14px;padding:14px;background:var(--panel-2)}
.verif h4{margin:0 0 8px;font-size:11px;letter-spacing:.08em;text-transform:uppercase;color:var(--muted);font-weight:800}
.verif table{width:100%;border-collapse:collapse;font-size:12.5px}
.verif td{padding:7px 6px 7px 0;border-bottom:1px solid #eef2f7;vertical-align:top}
.verif td:last-child{text-align:right;font-family:ui-monospace,monospace;font-weight:700}
.verif .ok{color:var(--ok)}
.tag{font-size:11px;font-weight:700;letter-spacing:.06em;text-transform:uppercase;padding:3px 7px;border-radius:999px;border:1px solid var(--border);background:white;color:var(--muted)}
.mono{font-family:ui-monospace, SFMono-Regular, Menlo, monospace}
.help{font-size:11px;color:var(--muted);margin-top:6px;line-height:1.5}
.hover-info{font-family:ui-monospace,monospace;font-size:11px;color:var(--muted);min-height:18px;margin-top:6px}
</style>
</head>
<body>
<div class="wrap">
<header>
<div class="eyebrow">Noise Scale · Delta Loss per Step Verification</div>
<h1>ΔL at each step: <span>full-batch vs mini-batch</span></h1>
<p class="sub">
Same data, same starting weights, optimal step size at every step. We plot the <b style="color:var(--text)">actual change in training loss ΔL<sub>t</sub> = L<sub>t+1</sub> − L<sub>t</sub></b> and the <b style="color:var(--text)">change predicted by the noise-scale theory</b>.
For this task the theory is exact, so the two should line up. Then we turn that into an exchange rate:
<b style="color:var(--text)">how many mini-batch steps it takes to buy the loss drop of one full-batch step</b>, at every step of training.
</p>
<div class="formula-bar">
<div class="formulas">
<div class="formula"><b>Optimal step size</b> ε<sub>opt</sub>(B) = |G|² / ( G<sup>T</sup>HG + tr(HΣ)/B ) <small>— Eq 2.6</small></div>
<div class="formula"><b>Predicted improvement</b> ΔL<sub>opt</sub>(B) = −½|G|⁴ / (G<sup>T</sup>HG + tr(HΣ)/B) <small>— Eq 2.5 at optimum</small></div>
<div class="formula"><b>Noise scale</b> B<sub>noise</sub> = tr(HΣ) / G<sup>T</sup>HG <small>— Eq 2.8</small></div>
</div>
<div class="pill" id="pillBnoise">Average noise scale ≈ —</div>
</div>
<p class="help" style="margin-top:8px;max-width:760px">
The learning rate is <b>not a slider</b> — it is derived at each step from the current gradient, curvature, and noise. Full-batch uses ε = |G|² / G<sup>T</sup>HG. Mini-batch divides by (1 + B<sub>noise</sub>/B), so it must take a smaller step when the gradient is noisy. This automatically gives the largest possible drop in loss for that batch size.
</p>
</header>
<!-- Controls -->
<div class="card">
<div class="controls">
<div class="field" style="flex:1">
<label>Mini-batch size B <span style="font-weight:400;text-transform:none;letter-spacing:0;color:var(--muted)">(full-batch is always N=256)</span></label>
<div class="batch-row" id="batchChips"></div>
<div class="help">Smaller B means noisier gradient. Larger B is closer to full-batch.</div>
</div>
<div class="field" style="flex:0.9">
<label>Target training loss <span class="value" id="v-target">0.10</span></label>
<input id="target" class="slider" type="range" min="0.06" max="0.20" step="0.005" value="0.10"/>
<div class="scale"><span>0.06 tight</span><span>0.20 early</span></div>
</div>
<button class="btn" id="runBtn">↻ New random data</button>
</div>
</div>
<!-- Loss trajectory (context) -->
<div class="card main" style="margin-top:14px">
<div class="chart-head">
<div>
<h3>Context · Training loss at each step</h3>
<h2>How the loss goes down over time</h2>
<p>Full training loss on all 256 examples, log scale. Dashed line = your target. Vertical lines = first step that reaches the target. The area under the ΔL plots below adds up to this curve.</p>
</div>
<span class="badge" id="badgeTarget">target 0.10</span>
</div>
<canvas id="cLoss" width="940" height="300"></canvas>
<div class="legend" id="legendLoss"></div>
<div class="kpis" id="kpis"></div>
<div class="note" id="insightBox"><span style="font-size:16px">💡</span><p id="insightText">—</p></div>
</div>
<!-- Two separate Delta plots -->
<div class="two-col">
<div class="card main">
<div class="chart-head">
<div>
<h3>Full-batch (GD) · B = 256</h3>
<h2>Change in loss at each step</h2>
<p>Blue solid = actual measured change. Blue dashed = theory prediction. For full-batch they should overlap perfectly (no noise, and the loss surface is quadratic).</p>
</div>
<span class="badge" style="background:#eff6ff;border-color:#dbeafe;color:#1e40af">GD</span>
</div>
<canvas id="cDeltaGD" width="460" height="320"></canvas>
<div class="legend" id="legendDeltaGD"></div>
<div class="hover-info" id="hoverDeltaGD"></div>
</div>
<div class="card main">
<div class="chart-head">
<div>
<h3>Mini-batch (SGD) · B = <span id="badgeB2">8</span></h3>
<h2>Change in loss at each step</h2>
<p>Pink solid = actual measured change (jiggly because the mini-batch gradient is noisy). Pink dashed = average change predicted by theory. The pink line should run through the middle of the jiggly line.</p>
</div>
<span class="badge" style="background:#fdf2f8;border-color:#fbcfe8;color:#be185d">SGD</span>
</div>
<canvas id="cDeltaSGD" width="460" height="320"></canvas>
<div class="legend" id="legendDeltaSGD"></div>
<div class="hover-info" id="hoverDeltaSGD"></div>
</div>
</div>
<p class="help" style="text-align:center;margin-top:8px">Negative ΔL means the loss went down (good). Zero is the boundary. Hover any delta plot to see exact numbers for that step. Both plots share the same vertical scale so you can compare how much smaller SGD improvements are.</p>
<!-- Two separate scatter verification plots -->
<div class="two-col">
<div class="card main">
<div class="chart-head">
<div>
<h3>Verification · Full-batch</h3>
<h2>Predicted vs actual (each dot is one step)</h2>
<p>If theory is correct, every dot sits on the diagonal line.</p>
</div>
</div>
<canvas id="cScatterGD" width="460" height="240"></canvas>
<div class="legend" id="legendScatterGD"></div>
</div>
<div class="card main">
<div class="chart-head">
<div>
<h3>Verification · Mini-batch</h3>
<h2>Predicted vs actual (each dot is one step)</h2>
<p>On average dots should be on the diagonal, but individual steps scatter around it because of gradient noise.</p>
</div>
</div>
<canvas id="cScatterSGD" width="460" height="240"></canvas>
<div class="legend" id="legendScatterSGD"></div>
</div>
</div>
<!-- Step-by-step verification table - plain language -->
<div class="card main" style="margin-top:14px">
<div class="chart-head">
<div>
<h3>Step-by-step check: does the measured change match the theory at every step?</h3>
<h2>Plain-language verification</h2>
<p>We compare the two numbers at each step: what actually happened vs what the formula predicted. Because the loss surface is exactly quadratic, the full-batch prediction is mathematically exact.</p>
</div>
<span class="tag">Explained without jargon</span>
</div>
<div class="verif-grid" style="margin-top:10px">
<div class="verif" id="verifGD">
<h4>Full-batch (GD) — no randomness</h4>
<table id="tblGD"><tbody></tbody></table>
<div class="help" id="helpGD" style="margin-top:8px"></div>
</div>
<div class="verif" id="verifSGD">
<h4>Mini-batch (SGD) — noisy gradient</h4>
<table id="tblSGD"><tbody></tbody></table>
<div class="help" id="helpSGD" style="margin-top:8px"></div>
</div>
</div>
<div class="note" style="margin-top:14px">
<span style="font-size:16px">🔬</span>
<p id="verifText">—</p>
</div>
<div class="help" style="margin-top:8px">
<b>How to read this:</b> For full-batch, the two lines should be indistinguishable and every dot on the scatter plot should touch the diagonal. For mini-batch, the solid line is jiggly, but its center should follow the dashed predicted line, and the scatter dots should cloud around the diagonal. The gap between the two methods’ improvements is exactly the noise cost: mini-batch must take a smaller step, so its predicted improvement is smaller by a factor of 1 / (1 + noise_scale / batch_size).
</div>
</div>
<!-- Steps to match one full-batch step -->
<div class="card main" style="margin-top:14px">
<div class="chart-head">
<div>
<h3>The exchange rate · steps per step</h3>
<h2>How many mini-batch steps buy the same loss drop as one full-batch step?</h2>
<p>Take the loss drop ΔL that full-batch GD achieves at step t. Starting from the exact same weights, how many
mini-batch steps does SGD need to bank that same drop? The noise-scale theory says
<b style="color:var(--text)">1 + B<sub>noise</sub>/B</b>, and nothing else.</p>
</div>
<span class="badge" id="badgeMatchB">B = 8</span>
</div>
<canvas id="cSteps" width="940" height="330"></canvas>
<div class="legend" id="legendSteps"></div>
<div class="hover-info" id="hoverSteps"></div>
<div class="kpis" id="kpisMatch" style="grid-template-columns:1fr 1fr 1fr"></div>
<div class="note"><span style="font-size:16px">💡</span><p id="matchInsight">—</p></div>
<div class="help" style="margin-top:8px">
<b>Why there are two measured lines.</b> The pink line divides GD's loss drop at step t by the average loss drop of a
<i>single</i> mini-batch step taken from the same weights — that is exactly the quantity 1 + B<sub>noise</sub>/B predicts, so it should
sit on the dashed line. The grey line actually runs SGD from those weights until the loss has fallen that far and counts the steps.
It lands slightly higher, because inside the catch-up window the gradient keeps shrinking, so step 2 of the window buys a little
less than step 1 did — a compounding effect the single-step ratio does not include.
</div>
</div>
<!-- Batch schedule: varying batch size per learning step -->
<div class="card main" style="margin-top:14px">
<div class="chart-head">
<div>
<h3>The exchange rate — batch schedule</h3>
<h2>Varying batch size per step: B<sub>t</sub> ∈ {B<sub>a</sub>, B<sub>b</sub>} at random</h2>
<p>Now the batch size itself is a <b>schedule</b> that varies each step. At every learning
step we flip a fair coin and pick one of two sizes, e.g. B<sub>t</sub> = 8 or 32 (50% each).
The paper's per-step optimum holds <i>conditionally</i> on the realized B<sub>t</sub>:
ε*(B<sub>t</sub>) = |G|²/(G<sup>T</sup>HG + tr(HΣ)/B<sub>t</sub>) and the predicted exchange
rate at step t is <b>S(t) = 1 + B<sub>noise</sub>(t)/B<sub>t</sub></b> — a random jittered sequence.
Averaging over the coin gives E[S] = 1 + B<sub>noise</sub>·E[1/B] = 1 + B<sub>noise</sub>·(1/B<sub>a</sub>+1/B<sub>b</sub>)/2.
Note E[1/B] ≠ 1/E[B]; using the arithmetic mean batch B̄ under-predicts the mean cost.</p>
</div>
<span class="badge" id="badgeSched">B ∈ {8, 32}</span>
</div>
<div class="controls" style="padding:16px 16px 14px;gap:14px;align-items:flex-start;overflow:visible">
<div style="flex:1;min-width:260px">
<label style="font-size:11px;letter-spacing:.07em;text-transform:uppercase;color:var(--muted);font-weight:700;display:block;margin-bottom:6px;line-height:1.6;padding-top:2px;overflow:visible">Schedule: pick B<sub>a</sub> (small) — each step coin picks B<sub>a</sub> vs B<sub>b</sub></label>
<div class="batch-row" id="schedChipsA"></div>
</div>
<div style="flex:1;min-width:260px">
<label style="font-size:11px;letter-spacing:.07em;text-transform:uppercase;color:var(--muted);font-weight:700;display:block;margin-bottom:6px;line-height:1.6;padding-top:2px;overflow:visible">B<sub>b</sub> (large)</label>
<div class="batch-row" id="schedChipsB"></div>
</div>
<button class="btn" id="newSchedBtn" title="Resample the coin flips for the schedule" style="align-self:flex-end">↻ New schedule</button>
</div>
<canvas id="cSchedule" width="940" height="340"></canvas>
<div class="legend" id="legendSchedule"></div>
<div class="hover-info" id="hoverSchedule"></div>
<div style="display:flex;gap:8px;flex-wrap:wrap;margin:8px 16px 0">
<div style="background:var(--panel-2);border:1px solid var(--border);border-radius:10px;padding:8px 10px;font-size:11px;line-height:1.5;flex:1;min-width:200px">
<div class="mono" style="font-weight:800;color:var(--text)">Exchange rate S<sub>theory</sub>(t) = 1 + B<sub>noise</sub>(t)/B<sub>t</sub> — grey dashed</div>
<div style="color:var(--muted)">Predicted exchange rate per full-batch GD step, jagged because realized B<sub>t</sub> jitters between B<sub>a</sub> and B<sub>b</sub>.</div>
</div>
<div style="background:#fff7fb;border:1px solid #fbcfe8;border-radius:10px;padding:8px 10px;font-size:11px;line-height:1.5;flex:1;min-width:200px">
<div style="font-weight:800;color:#be185d">Exchange rate S(t) measured — pink solid + ±2 s.e. band</div>
<div style="color:var(--muted)">Measured exchange rate = ΔL<sub>GD</sub>(t) / E[ΔL<sub>Sgd</sub>(B<sub>t</sub>)] per GD step t, avg over 300 batch draws conditioned on same B<sub>t</sub>. Should sit on grey dashed.</div>
</div>
</div>
<div class="kpis" id="kpisSched" style="grid-template-columns:1fr 1fr 1fr"></div>
<div class="note"><span style="font-size:16px">💡</span><p id="schedInsight">—</p></div>
<div class="help" style="margin-top:8px">
<b>Derivation.</b> Starting from Eq 2.5–2.8: ΔL<sub>opt</sub>(B)=−½|G|⁴/(G<sup>T</sup>HG+tr(HΣ)/B).
Divide full-batch ΔL<sub>GD</sub>=−½|G|⁴/G<sup>T</sup>HG by ΔL<sub>opt</sub>(B<sub>t</sub>) to get S(t)=1+B<sub>noise</sub>/B<sub>t</sub>.
With a schedule the denominator changes each step, so S(t) inherits the schedule's randomness. Its mean over the coin is 1+B<sub>noise</sub>·E[1/B], not 1+B<sub>noise</sub>/E[B]; e.g. for {8,32}, E[1/B]=(1/8+1/32)/2=0.0781 while 1/E[B]=1/20=0.05, a 56% gap. The schedule also affects the *real* SGD trajectory: a small-B step uses a smaller ε*, so progress stalls on tiny-batch steps and surges on large-batch steps — the pink measured line tracks this conditional expectation.
</div>
</div>
<p class="foot">
Setup: 256 training examples, 12 parameters, linear model with squared error. Feature variances span a factor of 200, so the curvature is ill-conditioned and full-batch GD needs tens of distinct steps rather than a handful. Curvature H is constant, so the second-order prediction is not an approximation — it is exact. Gradient G, curvature H, and the full D×D gradient covariance Σ are computed over all 256 examples at each step, and B<sub>noise</sub> = tr(HΣ) / G<sup>T</sup>HG is formed from that explicit Σ — no shortcut. Step size is chosen at each step to give the biggest expected drop (Eq 2.6); for full-batch that is the exact line-search minimiser, so no stability cap is needed. Both methods start from the same weights. Mini-batches are drawn <i>with</i> replacement, which is the sampling assumption behind Σ/B in the paper. Median noise scale is the middle value of B<sub>noise</sub> over the first 30 steps.
</p>
</div>
<script>
/* ---------- Helpers ---------- */
function mulberry32(seed){
return function(){
let t = seed += 0x6D2B79F5;
t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return ((t ^ t >>> 14) >>> 0) / 4294967296;
}
}
function randn(rng){
let u=0,v=0; while(u===0) u=rng(); while(v===0) v=rng();
return Math.sqrt(-2*Math.log(u))*Math.cos(2*Math.PI*v);
}
function dot(a,b){ let s=0; for(let i=0;i<a.length;i++) s+=a[i]*b[i]; return s; }
function zeros(n){ return new Float64Array(n); }
function clone(a){ return new Float64Array(a); }
/* ---------- Data ---------- */
function generateData(seed){
const rng=mulberry32(seed);
// N examples, D parameters. Feature variances span a factor of `spread`, so the curvature
// H is ill-conditioned and full-batch GD needs tens of distinct steps rather than a handful.
const N=256, D=12, sigma=0.3, spread=200;
const X=Array.from({length:N},()=> new Float64Array(D));
for(let i=0;i<N;i++) for(let j=0;j<D;j++){
const eig=Math.pow(spread, -(j/(D-1))); // eigenvalues 1 … 1/200
X[i][j]=randn(rng)*Math.sqrt(eig);
}
const wStar=new Float64Array(D);
for(let j=0;j<D;j++) wStar[j]=(rng()*2-1)*1.2;
const y=new Float64Array(N);
for(let i=0;i<N;i++) y[i]=dot(X[i],wStar)+randn(rng)*sigma;
const w0=new Float64Array(D);
for(let j=0;j<D;j++) w0[j]=randn(rng)*0.5;
const H=Array.from({length:D},()=> new Float64Array(D));
for(let i=0;i<N;i++) for(let a=0;a<D;a++) for(let b=0;b<D;b++) H[a][b]+=X[i][a]*X[i][b]/N;
return {X,y,w0,H,N,D};
}
function matVec(H, v){
const D=v.length;
const out=zeros(D);
for(let a=0;a<D;a++){ let s=0; for(let b=0;b<D;b++) s+=H[a][b]*v[b]; out[a]=s; }
return out;
}
function fullStats(w, data){
// ---- Forward + Backward on FULL dataset (for logging / theory) ----
// Forward: pred_i = dot(X[i], w), err_i = pred_i - y[i], loss = mean 0.5 err^2
// Backward: per-example grad_i[j] = X[i][j] * err_i, full grad G = mean grad_i
const {X,y,N,D,H}=data;
let lossSum=0;
const G=zeros(D);
const perGrads=Array.from({length:N},()=> zeros(D));
for(let i=0;i<N;i++){
const err=dot(X[i],w)-y[i]; // forward
lossSum+=0.5*err*err;
for(let j=0;j<D;j++){ const g=X[i][j]*err; perGrads[i][j]=g; G[j]+=g/N; } // backward
}
const loss=lossSum/N;
const Gnorm2=dot(G,G);
const HG=matVec(H,G);
const GHG=dot(G,HG);
// Covariance of per-example grads: Sigma = Cov(grad_i)
const Sigma=Array.from({length:D},()=> zeros(D));
for(let i=0;i<N;i++) for(let j=0;j<D;j++) perGrads[i][j]-=G[j];
for(let i=0;i<N;i++) for(let a=0;a<D;a++) for(let b=0;b<D;b++) Sigma[a][b]+=perGrads[i][a]*perGrads[i][b]/N;
let trHS=0;
for(let a=0;a<D;a++) for(let b=0;b<D;b++) trHS+=H[a][b]*Sigma[b][a];
let trS=0; for(let a=0;a<D;a++) trS+=Sigma[a][a];
let Bnoise=null, Bsimple=null;
if(GHG>1e-12 && isFinite(trHS) && trHS>=0) Bnoise=trHS/GHG;
if(Bnoise!==null && (!isFinite(Bnoise) || Bnoise>1e6 || Bnoise<0)) Bnoise=null;
if(Gnorm2>1e-12 && isFinite(trS)) Bsimple=trS/Gnorm2;
if(Bsimple!==null && (!isFinite(Bsimple)||Bsimple>1e6||Bsimple<0)) Bsimple=null;
return {loss, G, Gnorm2, GHG, trHS, trS, Bnoise, Bsimple, HG};
}
/* ---------- Helpers: make the training loop readable ---------- */
function sampleMiniBatch(N, B, rng){
// Uniform sample of B indices WITH replacement. This is the sampling assumption behind
// Cov(grad_B) = Sigma/B in the paper; sampling without replacement would shrink the noise
// by (N-B)/(N-1) and the predictions below would be systematically pessimistic.
const idx=new Array(B);
for(let b=0;b<B;b++) idx[b]=Math.floor(rng()*N);
return idx;
}
function miniBatchGradient(w, data, batchIdx){
// Forward + Backward on ONE mini-batch: grad_B = mean X[i]*err_i over batch
const {X, y}=data;
const D=w.length, B=batchIdx.length;
const g=zeros(D);
for(const i of batchIdx){
const err=dot(X[i],w)-y[i]; // forward on this example
for(let j=0;j<D;j++) g[j]+=X[i][j]*err / B; // backward, averaged
}
return g;
}
function optimalEpsilon(stats, B, N){
// Paper's optimal lr: eps* = |G|^2 / (G^T H G + tr(H Sigma)/B)
// For GD (B=N): eps* = |G|^2 / G^T H G, which is the exact line-search minimiser on a
// quadratic, so it never needs a stability cap.
if(stats.GHG<=1e-12) return 0;
let denom;
if(B>=N) denom = stats.GHG;
else denom = stats.GHG + (stats.trHS!==null ? stats.trHS/B : 0);
if(denom<=1e-12) denom = stats.GHG;
let eps = stats.Gnorm2 / denom;
if(!isFinite(eps) || eps<0) eps=0;
return eps;
}
function predictedDelta(stats, B, N){
// Predicted loss change at optimal eps: -0.5 * |G|^4 / denom
if(stats.GHG<=1e-12) return 0;
let denom;
if(B>=N) denom = stats.GHG;
else denom = stats.GHG + (stats.trHS!==null ? stats.trHS/B : 0);
if(denom<=1e-12) denom = stats.GHG;
return -0.5 * stats.Gnorm2*stats.Gnorm2 / denom;
}
/* ---------- Simplified training loop ---------- */
function simulateOptimal(B, data, totalSteps, seed){
const rng=mulberry32(seed + B*1009);
let w=clone(data.w0);
const {D,N}=data;
const losses=[], stats=[], epsilons=[], deltasPred=[], weights=[];
for(let step=0; step<=totalSteps; step++){
// 1) Evaluate full loss + gradient (for plotting + theory)
const st=fullStats(w, data);
losses.push(st.loss);
stats.push(st);
weights.push(clone(w)); // needed to restart SGD from the exact same point later
if(step===totalSteps) break;
// 2) Derive step size and prediction from theory
const eps=optimalEpsilon(st, B, N);
const pred=predictedDelta(st, B, N);
epsilons.push(eps);
deltasPred.push(pred);
// 3) Sample batch, compute batch gradient, update
const gStep = (B>=N) ? st.G : miniBatchGradient(w, data, sampleMiniBatch(N, B, rng));
for(let j=0;j<D;j++) w[j] -= eps * gStep[j]; // SGD update
}
const deltasMeasured=[];
for(let t=0;t<totalSteps;t++) deltasMeasured.push(losses[t+1]-losses[t]);
const lossPts = losses.map((y,x)=>({x,y}));
const deltaMeasPts = deltasMeasured.map((y,x)=>({x,y}));
const deltaPredPts = deltasPred.map((y,x)=>({x,y}));
return {losses:lossPts, deltaMeasPts, deltaPredPts, deltasMeasured, deltasPred, epsilons, stats, weights};
}
/* ---------- How many mini-batch steps equal one full-batch step? ----------
Two readings of the same question, both measured against the same GD trajectory.
(a) RATIO OF PER-STEP IMPROVEMENTS.
From the exact weights w_t that GD is at, take ONE mini-batch step with the optimal
eps*(B) and record the real change in full training loss. Average over many batches.
steps = deltaL_GD(t) / mean deltaL_SGD(t).
This is the quantity the paper predicts: it should equal 1 + B_noise/B.
(b) STEPS ACTUALLY NEEDED TO CATCH UP.
From the same w_t, run mini-batch SGD until the full training loss has fallen to
L_GD(t+1) - i.e. until it has banked the same loss delta - and count the steps.
This runs a little higher than (a): within the catch-up window the gradient is
shrinking, so the later steps in the window buy less than the first one did. */
function lossAt(w, data){
// Full training loss only. O(N*D) - no gradient, no covariance.
const {X,y,N}=data;
let s=0;
for(let i=0;i<N;i++){ const e=dot(X[i],w)-y[i]; s+=0.5*e*e; }
return s/N;
}
function deltaRatioAtStep(w, st, dLgd, data, B, reps, rng){
// (a) mean one-step loss change of mini-batch SGD, started from w.
const {N,D}=data;
const eps=optimalEpsilon(st, B, N);
if(eps<=0 || !(dLgd<0)) return null;
const wTry=zeros(D);
let sum=0, sumSq=0;
for(let r=0;r<reps;r++){
const g=miniBatchGradient(w, data, sampleMiniBatch(N, B, rng));
for(let j=0;j<D;j++) wTry[j]=w[j]-eps*g[j];
const d=lossAt(wTry, data)-st.loss;
sum+=d; sumSq+=d*d;
}
const m=sum/reps;
if(!(m<0)) return null;
const varr=Math.max(0,(sumSq/reps - m*m))*reps/Math.max(1,reps-1);
const se=Math.sqrt(varr/reps);
return {
steps: dLgd/m,
lo: (m-2*se<0)? dLgd/(m-2*se) : null, // tighter bound (SGD doing better)
hi: (m+2*se<0)? dLgd/(m+2*se) : null
};
}
function catchUpAtStep(w, target, data, B, reps, maxK, seedBase, budget){
// (b) median number of SGD steps to reach the loss level GD reached in one step.
const {N,D}=data;
const counts=[];
let spent=0;
for(let r=0;r<reps;r++){
if(spent>=budget) break;
const rng=mulberry32(seedBase + r*7919);
const wv=clone(w);
let st=fullStats(wv, data);
let k=0, reached=false;
while(k<maxK && spent<budget){
const eps=optimalEpsilon(st, B, N);
if(eps<=0) break;
const g=miniBatchGradient(wv, data, sampleMiniBatch(N, B, rng));
for(let j=0;j<D;j++) wv[j]-=eps*g[j];
k++; spent++;
st=fullStats(wv, data);
if(st.loss<=target){ reached=true; break; }
}
if(reached) counts.push(k);
}
return {steps: counts.length? median(counts) : null, spent};
}
function measureStepsToMatch(gdRes, data, B, T, opts){
// Small batches need an order of magnitude more catch-up steps per GD step, so they get
// fewer replicate runs. The delta-ratio measurement is cheap and keeps its full replicate count.
const reps = B>=16 ? opts.reps : opts.repsSmall;
const deltaReps=opts.deltaReps, maxK=opts.maxK;
let budget=opts.budget;
const theory=[], ratio=[], ratioLo=[], ratioHi=[], catchUp=[];
const rng=mulberry32(4242 + B*104729);
for(let t=0;t<T;t++){
const st=gdRes.stats[t];
const dLgd=gdRes.deltasMeasured[t];
theory.push((st.Bnoise!==null && isFinite(st.Bnoise)) ? 1+st.Bnoise/B : null);
const rr=deltaRatioAtStep(gdRes.weights[t], st, dLgd, data, B, deltaReps, rng);
ratio.push(rr? rr.steps : null);
ratioLo.push(rr? rr.lo : null);
ratioHi.push(rr? rr.hi : null);
if(budget>0){
const cu=catchUpAtStep(gdRes.weights[t], gdRes.losses[t+1].y, data, B, reps, maxK, 9001+t*131+B*7919, budget);
budget-=cu.spent;
catchUp.push(cu.steps);
} else {
catchUp.push(null);
}
}
return {theory, ratio, ratioLo, ratioHi, catchUp, budgetLeft:budget};
}
function stepsToTarget(losses, target){
for(let i=0;i<losses.length;i++) if(losses[i].y <= target) return losses[i].x;
return null;
}
function median(arr){
const s=[...arr].sort((a,b)=>a-b);
return s[Math.floor(s.length/2)];
}
function pearson(xs, ys){
const n=Math.min(xs.length, ys.length);
if(n<2) return null;
let sx=0, sy=0;
for(let i=0;i<n;i++){ sx+=xs[i]; sy+=ys[i]; }
sx/=n; sy/=n;
let num=0, dx=0, dy=0;
for(let i=0;i<n;i++){ const dx_i=xs[i]-sx, dy_i=ys[i]-sy; num+=dx_i*dy_i; dx+=dx_i*dx_i; dy+=dy_i*dy_i; }
dx=Math.sqrt(dx); dy=Math.sqrt(dy);
if(dx===0||dy===0) return null;
return num/(dx*dy);
}
function mean(arr){ if(!arr.length) return NaN; return arr.reduce((a,b)=>a+b,0)/arr.length; }
/* ---------- Drawing ---------- */
function setupCanvas(canvas){
const dpr=window.devicePixelRatio||1;
const rect=canvas.getBoundingClientRect();
const W=Math.round(rect.width*dpr), H=Math.round(rect.height*dpr);
if(canvas.width!==W) canvas.width=W;
if(canvas.height!==H) canvas.height=H;
const ctx=canvas.getContext('2d');
ctx.setTransform(dpr,0,0,dpr,0,0);
return {ctx, width:rect.width, height:rect.height};
}
function getLogTicks(min,max){
const ticks=[];
const start=Math.pow(10, Math.floor(Math.log10(Math.max(min,1e-9))));
for(let v=start; v<=max*1.02; v*=10){ ticks.push(v); if(v*2<=max) ticks.push(v*2); if(v*5<=max) ticks.push(v*5); }
return ticks.filter(v=>v>=min*0.9 && v<=max*1.1).sort((a,b)=>a-b);
}
function drawLoss(canvas, gdLosses, sgdLosses, target, gdCross, sgdCross, B){
const {ctx,width,height}=setupCanvas(canvas);
ctx.clearRect(0,0,width,height);
const pad={l:56,r:16,t:14,b:30};
const plotW=width-pad.l-pad.r, plotH=height-pad.t-pad.b;
let yMin=Infinity,yMax=-Infinity, xMax=0;
for(const pt of gdLosses){ yMin=Math.min(yMin,pt.y); yMax=Math.max(yMax,pt.y); xMax=Math.max(xMax,pt.x); }
for(const pt of sgdLosses){ yMin=Math.min(yMin,pt.y); yMax=Math.max(yMax,pt.y); }
yMin=Math.min(yMin, target*0.85);
let minPos=Math.min(...gdLosses.filter(p=>p.y>0).map(p=>p.y), ...sgdLosses.filter(p=>p.y>0).map(p=>p.y));
yMin=Math.max(minPos*0.75, Math.min(yMin, minPos*0.9));
yMax*=1.12;
const yMinLog=Math.log10(yMin), yMaxLog=Math.log10(yMax);
const xMin=0;
function xToPx(x){ return pad.l + (x - xMin)/(Math.max(1,xMax)-xMin)*plotW; }
function yToPx(y){ if(y<=0) return pad.t+plotH; const ly=Math.log10(y); return pad.t + (1-(ly-yMinLog)/(yMaxLog-yMinLog))*plotH; }
ctx.strokeStyle='#eef2f7'; ctx.lineWidth=1; ctx.beginPath();
for(let i=0;i<=6;i++){ const x=xMin+(xMax-xMin)*i/6; const px=xToPx(x); ctx.moveTo(px,pad.t); ctx.lineTo(px,pad.t+plotH); }
const ticks=getLogTicks(yMin,yMax);
for(const v of ticks){ const py=yToPx(v); ctx.moveTo(pad.l,py); ctx.lineTo(pad.l+plotW,py); }
ctx.stroke();
const pyT=yToPx(target);
ctx.save();
ctx.fillStyle='rgba(245,158,11,0.07)'; ctx.fillRect(pad.l, pyT, plotW, pad.t+plotH - pyT);
ctx.strokeStyle='#f59e0b'; ctx.lineWidth=1.9; ctx.setLineDash([7,6]); ctx.beginPath(); ctx.moveTo(pad.l,pyT); ctx.lineTo(pad.l+plotW,pyT); ctx.stroke(); ctx.setLineDash([]);
ctx.fillStyle='#92400e'; ctx.font='11px ui-monospace, monospace';
const label='target '+target.toFixed(2);
const tw=ctx.measureText(label).width;
ctx.fillStyle='#fffbeb'; ctx.strokeStyle='#fde68a'; ctx.lineWidth=1;
ctx.beginPath(); if(ctx.roundRect) ctx.roundRect(pad.l+plotW - tw - 18, pyT-20, tw+14, 16, 8); else ctx.rect(pad.l+plotW - tw - 18, pyT-20, tw+14,16);
ctx.fill(); ctx.stroke();
ctx.fillStyle='#92400e'; ctx.fillText(label, pad.l+plotW - tw - 11, pyT-9);
ctx.restore();
function drawCross(x, color, label){
if(x===null) return;
const px=xToPx(x);
ctx.save();
ctx.strokeStyle=color; ctx.lineWidth=1.6; ctx.setLineDash([5,5]); ctx.globalAlpha=0.95;
ctx.beginPath(); ctx.moveTo(px,pad.t); ctx.lineTo(px, yToPx(target)); ctx.stroke(); ctx.setLineDash([]);
ctx.fillStyle=color; ctx.beginPath(); ctx.arc(px,yToPx(target),5,0,Math.PI*2); ctx.fill();
ctx.fillStyle='white'; ctx.beginPath(); ctx.arc(px,yToPx(target),2,0,Math.PI*2); ctx.fill();
ctx.font='11px ui-monospace, monospace';
const t=label; const w=ctx.measureText(t).width;
const bx=Math.max(pad.l, Math.min(px - w/2 - 7, pad.l+plotW - w - 14)); const by=pad.t+8;
ctx.fillStyle='white'; ctx.strokeStyle=color; ctx.lineWidth=1.2;
ctx.beginPath(); if(ctx.roundRect) ctx.roundRect(bx, by, w+14, 18, 9); else ctx.rect(bx,by,w+14,18); ctx.fill(); ctx.stroke();
ctx.fillStyle=color; ctx.fillText(t, bx+7, by+12);
ctx.beginPath(); ctx.moveTo(px, by+18); ctx.lineTo(px, pad.t+24); ctx.stroke();
ctx.restore();
}
function drawLine(losses, color, width){
ctx.beginPath(); ctx.strokeStyle=color; ctx.lineWidth=width; ctx.lineJoin='round'; ctx.lineCap='round';
let started=false;
for(const p of losses){ if(!isFinite(p.y)||p.y<=0) continue; const px=xToPx(p.x), py=yToPx(p.y); if(!started){ctx.moveTo(px,py);started=true;} else ctx.lineTo(px,py); }
ctx.stroke();
}
let sgdDisplay=sgdLosses;
if(sgdLosses.length>2){
const out=[sgdLosses[0]]; let prev=sgdLosses[0].y;
for(let i=1;i<sgdLosses.length;i++){ const y=0.28*sgdLosses[i].y+0.72*prev; out.push({x:sgdLosses[i].x, y}); prev=y; }
sgdDisplay=out;
}
drawLine(gdLosses,'#2563eb',2.7);
drawLine(sgdDisplay,'#ec4899',2.2);
drawCross(gdCross,'#2563eb', gdCross===null? 'GD: not reached' : 'GD: '+gdCross+' steps');
drawCross(sgdCross,'#ec4899', sgdCross===null? 'SGD: not reached' : 'SGD B='+B+': '+sgdCross+' steps');
ctx.strokeStyle='#cbd5e1'; ctx.lineWidth=1.1; ctx.beginPath(); ctx.moveTo(pad.l,pad.t); ctx.lineTo(pad.l,pad.t+plotH); ctx.lineTo(pad.l+plotW,pad.t+plotH); ctx.stroke();
ctx.fillStyle='#64748b'; ctx.font='11px ui-monospace, monospace'; ctx.textAlign='center';
for(let i=0;i<=6;i++){ const x=Math.round(xMin+(xMax-xMin)*i/6); ctx.fillText(String(x), xToPx(x), pad.t+plotH+18); }
ctx.textAlign='right';
const shown=ticks.filter((_,i)=> i%2===0);
for(const v of shown){ const py=yToPx(v); if(py<pad.t-2||py>pad.t+plotH+2) continue; let lab=v>=10? v.toFixed(1): v>=1? v.toFixed(1): v.toFixed(2); if(v<0.1) lab=v.toFixed(2); ctx.fillText(lab, pad.l-8, py+3); }
ctx.fillStyle='#94a3b8'; ctx.font='11px Inter, sans-serif'; ctx.textAlign='center';
ctx.fillText('optimizer step', pad.l+plotW/2, height-5);
ctx.save(); ctx.translate(13, pad.t+plotH/2); ctx.rotate(-Math.PI/2); ctx.textAlign='center'; ctx.fillText('training loss (log scale)',0,0); ctx.restore();
}
function drawDeltaSingle(canvas, measuredPts, predPts, colorMeas, colorPred, hoverStep, globalRange){
const {ctx,width,height}=setupCanvas(canvas);
ctx.clearRect(0,0,width,height);
const pad={l:56,r:16,t:16,b:30};
const plotW=width-pad.l-pad.r, plotH=height-pad.t-pad.b;
// y range: use globalRange if provided, else compute from this series
let yMin, yMax;
if(globalRange){
yMin=globalRange.yMin; yMax=globalRange.yMax;
} else {
const allY=[...measuredPts.map(p=>p.y), ...predPts.map(p=>p.y)];
yMin=Math.min(...allY); yMax=Math.max(...allY);
yMin=Math.min(yMin,-0.005); yMax=Math.max(yMax,0.005);
const padY=(yMax-yMin)*0.10; yMin-=padY; yMax+=padY;
}
const xMin=0, xMax=Math.max(measuredPts.length-1, predPts.length-1,1);
function xToPx(x){ return pad.l + (x - xMin)/(xMax - xMin)*plotW; }
function yToPx(y){ return pad.t + (1 - (y - yMin)/(yMax - yMin))*plotH; }
// grid
ctx.strokeStyle='#f1f5f9'; ctx.lineWidth=1; ctx.beginPath();
for(let i=0;i<=6;i++){ const x=xMin+(xMax-xMin)*i/6; ctx.moveTo(xToPx(x),pad.t); ctx.lineTo(xToPx(x),pad.t+plotH); }
for(let i=0;i<=6;i++){ const y=yMin+(yMax-yMin)*i/6; ctx.moveTo(pad.l,yToPx(y)); ctx.lineTo(pad.l+plotW,yToPx(y)); }
ctx.stroke();
// zero line + improvement shading
const py0=yToPx(0);
if(py0>=pad.t && py0<=pad.t+plotH){
ctx.save();
ctx.strokeStyle='#94a3b8'; ctx.lineWidth=1.2; ctx.setLineDash([6,5]); ctx.beginPath(); ctx.moveTo(pad.l,py0); ctx.lineTo(pad.l+plotW,py0); ctx.stroke(); ctx.setLineDash([]);
ctx.fillStyle='#64748b'; ctx.font='11px ui-monospace, monospace'; ctx.textAlign='right'; ctx.fillText('0', pad.l-8, py0+3);
if(py0 < pad.t+plotH){
ctx.fillStyle='rgba(34,197,94,0.07)'; ctx.fillRect(pad.l, py0, plotW, pad.t+plotH - py0);
ctx.fillStyle='#059669'; ctx.globalAlpha=0.75; ctx.font='10px Inter, sans-serif'; ctx.textAlign='left'; ctx.fillText('improvement ↓', pad.l+8, py0+14); ctx.globalAlpha=1;
}
ctx.restore();
}
function drawLine(pts, color, width, dash=null, alpha=1){
if(!pts.length) return;
ctx.save();
ctx.beginPath(); ctx.strokeStyle=color; ctx.lineWidth=width; ctx.globalAlpha=alpha; ctx.lineJoin='round'; ctx.lineCap='round';
if(dash) ctx.setLineDash(dash);
let started=false;
for(const p of pts){ if(!isFinite(p.y)) continue; const px=xToPx(p.x), py=yToPx(p.y); if(!started){ctx.moveTo(px,py);started=true;} else ctx.lineTo(px,py); }
ctx.stroke(); ctx.restore();
}
// predicted behind
drawLine(predPts, colorPred, 1.9, [7,5], 0.95);
// measured
drawLine(measuredPts, colorMeas, 2.6);
// hover
if(hoverStep!==null && hoverStep>=0 && hoverStep<=xMax){
const px=xToPx(hoverStep);
ctx.save(); ctx.strokeStyle='rgba(15,23,42,0.18)'; ctx.lineWidth=1; ctx.setLineDash([4,4]); ctx.beginPath(); ctx.moveTo(px,pad.t); ctx.lineTo(px,pad.t+plotH); ctx.stroke(); ctx.restore();
const mY=measuredPts[hoverStep]?.y, pY=predPts[hoverStep]?.y;
if(isFinite(mY)){ const py=yToPx(mY); ctx.fillStyle=colorMeas; ctx.beginPath(); ctx.arc(px,py,4,0,Math.PI*2); ctx.fill(); ctx.fillStyle='white'; ctx.beginPath(); ctx.arc(px,py,1.7,0,Math.PI*2); ctx.fill(); }
if(isFinite(pY)){ const py=yToPx(pY); ctx.strokeStyle=colorPred; ctx.lineWidth=1.5; ctx.beginPath(); ctx.arc(px,py,4.5,0,Math.PI*2); ctx.stroke(); }
}
// axes
ctx.strokeStyle='#cbd5e1'; ctx.lineWidth=1.1; ctx.beginPath(); ctx.moveTo(pad.l,pad.t); ctx.lineTo(pad.l,pad.t+plotH); ctx.lineTo(pad.l+plotW,pad.t+plotH); ctx.stroke();
ctx.fillStyle='#64748b'; ctx.font='11px ui-monospace, monospace'; ctx.textAlign='center';
for(let i=0;i<=6;i++){ const x=Math.round(xMin+(xMax-xMin)*i/6); ctx.fillText(String(x), xToPx(x), pad.t+plotH+18); }
ctx.textAlign='right';
for(let i=0;i<=6;i++){ const y=yMin+(yMax-yMin)*i/6; const py=yToPx(y); if(py<pad.t-2||py>pad.t+plotH+2) continue; if(Math.abs(y)<0.0005) continue; ctx.fillText(y.toFixed(3), pad.l-8, py+3); }
ctx.fillStyle='#94a3b8'; ctx.font='11px Inter, sans-serif'; ctx.textAlign='center';
ctx.fillText('step', pad.l+plotW/2, height-5);
ctx.save(); ctx.translate(14, pad.t+plotH/2); ctx.rotate(-Math.PI/2); ctx.textAlign='center'; ctx.fillText('change in loss',0,0); ctx.restore();
return {xToPx,yToPx,pad,plotW,plotH,yMin,yMax,xMin,xMax};
}
function drawScatterSingle(canvas, predPts, measPts, color){
const {ctx,width,height}=setupCanvas(canvas);
ctx.clearRect(0,0,width,height);
const pad={l:46,r:14,t:14,b:36};
const plotW=width-pad.l-pad.r, plotH=height-pad.t-pad.b;
const predYs=predPts.map(p=>p.y), measYs=measPts.map(p=>p.y);
let minV=Math.min(...predYs, ...measYs), maxV=Math.max(...predYs, ...measYs);
const padV=(maxV-minV)*0.08 || 0.01; minV-=padV; maxV+=padV;
if(minV===maxV){minV-=0.01; maxV+=0.01;}
function xToPx(x){ return pad.l + (x - minV)/(maxV - minV)*plotW; }
function yToPx(y){ return pad.t + (1 - (y - minV)/(maxV - minV))*plotH; }
ctx.strokeStyle='#eef2f7'; ctx.lineWidth=1; ctx.beginPath();
for(let i=0;i<=4;i++){ const v=minV+(maxV-minV)*i/4; ctx.moveTo(xToPx(v),pad.t); ctx.lineTo(xToPx(v),pad.t+plotH); ctx.moveTo(pad.l,yToPx(v)); ctx.lineTo(pad.l+plotW,yToPx(v)); }
ctx.stroke();
// diagonal
ctx.save(); ctx.strokeStyle='#cbd5e1'; ctx.lineWidth=1.4; ctx.setLineDash([6,4]); ctx.beginPath(); ctx.moveTo(xToPx(minV), yToPx(minV)); ctx.lineTo(xToPx(maxV), yToPx(maxV)); ctx.stroke(); ctx.setLineDash([]); ctx.fillStyle='#94a3b8'; ctx.font='10px Inter, sans-serif'; ctx.textAlign='left'; ctx.fillText('perfect prediction', xToPx(minV)+6, yToPx(minV)-8); ctx.restore();
// points
ctx.fillStyle=color;
for(let i=0;i<predPts.length;i++){
const px=xToPx(predPts[i].y), py=yToPx(measPts[i].y);
if(!isFinite(px)||!isFinite(py)) continue;
ctx.beginPath(); ctx.arc(px,py,3.3,0,Math.PI*2); ctx.fill();
ctx.fillStyle='white'; ctx.beginPath(); ctx.arc(px,py,1.2,0,Math.PI*2); ctx.fill(); ctx.fillStyle=color;
}
ctx.strokeStyle='#0f172a'; ctx.lineWidth=1.1; ctx.beginPath(); ctx.moveTo(pad.l,pad.t+plotH); ctx.lineTo(pad.l+plotW,pad.t+plotH); ctx.moveTo(pad.l,pad.t); ctx.lineTo(pad.l,pad.t+plotH); ctx.stroke();
ctx.fillStyle='#0f172a'; ctx.font='11px ui-monospace, monospace'; ctx.textAlign='center'; ctx.fillText('predicted change', pad.l+plotW/2, height-7);
ctx.save(); ctx.translate(12, pad.t+plotH/2); ctx.rotate(-Math.PI/2); ctx.textAlign='center'; ctx.fillText('actual change',0,0); ctx.restore();
ctx.fillStyle='#64748b'; ctx.font='10px ui-monospace, monospace'; ctx.textAlign='center';
for(let i=0;i<=4;i++){ const v=minV+(maxV-minV)*i/4; ctx.fillText(v.toFixed(3), xToPx(v), pad.t+plotH+14); }
ctx.textAlign='right'; for(let i=0;i<=4;i++){ const v=minV+(maxV-minV)*i/4; ctx.fillText(v.toFixed(3), pad.l-8, yToPx(v)+3); }
}
function drawStepsToMatch(canvas, m, B, T, hoverStep){
const {ctx,width,height}=setupCanvas(canvas);
ctx.clearRect(0,0,width,height);
const pad={l:58,r:16,t:18,b:42};
const plotW=width-pad.l-pad.r, plotH=height-pad.t-pad.b;
let yMax=1.4;
for(let t=0;t<T;t++){
for(const v of [m.theory[t], m.ratio[t], m.ratioHi[t], m.catchUp[t]])
if(v!==null && isFinite(v) && v>0) yMax=Math.max(yMax, v);
}
yMax*=1.35;
const yMin=0.9;
const lmin=Math.log10(yMin), lmax=Math.log10(yMax);
const xMax=Math.max(T,2);
function xToPx(x){ return pad.l + (x-1)/(xMax-1)*plotW; }
function yToPx(y){ return pad.t + (1-(Math.log10(Math.max(y,1e-6))-lmin)/(lmax-lmin))*plotH; }
// grid
const yTicks=getLogTicks(yMin,yMax);
ctx.strokeStyle='#eef2f7'; ctx.lineWidth=1; ctx.beginPath();
for(let i=0;i<=6;i++){ const px=xToPx(1+(xMax-1)*i/6); ctx.moveTo(px,pad.t); ctx.lineTo(px,pad.t+plotH); }
for(const v of yTicks){ const py=yToPx(v); ctx.moveTo(pad.l,py); ctx.lineTo(pad.l+plotW,py); }
ctx.stroke();
// "1 step = 1 step" reference
const py1=yToPx(1);
ctx.save();
ctx.strokeStyle='#94a3b8'; ctx.lineWidth=1.3; ctx.setLineDash([6,5]);
ctx.beginPath(); ctx.moveTo(pad.l,py1); ctx.lineTo(pad.l+plotW,py1); ctx.stroke();
ctx.restore();
ctx.fillStyle='#94a3b8'; ctx.font='10px Inter, sans-serif'; ctx.textAlign='left';
ctx.fillText('1 = mini-batch keeps up with full-batch', pad.l+8, py1-6);
// ±2 s.e. band on the delta ratio
const bandPts=[];
for(let t=0;t<T;t++) if(m.ratioLo[t]!==null && m.ratioHi[t]!==null && m.ratioHi[t]>0)
bandPts.push({x:xToPx(t+1), hi:yToPx(m.ratioHi[t]), lo:yToPx(m.ratioLo[t])});
if(bandPts.length>1){
ctx.save(); ctx.fillStyle='rgba(236,72,153,0.13)'; ctx.beginPath();
ctx.moveTo(bandPts[0].x, bandPts[0].hi);
for(let i=1;i<bandPts.length;i++) ctx.lineTo(bandPts[i].x, bandPts[i].hi);
for(let i=bandPts.length-1;i>=0;i--) ctx.lineTo(bandPts[i].x, bandPts[i].lo);
ctx.closePath(); ctx.fill(); ctx.restore();
}
function drawSeries(arr, color, width, dash){
ctx.save();
ctx.beginPath(); ctx.strokeStyle=color; ctx.lineWidth=width; ctx.lineJoin='round'; ctx.lineCap='round';
if(dash) ctx.setLineDash(dash);
let started=false;
for(let t=0;t<T;t++){
const v=arr[t];
if(v===null || !isFinite(v) || v<=0){ started=false; continue; }
const px=xToPx(t+1), py=yToPx(v);
if(!started){ ctx.moveTo(px,py); started=true; } else ctx.lineTo(px,py);
}
ctx.stroke(); ctx.restore();
}
drawSeries(m.theory,'#1e3a8a',2.1,[7,5]);
drawSeries(m.catchUp,'#475569',1.6);
drawSeries(m.ratio,'#ec4899',2.6);
for(let t=0;t<T;t++){
const v=m.catchUp[t];
if(v===null||!isFinite(v)) continue;
ctx.fillStyle='#475569'; ctx.beginPath(); ctx.arc(xToPx(t+1),yToPx(v),2.7,0,Math.PI*2); ctx.fill();
}
// where the catch-up measurement ran out of budget
let lastMeasured=-1;
for(let t=0;t<T;t++) if(m.catchUp[t]!==null) lastMeasured=t;
if(lastMeasured>=0 && lastMeasured<T-1){
const px=xToPx(lastMeasured+1);
ctx.save();
ctx.fillStyle='rgba(148,163,184,0.10)'; ctx.fillRect(px, pad.t, pad.l+plotW-px, plotH);
ctx.strokeStyle='#cbd5e1'; ctx.lineWidth=1; ctx.setLineDash([3,3]);
ctx.beginPath(); ctx.moveTo(px,pad.t); ctx.lineTo(px,pad.t+plotH); ctx.stroke();
ctx.restore();
ctx.fillStyle='#94a3b8'; ctx.font='10px Inter, sans-serif'; ctx.textAlign='left';
ctx.fillText('catch-up runs stop here (step budget)', px+6, pad.t+12);
}
// hover marker
if(hoverStep!==null && hoverStep>=1 && hoverStep<=T){
const px=xToPx(hoverStep);
ctx.save(); ctx.strokeStyle='rgba(15,23,42,0.18)'; ctx.lineWidth=1; ctx.setLineDash([4,4]);
ctx.beginPath(); ctx.moveTo(px,pad.t); ctx.lineTo(px,pad.t+plotH); ctx.stroke(); ctx.restore();
const v=m.ratio[hoverStep-1];
if(v!==null && isFinite(v)){
ctx.fillStyle='#ec4899'; ctx.beginPath(); ctx.arc(px,yToPx(v),4.2,0,Math.PI*2); ctx.fill();
ctx.fillStyle='white'; ctx.beginPath(); ctx.arc(px,yToPx(v),1.7,0,Math.PI*2); ctx.fill();
}
}
// axes + labels
ctx.strokeStyle='#cbd5e1'; ctx.lineWidth=1.1; ctx.beginPath();
ctx.moveTo(pad.l,pad.t); ctx.lineTo(pad.l,pad.t+plotH); ctx.lineTo(pad.l+plotW,pad.t+plotH); ctx.stroke();
ctx.fillStyle='#64748b'; ctx.font='11px ui-monospace, monospace'; ctx.textAlign='center';
for(let i=0;i<=6;i++){ const x=Math.round(1+(xMax-1)*i/6); ctx.fillText(String(x), xToPx(x), pad.t+plotH+18); }
ctx.textAlign='right';
for(const v of yTicks){
const py=yToPx(v);
if(py<pad.t-2||py>pad.t+plotH+2) continue;
ctx.fillText(v>=10? String(Math.round(v)) : v.toFixed(1), pad.l-8, py+3);
}
ctx.fillStyle='#94a3b8'; ctx.font='11px Inter, sans-serif'; ctx.textAlign='center';
ctx.fillText('full-batch step t', pad.l+plotW/2, height-6);
ctx.save(); ctx.translate(14, pad.t+plotH/2); ctx.rotate(-Math.PI/2); ctx.textAlign='center';
ctx.fillText('exchange rate S = mini-batch steps / GD step (log)',0,0); ctx.restore();
}
/* ---------- Batch schedule helpers ---------- */
function generateSchedule(T, Ba, Bb, seed){
// Random choice from two batch sizes per learning step: coin flip each t.
// Ba, Bb are the two choices (e.g. 8 and 32), seed controls the coin flips.
const rng = mulberry32(seed);
const sched = new Array(T);
for(let t=0; t<T; t++) sched[t] = rng() < 0.5 ? Ba : Bb;
return sched;
}
function measureSchedule(gdRes, data, schedule, T, opts){
// Per-step exchange rate conditioned on realized schedule Bt = schedule[t].
// Theory: S_theory(t) = 1 + B_noise(t)/B_t (conditional on Bt)
// Measured: S_meas(t) = ΔL_GD(t) / E[ΔL_sgd(B_t)] (average over batch noise, conditioned on Bt)
const deltaReps = opts.deltaReps;
const theory=[], ratio=[], ratioLo=[], ratioHi=[];
for(let t=0; t<T; t++){
const st = gdRes.stats[t];
const dLgd = gdRes.deltasMeasured[t];
const Bt = schedule[t];
theory.push((st.Bnoise!==null && isFinite(st.Bnoise)) ? 1+st.Bnoise/Bt : null);
const stepRng = mulberry32(424242 + Bt*104729 + t*7919 + schedSeed*31);
const rr = deltaRatioAtStep(gdRes.weights[t], st, dLgd, data, Bt, deltaReps, stepRng);
ratio.push(rr? rr.steps : null);
ratioLo.push(rr? rr.lo : null);
ratioHi.push(rr? rr.hi : null);
}
return {theory, ratio, ratioLo, ratioHi, schedule: schedule.slice()};
}
function drawSchedule(canvas, m, T, hoverStep){
// Draw exchange rate for batch schedule: jagged theory (grey dashed) vs measured (pink solid + band).
// Background shading shows which B_t was chosen at each step (small vs large).
const {ctx,width,height}=setupCanvas(canvas);
ctx.clearRect(0,0,width,height);
const pad={l:58,r:16,t:28,b:42};
const plotW=width-pad.l-pad.r, plotH=height-pad.t-pad.b;
// Cap y-range to keep early steps visible - explosion beyond ~20x is near-optimum (GHG->0)
let yMax=1.4;
for(let t=0; t<T; t++){
for(const v of [m.theory[t], m.ratio[t], m.ratioHi[t]])
if(v!==null && isFinite(v) && v>0) yMax=Math.max(yMax, v);
}
yMax*=1.35;
yMax = Math.min(yMax, 24);
yMax = Math.max(yMax, 5);
const yMin=0.9;
const lmin=Math.log10(yMin), lmax=Math.log10(yMax);
const xMax=Math.max(T,2);
function xToPx(x){ return pad.l + (x-1)/(xMax-1)*plotW; }
function yToPx(y){ const yy=Math.min(Math.max(y,1e-6), yMax); return pad.t + (1-(Math.log10(yy)-lmin)/(lmax-lmin))*plotH; }
// background shading per step for schedule B_t
for(let t=0; t<T; t++){
const Bt = m.schedule[t];
const isSmall = Bt === Math.min(schedA, schedB);
const x0 = xToPx(t+1 - 0.48);
const x1 = xToPx(t+1 + 0.48);
const w = x1 - x0;
// small batch -> pinkish tint (costly), large batch -> blueish/grey tint (cheap)
ctx.fillStyle = isSmall ? 'rgba(236,72,153,0.070)' : 'rgba(37,99,235,0.055)';
// clip to plot area
const left = Math.max(pad.l, x0);
const right = Math.min(pad.l+plotW, x1);
if(right > left) ctx.fillRect(left, pad.t, right-left, plotH);
// thin tick at bottom indicating Bt
ctx.fillStyle = isSmall ? '#ec4899' : '#2563eb';
const tickY = pad.t+plotH - 1;
// small triangle or rect
ctx.fillRect((left+right)/2 - 1, tickY - 6, 2, 6);
}
// grid
const yTicks=getLogTicks(yMin,yMax);
ctx.strokeStyle='#eef2f7'; ctx.lineWidth=1; ctx.beginPath();
for(let i=0;i<=6;i++){ const px=xToPx(1+(xMax-1)*i/6); ctx.moveTo(px,pad.t); ctx.lineTo(px,pad.t+plotH); }
for(const v of yTicks){ const py=yToPx(v); ctx.moveTo(pad.l,py); ctx.lineTo(pad.l+plotW,py); }
ctx.stroke();
// 1 step reference
const py1=yToPx(1);
ctx.save();
ctx.strokeStyle='#94a3b8'; ctx.lineWidth=1.3; ctx.setLineDash([6,5]);
ctx.beginPath(); ctx.moveTo(pad.l,py1); ctx.lineTo(pad.l+plotW,py1); ctx.stroke();
ctx.restore();
ctx.fillStyle='#94a3b8'; ctx.font='10px Inter, sans-serif'; ctx.textAlign='left';
ctx.fillText('1 = schedule keeps up with full-batch', pad.l+8, py1-6);
// band for measured ratio ±2 s.e.
const bandPts=[];
for(let t=0; t<T; t++) if(m.ratioLo[t]!==null && m.ratioHi[t]!==null && m.ratioHi[t]>0)
bandPts.push({x:xToPx(t+1), hi:yToPx(m.ratioHi[t]), lo:yToPx(m.ratioLo[t])});
if(bandPts.length>1){
ctx.save(); ctx.fillStyle='rgba(236,72,153,0.13)'; ctx.beginPath();
ctx.moveTo(bandPts[0].x, bandPts[0].hi);
for(let i=1;i<bandPts.length;i++) ctx.lineTo(bandPts[i].x, bandPts[i].hi);
for(let i=bandPts.length-1;i>=0;i--) ctx.lineTo(bandPts[i].x, bandPts[i].lo);