Lamapi commited on
Commit
28b4298
·
verified ·
1 Parent(s): 506b36e

Publish Next-Shift space

Browse files
Files changed (13) hide show
  1. README.md +52 -4
  2. app.js +92 -0
  3. core.js +98 -0
  4. curves.json +1 -0
  5. data.json +0 -0
  6. evaluation.json +532 -0
  7. index.html +46 -17
  8. model.json +0 -0
  9. nlrt.js +219 -0
  10. nlui.js +123 -0
  11. parity_cases.json +1 -0
  12. preview.png +0 -0
  13. style.css +53 -26
README.md CHANGED
@@ -1,10 +1,58 @@
1
  ---
2
  title: Next Shift
3
- emoji: 📉
4
- colorFrom: gray
5
- colorTo: red
6
  sdk: static
 
7
  pinned: false
 
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  title: Next Shift
3
+ emoji: 🔀
4
+ colorFrom: green
5
+ colorTo: gray
6
  sdk: static
7
+ app_file: index.html
8
  pinned: false
9
+ license: cc-by-4.0
10
+ short_description: Few-shot adaptation to a new visual environment, live
11
  ---
12
 
13
+ # Next Shift
14
+
15
+ Next Shift adapts a digit recogniser trained on clean scans to a **new visual environment** (grey, low-contrast, textured,
16
+ rotated, blurred captures) using only a handful of labeled target images. It first re-estimates BatchNorm statistics
17
+ on unlabeled target images (AdaBN), then fine-tunes the top MLP on the labels. The target environment is a **synthetic**
18
+ transform of real UCI digits.
19
+
20
+ **Model:** https://huggingface.co/thelamapi/Next-Shift — label a few target images; the top layers fine-tune in your browser and predictions update.
21
+
22
+ ## Results: target test accuracy (297 images, 5 seeds, mean ± std)
23
+
24
+ | Method | k=0 | k=1 | k=5 | k=10 (labels per class) |
25
+ |---|---:|---:|---:|---:|
26
+ | Next Shift (AdaBN + fine-tune top) | 0.394 ± 0.000 | 0.489 ± 0.033 | 0.588 ± 0.021 | 0.661 ± 0.010 |
27
+ | Ablation: fine-tune top, no AdaBN | 0.401 ± 0.000 | 0.459 ± 0.042 | 0.589 ± 0.014 | 0.673 ± 0.009 |
28
+ | Frozen source features + linear head | 0.401 ± 0.000 | 0.358 ± 0.031 | 0.558 ± 0.019 | 0.613 ± 0.022 |
29
+ | Random init, same labels and steps | 0.099 ± 0.023 | 0.211 ± 0.022 | 0.467 ± 0.026 | 0.636 ± 0.050 |
30
+
31
+ Source-only accuracy on the source validation split: 0.973; on the target test split: 0.401.
32
+ For each k and seed, every method sees the same labeled images. Steps and learning rates were chosen per method on a
33
+ separate 100-image target development split (seeds 100–102), never on the test split.
34
+
35
+ ![Accuracy vs labels](preview.png)
36
+
37
+ ## Method
38
+ * Encoder: 2 conv blocks (16, 32 channels, BatchNorm, ReLU, max-pool) → 512 features → fc 64 → 10 classes; pre-trained on 900 source images.
39
+ * Next Shift: AdaBN on 350 unlabeled target pool images, then 50 full-batch GD steps (lr 0.05) on the top MLP.
40
+ * Frozen representation: linear head on fixed 64-d source features (200 steps, lr 0.1).
41
+ * Random init: the whole network from scratch with Adam (100 steps, lr 0.001).
42
+ * Ablation without AdaBN: same fine-tuning on source-normalised features.
43
+ * Browser: conv stacks run through `nlrt.js` and the fine-tuning is a line-by-line port; Node parity max abs difference 5.412337245047638e-16.
44
+
45
+ ## Usage
46
+ ```python
47
+ from inference import load, adapt, predict
48
+ spec = load() # NumPy only
49
+ top = adapt(spec, target_images, labels) # images: (N,16,16) in [0,1]
50
+ probs = predict(spec, new_images, top)
51
+ ```
52
+ `source_model.safetensors` holds the PyTorch source weights (architecture in the notebook).
53
+
54
+ ## Limitations
55
+ * The domain shift is synthetic and the images are tiny; real shifts (sensors, lighting, populations) can be harder.
56
+ * AdaBN needs a batch of unlabeled target images; the released `conv_target` was estimated on the target pool.
57
+ * 96 of 297 target test images are still wrong for Next Shift at k=10 (seed 0); see `evaluation.json`.
58
+ * Trained on NVIDIA L4; profile full.
app.js ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Next Shift Space: label a few target-environment digits; the model adapts in the browser. */
2
+ const { $, h, fmt, grayImage, lineChart, legend } = NLUI;
3
+ let ctx, curves, labels = {}, active = null, result = null;
4
+ const COL = { next_shift: "#0b6e4f", frozen: "#2f5fb3", random_init: "#8a8f98", you: "#c0392b" };
5
+
6
+ function poolGrid() {
7
+ const box = $("pool"); box.innerHTML = "";
8
+ ctx.d.pool_images.forEach((im, i) => {
9
+ const c = h("canvas", { title: "pool item " + i });
10
+ grayImage(c, im, 16, 16, 3, { lo: 0, hi: 255 });
11
+ const tag = labels[i] != null ? String(labels[i]) : "";
12
+ const cell = h("button", { class: "chip" + (labels[i] != null ? " on" : ""), style: "padding:2px;border-radius:6px;position:relative", "data-pool": i,
13
+ onclick: () => { active = i; picker(); } }, c, h("span", { style: "position:absolute;right:3px;bottom:0;font-weight:700" }, tag));
14
+ if (active === i) cell.style.outline = "2px solid #c0392b";
15
+ box.append(cell);
16
+ });
17
+ }
18
+
19
+ function picker() {
20
+ const p = $("picker"); p.innerHTML = "";
21
+ if (active == null) { p.textContent = "Tap an image above, then choose its digit."; return; }
22
+ const c = h("canvas"); grayImage(c, ctx.d.pool_images[active], 16, 16, 5, { lo: 0, hi: 255 });
23
+ const row = h("div", { class: "chips" });
24
+ for (let k = 0; k < 10; k++) row.append(h("button", { class: "chip", "data-label": k, onclick: () => setLabel(active, k) }, String(k)));
25
+ row.append(h("button", { class: "chip", onclick: () => setLabel(active, null) }, "clear"));
26
+ const hint = $("hint").checked ? h("span", { class: "small muted" }, ` dataset label: ${ctx.d.pool_y[active]}`) : null;
27
+ p.append(h("div", { class: "row" }, c, hint), row);
28
+ }
29
+
30
+ function setLabel(i, k) {
31
+ if (k == null) delete labels[i]; else labels[i] = k;
32
+ active = null; poolGrid(); picker(); update();
33
+ }
34
+
35
+ function update() {
36
+ const ids = Object.keys(labels).map(Number).sort((a, b) => a - b);
37
+ $("status").textContent = `Adapting with ${ids.length} labeled target images…`;
38
+ const t0 = performance.now();
39
+ result = Core.adapt(ctx, ids, ids.map(i => labels[i]));
40
+ $("k").textContent = ids.length;
41
+ $("acc_ft").textContent = fmt(100 * result.accFt, 1) + "%";
42
+ $("acc_lin").textContent = fmt(100 * result.accLin, 1) + "%";
43
+ $("status").textContent = `Updated in ${fmt((performance.now() - t0) / 1000, 2)} s on ${ctx.d.test_y.length} held-out target images.`;
44
+ const box = $("preds"); box.innerHTML = "";
45
+ for (let i = 0; i < 36; i++) {
46
+ const c = h("canvas"); grayImage(c, ctx.d.test_images[i], 16, 16, 3, { lo: 0, hi: 255 });
47
+ const ok = result.predFt[i] === ctx.d.test_y[i];
48
+ box.append(h("div", { style: "text-align:center" }, c, h("div", { class: "small", style: `color:${ok ? "#0b6e4f" : "#c0392b"};font-weight:600` }, String(result.predFt[i]))));
49
+ }
50
+ chart(ids.length);
51
+ }
52
+
53
+ function chart(k) {
54
+ const names = { next_shift: "Next Shift (AdaBN + fine-tune top)", frozen: "Frozen source features + linear head", random_init: "Random init, same data and steps" };
55
+ const series = Object.keys(names).map(m => ({ name: names[m] + " (benchmark)", color: COL[m], dash: m === "next_shift" ? [] : [5, 4],
56
+ points: curves.k.map((kk, j) => [kk * 10, curves.mean[m][j]]) }));
57
+ series.push({ name: "your labels (Next Shift, this page)", color: COL.you, points: [[k, result.accFt]], width: 6 });
58
+ const r = lineChart($("chart"), series, { xlabel: "labeled target images (benchmark uses k per class × 10)", ymin: 0, ymax: 1 });
59
+ const g = r.ctx; g.fillStyle = COL.you; g.beginPath(); g.arc(r.X(k), r.Y(result.accFt), 5, 0, 7); g.fill();
60
+ legend($("cl"), series);
61
+ }
62
+
63
+ function autoLabel() {
64
+ // one dataset-labeled example per digit, first unlabeled ones in pool order
65
+ for (let c = 0; c < 10; c++) {
66
+ const i = ctx.d.pool_y.findIndex((y, j) => y === c && labels[j] == null);
67
+ if (i >= 0) labels[i] = c;
68
+ }
69
+ poolGrid(); update();
70
+ }
71
+
72
+ window.__liveTest = function () {
73
+ labels = {}; update();
74
+ const zero = result.accFt;
75
+ for (let c = 0; c < 10; c++) {
76
+ const i = ctx.d.pool_y.findIndex(y => y === c);
77
+ document.querySelector(`button[data-pool="${i}"]`).click();
78
+ document.querySelector(`#picker button[data-label="${c}"]`).click();
79
+ }
80
+ return { ok: Object.keys(labels).length === 10 && $("k").textContent === "10" && result.predFt.length === ctx.d.test_y.length,
81
+ acc_zero_shot: zero, acc_after_10_labels: result.accFt, frozen_after_10: result.accLin };
82
+ };
83
+
84
+ NLUI.boot(Core, async c => {
85
+ ctx = c;
86
+ curves = await (await fetch("curves.json")).json();
87
+ $("auto").addEventListener("click", autoLabel);
88
+ $("reset").addEventListener("click", () => { labels = {}; active = null; poolGrid(); picker(); update(); });
89
+ $("hint").addEventListener("change", picker);
90
+ window.addEventListener("resize", () => result && chart(Object.keys(labels).length));
91
+ poolGrid(); picker(); update();
92
+ });
core.js ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Next Shift compute core: port of shift_core.py (conv features via nlrt, top-MLP fine-tuning, linear probe). */
2
+ (function (root, factory) {
3
+ if (typeof module === "object" && module.exports) module.exports = factory();
4
+ else root.Core = factory();
5
+ })(typeof self !== "undefined" ? self : this, function () {
6
+ "use strict";
7
+ const ASSETS = ["model.json", "data.json"];
8
+
9
+ function img(u8) { return NLRT.T([1, 16, 16], Float64Array.from(u8, v => v / 255)); }
10
+
11
+ function init(assets) {
12
+ const m = NLRT.decode(assets["model.json"]), d = assets["data.json"];
13
+ const t = m.tensors, top = { w1: t.w1.data, b1: t.b1.data, w2: t.w2.data, b2: t.b2.data };
14
+ const ctx = { m, d, top, hyper: m.meta.hyper, cache: {} };
15
+ return ctx;
16
+ }
17
+
18
+ // Features are computed lazily and cached: target-BN conv features (512) and frozen source features (64).
19
+ function feats(ctx, split, i) {
20
+ const key = split + i;
21
+ if (ctx.cache[key]) return ctx.cache[key];
22
+ const x = img(ctx.d[split + "_images"][i]);
23
+ const ft = NLRT.forward(ctx.m.nets.conv_target, x).data;
24
+ const fs = NLRT.forward(ctx.m.nets.conv_source, x).data;
25
+ const z = new Float64Array(64);
26
+ for (let j = 0; j < 64; j++) { let s = ctx.top.b1[j]; for (let k = 0; k < 512; k++) s += ctx.top.w1[j * 512 + k] * fs[k]; z[j] = s > 0 ? s : 0; }
27
+ return (ctx.cache[key] = { ft, z });
28
+ }
29
+
30
+ function mlp(p, F, h, out) {
31
+ for (let j = 0; j < 64; j++) { let s = p.b1[j]; for (let k = 0; k < 512; k++) s += p.w1[j * 512 + k] * F[k]; h[j] = s > 0 ? s : 0; }
32
+ for (let c = 0; c < 10; c++) { let s = p.b2[c]; for (let j = 0; j < 64; j++) s += p.w2[c * 64 + j] * h[j]; out[c] = s; }
33
+ }
34
+
35
+ function softmaxInPlace(z) { let m = -Infinity; for (const v of z) if (v > m) m = v; let s = 0; for (let i = 0; i < z.length; i++) { z[i] = Math.exp(z[i] - m); s += z[i]; } for (let i = 0; i < z.length; i++) z[i] /= s; }
36
+
37
+ function finetuneTop(p0, Fs, y, steps, lr) {
38
+ const p = { w1: Float64Array.from(p0.w1), b1: Float64Array.from(p0.b1), w2: Float64Array.from(p0.w2), b2: Float64Array.from(p0.b2) };
39
+ const n = y.length, H = Fs.map(() => new Float64Array(64)), P = Fs.map(() => new Float64Array(10));
40
+ const gw1 = new Float64Array(64 * 512), gb1 = new Float64Array(64), gw2 = new Float64Array(640), gb2 = new Float64Array(10), dh = new Float64Array(64);
41
+ for (let s = 0; s < steps; s++) {
42
+ gw1.fill(0); gb1.fill(0); gw2.fill(0); gb2.fill(0);
43
+ for (let i = 0; i < n; i++) { mlp(p, Fs[i], H[i], P[i]); softmaxInPlace(P[i]); P[i][y[i]] -= 1; for (let c = 0; c < 10; c++) P[i][c] /= n; }
44
+ for (let i = 0; i < n; i++) {
45
+ for (let c = 0; c < 10; c++) { const pv = P[i][c]; gb2[c] += pv; for (let j = 0; j < 64; j++) gw2[c * 64 + j] += pv * H[i][j]; }
46
+ for (let j = 0; j < 64; j++) { let a = 0; for (let c = 0; c < 10; c++) a += P[i][c] * p.w2[c * 64 + j]; dh[j] = H[i][j] > 0 ? a : 0; }
47
+ for (let j = 0; j < 64; j++) { const d = dh[j]; gb1[j] += d; if (d === 0) continue; const F = Fs[i]; for (let k = 0; k < 512; k++) gw1[j * 512 + k] += d * F[k]; }
48
+ }
49
+ for (let i = 0; i < gw1.length; i++) p.w1[i] -= lr * gw1[i];
50
+ for (let i = 0; i < 64; i++) p.b1[i] -= lr * gb1[i];
51
+ for (let i = 0; i < 640; i++) p.w2[i] -= lr * gw2[i];
52
+ for (let i = 0; i < 10; i++) p.b2[i] -= lr * gb2[i];
53
+ }
54
+ return p;
55
+ }
56
+
57
+ function trainLinear(Zs, y, steps, lr) {
58
+ const n = y.length, d = 64, W = new Float64Array(10 * d), b = new Float64Array(10), P = Zs.map(() => new Float64Array(10));
59
+ for (let s = 0; s < steps; s++) {
60
+ for (let i = 0; i < n; i++) { for (let c = 0; c < 10; c++) { let a = b[c]; for (let k = 0; k < d; k++) a += W[c * d + k] * Zs[i][k]; P[i][c] = a; } softmaxInPlace(P[i]); P[i][y[i]] -= 1; for (let c = 0; c < 10; c++) P[i][c] /= n; }
61
+ const gW = new Float64Array(10 * d), gb = new Float64Array(10);
62
+ for (let i = 0; i < n; i++) for (let c = 0; c < 10; c++) { gb[c] += P[i][c]; for (let k = 0; k < d; k++) gW[c * d + k] += P[i][c] * Zs[i][k]; }
63
+ for (let i = 0; i < W.length; i++) W[i] -= lr * gW[i];
64
+ for (let c = 0; c < 10; c++) b[c] -= lr * gb[c];
65
+ }
66
+ return { W, b };
67
+ }
68
+
69
+ function linLogits(m, z) { const out = new Float64Array(10); for (let c = 0; c < 10; c++) { let a = m.b[c]; for (let k = 0; k < 64; k++) a += m.W[c * 64 + k] * z[k]; out[c] = a; } return out; }
70
+
71
+ // Adapt on labeled pool items, then predict the whole target test set.
72
+ function adapt(ctx, labeled, labels) {
73
+ const hy = ctx.hyper, nTest = ctx.d.test_images.length;
74
+ const h = new Float64Array(64), out = new Float64Array(10);
75
+ let ft = ctx.top, lin = null;
76
+ if (labeled.length) {
77
+ ft = finetuneTop(ctx.top, labeled.map(i => feats(ctx, "pool", i).ft), labels, hy.ft_steps, hy.ft_lr);
78
+ lin = trainLinear(labeled.map(i => feats(ctx, "pool", i).z), labels, hy.lin_steps, hy.lin_lr);
79
+ }
80
+ const predFt = [], predLin = [], prob0 = [];
81
+ for (let i = 0; i < nTest; i++) {
82
+ const f = feats(ctx, "test", i);
83
+ mlp(ft, f.ft, h, out); predFt.push(NLRT.argmax(out));
84
+ if (i === 0) { const z = Float64Array.from(out); softmaxInPlace(z); prob0.push(...z); }
85
+ if (lin) predLin.push(NLRT.argmax(linLogits(lin, f.z)));
86
+ else { mlp(ctx.top, NLRT.forward(ctx.m.nets.conv_source, img(ctx.d.test_images[i])).data, h, out); predLin.push(NLRT.argmax(out)); }
87
+ }
88
+ const y = ctx.d.test_y, acc = p => p.reduce((a, v, i) => a + (v === y[i] ? 1 : 0), 0) / y.length;
89
+ return { predFt, predLin, accFt: acc(predFt), accLin: acc(predLin), prob0 };
90
+ }
91
+
92
+ function runCase(ctx, input) {
93
+ const r = adapt(ctx, input.labeled, input.labels);
94
+ return { acc_next_shift: r.accFt, acc_frozen: r.accLin, prob_test0: r.prob0 };
95
+ }
96
+
97
+ return { ASSETS, init, adapt, runCase, feats };
98
+ });
curves.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"k": [0, 1, 5, 10], "mean": {"next_shift": [0.3939393939393939, 0.48888888888888893, 0.5878787878787878, 0.6612794612794612], "frozen": [0.4006734006734007, 0.35757575757575755, 0.5575757575757576, 0.6127946127946128], "random_init": [0.09898989898989899, 0.21077441077441078, 0.4673400673400673, 0.6363636363636365]}}
data.json ADDED
The diff for this file is too large to render. See raw diff
 
evaluation.json ADDED
@@ -0,0 +1,532 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "project": "Next Shift",
3
+ "profile": "full",
4
+ "seed": 73,
5
+ "device": {
6
+ "python": "3.13.16",
7
+ "platform": "Linux-6.6.159+-x86_64-with-glibc2.39",
8
+ "cpu_count": 12,
9
+ "numpy": "2.4.6",
10
+ "torch": "2.14.1+cu130",
11
+ "cuda": true,
12
+ "gpu": "NVIDIA L4",
13
+ "colab": true
14
+ },
15
+ "data": {
16
+ "name": "Optical Recognition of Handwritten Digits",
17
+ "source": "UCI id 80 via sklearn.datasets.load_digits",
18
+ "license": "CC BY 4.0",
19
+ "sklearn_version": "1.6.1",
20
+ "target_environment": "synthetic: 16x16 upsampling, rotation ±25°, scale 0.8-1.05, grey background with weaker ink, illumination gradient, stripe texture, blur, noise",
21
+ "split": {
22
+ "source_train": 900,
23
+ "source_val": 150,
24
+ "target_pool": 350,
25
+ "target_dev": 100,
26
+ "target_test": 297,
27
+ "method": "stratified, random_state=73; source and target use disjoint digit samples"
28
+ }
29
+ },
30
+ "pretraining": {
31
+ "epochs": 40,
32
+ "best_epoch": 5,
33
+ "source_val_acc": 0.9733333333333334,
34
+ "seconds": 1.5,
35
+ "target_test_acc_source_only": 0.4006734006734007
36
+ },
37
+ "hyper": {
38
+ "ft_steps": 50,
39
+ "ft_lr": 0.05,
40
+ "lin_steps": 200,
41
+ "lin_lr": 0.1,
42
+ "rand_steps": 100,
43
+ "rand_lr": 0.001,
44
+ "noada_steps": 50,
45
+ "noada_lr": 0.05
46
+ },
47
+ "hyper_selection": {
48
+ "split": "target development (100 images)",
49
+ "dev_seeds": [
50
+ 100,
51
+ 101,
52
+ 102
53
+ ],
54
+ "next_shift": {
55
+ "50,0.05": 0.7033333333333335,
56
+ "50,0.1": 0.6933333333333334,
57
+ "50,0.3": 0.10000000000000002,
58
+ "100,0.05": 0.6933333333333334,
59
+ "100,0.1": 0.68,
60
+ "100,0.3": 0.10333333333333333,
61
+ "200,0.05": 0.68,
62
+ "200,0.1": 0.6733333333333333,
63
+ "200,0.3": 0.10666666666666667
64
+ },
65
+ "no_adabn": {
66
+ "50,0.05": 0.68,
67
+ "50,0.1": 0.6733333333333333,
68
+ "50,0.3": 0.12666666666666668,
69
+ "100,0.05": 0.6766666666666666,
70
+ "100,0.1": 0.6633333333333332,
71
+ "100,0.3": 0.10666666666666667,
72
+ "200,0.05": 0.6666666666666666,
73
+ "200,0.1": 0.6699999999999999,
74
+ "200,0.3": 0.11333333333333334
75
+ },
76
+ "frozen": {
77
+ "200,0.01": 0.6033333333333334,
78
+ "200,0.03": 0.6233333333333334,
79
+ "200,0.1": 0.63,
80
+ "500,0.01": 0.63,
81
+ "500,0.03": 0.6166666666666667,
82
+ "500,0.1": 0.6166666666666667
83
+ },
84
+ "random_init": {
85
+ "100,0.001": 0.5333333333333333,
86
+ "100,0.003": 0.5233333333333333,
87
+ "300,0.001": 0.52,
88
+ "300,0.003": 0.5166666666666667
89
+ }
90
+ },
91
+ "budget": "for each k all methods see the same labeled images; steps chosen per method on the development split",
92
+ "results": {
93
+ "next_shift": {
94
+ "k_per_class": [
95
+ 0,
96
+ 1,
97
+ 5,
98
+ 10
99
+ ],
100
+ "mean": [
101
+ 0.3939393939393939,
102
+ 0.48888888888888893,
103
+ 0.5878787878787878,
104
+ 0.6612794612794612
105
+ ],
106
+ "std": [
107
+ 0.0,
108
+ 0.03267207938237902,
109
+ 0.020929656926491656,
110
+ 0.009896928253669397
111
+ ],
112
+ "per_seed": [
113
+ [
114
+ 0.3939393939393939,
115
+ 0.46464646464646464,
116
+ 0.5656565656565656,
117
+ 0.6767676767676768
118
+ ],
119
+ [
120
+ 0.3939393939393939,
121
+ 0.468013468013468,
122
+ 0.569023569023569,
123
+ 0.6464646464646465
124
+ ],
125
+ [
126
+ 0.3939393939393939,
127
+ 0.4882154882154882,
128
+ 0.5791245791245792,
129
+ 0.6565656565656566
130
+ ],
131
+ [
132
+ 0.3939393939393939,
133
+ 0.5521885521885522,
134
+ 0.6161616161616161,
135
+ 0.6632996632996633
136
+ ],
137
+ [
138
+ 0.3939393939393939,
139
+ 0.4713804713804714,
140
+ 0.6094276094276094,
141
+ 0.6632996632996633
142
+ ]
143
+ ]
144
+ },
145
+ "no_adabn": {
146
+ "k_per_class": [
147
+ 0,
148
+ 1,
149
+ 5,
150
+ 10
151
+ ],
152
+ "mean": [
153
+ 0.4006734006734007,
154
+ 0.45925925925925926,
155
+ 0.5892255892255892,
156
+ 0.6734006734006733
157
+ ],
158
+ "std": [
159
+ 0.0,
160
+ 0.04219380524665465,
161
+ 0.01412537169252731,
162
+ 0.00903461809090825
163
+ ],
164
+ "per_seed": [
165
+ [
166
+ 0.4006734006734007,
167
+ 0.4377104377104377,
168
+ 0.5656565656565656,
169
+ 0.6632996632996633
170
+ ],
171
+ [
172
+ 0.4006734006734007,
173
+ 0.41414141414141414,
174
+ 0.5824915824915825,
175
+ 0.6632996632996633
176
+ ],
177
+ [
178
+ 0.4006734006734007,
179
+ 0.45454545454545453,
180
+ 0.5993265993265994,
181
+ 0.6835016835016835
182
+ ],
183
+ [
184
+ 0.4006734006734007,
185
+ 0.5387205387205387,
186
+ 0.5925925925925926,
187
+ 0.6734006734006734
188
+ ],
189
+ [
190
+ 0.4006734006734007,
191
+ 0.4511784511784512,
192
+ 0.6060606060606061,
193
+ 0.6835016835016835
194
+ ]
195
+ ]
196
+ },
197
+ "frozen": {
198
+ "k_per_class": [
199
+ 0,
200
+ 1,
201
+ 5,
202
+ 10
203
+ ],
204
+ "mean": [
205
+ 0.4006734006734007,
206
+ 0.35757575757575755,
207
+ 0.5575757575757576,
208
+ 0.6127946127946128
209
+ ],
210
+ "std": [
211
+ 0.0,
212
+ 0.031180705462061222,
213
+ 0.019470481205119114,
214
+ 0.02202751142598803
215
+ ],
216
+ "per_seed": [
217
+ [
218
+ 0.4006734006734007,
219
+ 0.30976430976430974,
220
+ 0.5521885521885522,
221
+ 0.6060606060606061
222
+ ],
223
+ [
224
+ 0.4006734006734007,
225
+ 0.3400673400673401,
226
+ 0.5420875420875421,
227
+ 0.6127946127946128
228
+ ],
229
+ [
230
+ 0.4006734006734007,
231
+ 0.3838383838383838,
232
+ 0.5488215488215489,
233
+ 0.5757575757575758
234
+ ],
235
+ [
236
+ 0.4006734006734007,
237
+ 0.39730639730639733,
238
+ 0.5488215488215489,
239
+ 0.6397306397306397
240
+ ],
241
+ [
242
+ 0.4006734006734007,
243
+ 0.3569023569023569,
244
+ 0.5959595959595959,
245
+ 0.6296296296296297
246
+ ]
247
+ ]
248
+ },
249
+ "random_init": {
250
+ "k_per_class": [
251
+ 0,
252
+ 1,
253
+ 5,
254
+ 10
255
+ ],
256
+ "mean": [
257
+ 0.09898989898989899,
258
+ 0.21077441077441078,
259
+ 0.4673400673400673,
260
+ 0.6363636363636365
261
+ ],
262
+ "std": [
263
+ 0.02259658146203706,
264
+ 0.021986299955979137,
265
+ 0.02558922558922559,
266
+ 0.04989530923324634
267
+ ],
268
+ "per_seed": [
269
+ [
270
+ 0.05723905723905724,
271
+ 0.1919191919191919,
272
+ 0.4478114478114478,
273
+ 0.5791245791245792
274
+ ],
275
+ [
276
+ 0.10101010101010101,
277
+ 0.24242424242424243,
278
+ 0.494949494949495,
279
+ 0.696969696969697
280
+ ],
281
+ [
282
+ 0.10101010101010101,
283
+ 0.21212121212121213,
284
+ 0.4276094276094276,
285
+ 0.5858585858585859
286
+ ],
287
+ [
288
+ 0.1111111111111111,
289
+ 0.2255892255892256,
290
+ 0.4882154882154882,
291
+ 0.6902356902356902
292
+ ],
293
+ [
294
+ 0.12457912457912458,
295
+ 0.18181818181818182,
296
+ 0.4781144781144781,
297
+ 0.6296296296296297
298
+ ]
299
+ ]
300
+ }
301
+ },
302
+ "seeds": [
303
+ 0,
304
+ 1,
305
+ 2,
306
+ 3,
307
+ 4
308
+ ],
309
+ "failure_examples": {
310
+ "setting": "Next Shift, k=10, seed 0",
311
+ "n_wrong": 96,
312
+ "first": [
313
+ {
314
+ "target_test_index": 1,
315
+ "digit_row": 278,
316
+ "true": 2,
317
+ "predicted": 0
318
+ },
319
+ {
320
+ "target_test_index": 7,
321
+ "digit_row": 1789,
322
+ "true": 8,
323
+ "predicted": 4
324
+ },
325
+ {
326
+ "target_test_index": 8,
327
+ "digit_row": 1571,
328
+ "true": 8,
329
+ "predicted": 1
330
+ },
331
+ {
332
+ "target_test_index": 10,
333
+ "digit_row": 1200,
334
+ "true": 7,
335
+ "predicted": 4
336
+ },
337
+ {
338
+ "target_test_index": 13,
339
+ "digit_row": 550,
340
+ "true": 6,
341
+ "predicted": 5
342
+ },
343
+ {
344
+ "target_test_index": 17,
345
+ "digit_row": 730,
346
+ "true": 2,
347
+ "predicted": 8
348
+ },
349
+ {
350
+ "target_test_index": 18,
351
+ "digit_row": 349,
352
+ "true": 1,
353
+ "predicted": 8
354
+ },
355
+ {
356
+ "target_test_index": 23,
357
+ "digit_row": 1227,
358
+ "true": 1,
359
+ "predicted": 8
360
+ },
361
+ {
362
+ "target_test_index": 24,
363
+ "digit_row": 1790,
364
+ "true": 8,
365
+ "predicted": 2
366
+ },
367
+ {
368
+ "target_test_index": 25,
369
+ "digit_row": 103,
370
+ "true": 3,
371
+ "predicted": 2
372
+ },
373
+ {
374
+ "target_test_index": 28,
375
+ "digit_row": 1199,
376
+ "true": 1,
377
+ "predicted": 8
378
+ },
379
+ {
380
+ "target_test_index": 30,
381
+ "digit_row": 306,
382
+ "true": 2,
383
+ "predicted": 3
384
+ },
385
+ {
386
+ "target_test_index": 32,
387
+ "digit_row": 738,
388
+ "true": 9,
389
+ "predicted": 1
390
+ },
391
+ {
392
+ "target_test_index": 34,
393
+ "digit_row": 141,
394
+ "true": 1,
395
+ "predicted": 7
396
+ },
397
+ {
398
+ "target_test_index": 35,
399
+ "digit_row": 563,
400
+ "true": 1,
401
+ "predicted": 8
402
+ },
403
+ {
404
+ "target_test_index": 38,
405
+ "digit_row": 718,
406
+ "true": 0,
407
+ "predicted": 4
408
+ },
409
+ {
410
+ "target_test_index": 49,
411
+ "digit_row": 1088,
412
+ "true": 7,
413
+ "predicted": 8
414
+ },
415
+ {
416
+ "target_test_index": 50,
417
+ "digit_row": 281,
418
+ "true": 5,
419
+ "predicted": 9
420
+ },
421
+ {
422
+ "target_test_index": 51,
423
+ "digit_row": 447,
424
+ "true": 7,
425
+ "predicted": 9
426
+ },
427
+ {
428
+ "target_test_index": 54,
429
+ "digit_row": 1120,
430
+ "true": 1,
431
+ "predicted": 8
432
+ },
433
+ {
434
+ "target_test_index": 55,
435
+ "digit_row": 579,
436
+ "true": 3,
437
+ "predicted": 9
438
+ },
439
+ {
440
+ "target_test_index": 62,
441
+ "digit_row": 409,
442
+ "true": 3,
443
+ "predicted": 2
444
+ },
445
+ {
446
+ "target_test_index": 63,
447
+ "digit_row": 808,
448
+ "true": 5,
449
+ "predicted": 3
450
+ },
451
+ {
452
+ "target_test_index": 68,
453
+ "digit_row": 765,
454
+ "true": 9,
455
+ "predicted": 7
456
+ },
457
+ {
458
+ "target_test_index": 77,
459
+ "digit_row": 1097,
460
+ "true": 1,
461
+ "predicted": 8
462
+ },
463
+ {
464
+ "target_test_index": 79,
465
+ "digit_row": 225,
466
+ "true": 4,
467
+ "predicted": 6
468
+ },
469
+ {
470
+ "target_test_index": 82,
471
+ "digit_row": 930,
472
+ "true": 5,
473
+ "predicted": 9
474
+ },
475
+ {
476
+ "target_test_index": 93,
477
+ "digit_row": 121,
478
+ "true": 4,
479
+ "predicted": 1
480
+ },
481
+ {
482
+ "target_test_index": 95,
483
+ "digit_row": 1247,
484
+ "true": 1,
485
+ "predicted": 7
486
+ },
487
+ {
488
+ "target_test_index": 97,
489
+ "digit_row": 104,
490
+ "true": 6,
491
+ "predicted": 1
492
+ }
493
+ ]
494
+ },
495
+ "tests": {
496
+ "reload": {
497
+ "returncode": 0,
498
+ "seconds": 0.25,
499
+ "process": "fresh python -E subprocess, cwd = temporary copy of the model folder",
500
+ "passed": true,
501
+ "max_abs_diff": 0.0,
502
+ "n_images": 20,
503
+ "n_labels": 10
504
+ },
505
+ "node_parity": {
506
+ "cases": 3,
507
+ "failed": 0,
508
+ "maxDiff": 5.412337245047638e-16,
509
+ "results": [
510
+ {
511
+ "name": "zero",
512
+ "maxDiff": 3.3306690738754696e-16
513
+ },
514
+ {
515
+ "name": "one-each",
516
+ "maxDiff": 5.412337245047638e-16
517
+ },
518
+ {
519
+ "name": "twenty",
520
+ "maxDiff": 4.440892098500626e-16
521
+ }
522
+ ],
523
+ "ms": 2798,
524
+ "runtime": "node v22.23.4",
525
+ "status": "passed"
526
+ },
527
+ "browser": {
528
+ "status": "skipped",
529
+ "reason": "playwright not installed"
530
+ }
531
+ }
532
+ }
index.html CHANGED
@@ -1,19 +1,48 @@
1
  <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  </html>
 
1
  <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <link rel="icon" href="data:,">
7
+ <title>Next Shift</title>
8
+ <link rel="stylesheet" href="style.css">
9
+ </head>
10
+ <body>
11
+ <main>
12
+ <header>
13
+ <h1>Next Shift</h1>
14
+ <p>A digit recogniser trained on clean scans meets a new environment: grey, low-contrast, textured, rotated and blurred images. Label a few target images: the model fine-tunes its top layers on your labels in the browser, and its predictions on held-out target images update.</p>
15
+ <div class="badges"><span class="badge warn">Target environment is synthetic</span><span class="badge">Live in-browser adaptation</span><span class="badge">UCI digits (CC BY 4.0)</span><a class="badge" href="https://huggingface.co/thelamapi/Next-Shift">Model repo</a></div>
16
+ </header>
17
+ <div class="grid">
18
+ <section class="card">
19
+ <h2>Label target images</h2>
20
+ <div id="pool" class="chips" style="gap:4px"></div>
21
+ <div id="picker" style="margin-top:8px"></div>
22
+ <div class="row" style="margin-top:8px"><button id="auto" class="ghost">Add one dataset label per digit</button><button id="reset" class="ghost">Clear labels</button></div>
23
+ <label><input type="checkbox" id="hint"> Show dataset label as a hint</label>
24
+ </section>
25
+ <section class="card">
26
+ <h2>Held-out target accuracy</h2>
27
+ <div class="kpi">
28
+ <div><b id="k">0</b><span>your labels</span></div>
29
+ <div><b id="acc_ft">–</b><span>Next Shift</span></div>
30
+ <div><b id="acc_lin">–</b><span>frozen source features + linear head</span></div>
31
+ </div>
32
+ <p class="status" id="status">Loading…</p>
33
+ <canvas id="chart" style="margin-top:6px"></canvas><div id="cl"></div>
34
+ </section>
35
+ </div>
36
+ <section class="card" style="margin-top:12px">
37
+ <h2>Next Shift predictions on held-out target images</h2>
38
+ <div id="preds" style="display:grid;grid-template-columns:repeat(auto-fill,minmax(52px,1fr));gap:6px"></div>
39
+ </section>
40
+ <section class="card" style="margin-top:12px"><h2>Recorded benchmark</h2><img src="preview.png" alt="Accuracy vs labeled examples" style="width:100%;border-radius:8px"></section>
41
+ <footer>Source and target images come from the same UCI digits collection; the target "environment" is a synthetic visual transform.</footer>
42
+ </main>
43
+ <script src="nlrt.js"></script>
44
+ <script src="nlui.js"></script>
45
+ <script src="core.js"></script>
46
+ <script src="app.js"></script>
47
+ </body>
48
  </html>
model.json ADDED
The diff for this file is too large to render. See raw diff
 
nlrt.js ADDED
@@ -0,0 +1,219 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* nlrt.js: browser and Node twin of nlrt.py. Batch size 1, float64 math. */
2
+ (function (root, factory) {
3
+ if (typeof module === "object" && module.exports) module.exports = factory();
4
+ else root.NLRT = factory();
5
+ })(typeof self !== "undefined" ? self : this, function () {
6
+ "use strict";
7
+
8
+ function b64d(s) {
9
+ let bin;
10
+ if (typeof atob === "function") bin = atob(s);
11
+ else bin = Buffer.from(s, "base64").toString("binary");
12
+ const bytes = new Uint8Array(bin.length);
13
+ for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
14
+ return new Float32Array(bytes.buffer);
15
+ }
16
+
17
+ function decode(spec) {
18
+ for (const name in spec.nets || {}) {
19
+ for (const L of spec.nets[name]) {
20
+ if (typeof L.w === "string") L.w = b64d(L.w);
21
+ if (typeof L.b === "string") L.b = b64d(L.b);
22
+ }
23
+ }
24
+ for (const name in spec.tensors || {}) {
25
+ const t = spec.tensors[name];
26
+ if (typeof t.data === "string") t.data = b64d(t.data);
27
+ }
28
+ return spec;
29
+ }
30
+
31
+ async function loadModel(url) {
32
+ const r = await fetch(url);
33
+ if (!r.ok) throw new Error("cannot load " + url);
34
+ return decode(await r.json());
35
+ }
36
+
37
+ function pair(v) { return Array.isArray(v) ? v : [v, v]; }
38
+
39
+ // Tensor: {shape: [D]} or {shape: [C, H, W]} or {shape: [C, L]}, data: Float64Array
40
+ function T(shape, data) {
41
+ const n = shape.reduce((a, b) => a * b, 1);
42
+ return { shape: shape.slice(), data: data || new Float64Array(n) };
43
+ }
44
+
45
+ function as3(x) {
46
+ if (x.shape.length === 2) return { t: T([x.shape[0], 1, x.shape[1]], x.data), sq: true };
47
+ return { t: x, sq: false };
48
+ }
49
+
50
+ function conv2d(x0, L) {
51
+ const { t: x, sq } = as3(x0);
52
+ const [C, H, W] = x.shape;
53
+ const [O, I, KH, KW] = L.w_shape;
54
+ const [sh, sw] = pair(L.stride), [ph, pw] = pair(L.pad);
55
+ const OH = Math.floor((H + 2 * ph - KH) / sh) + 1;
56
+ const OW = Math.floor((W + 2 * pw - KW) / sw) + 1;
57
+ const out = new Float64Array(O * OH * OW);
58
+ const w = L.w, b = L.b, xd = x.data;
59
+ for (let o = 0; o < O; o++) {
60
+ for (let oy = 0; oy < OH; oy++) {
61
+ for (let ox = 0; ox < OW; ox++) {
62
+ let s = b[o];
63
+ for (let c = 0; c < I; c++) {
64
+ for (let ky = 0; ky < KH; ky++) {
65
+ const iy = oy * sh + ky - ph;
66
+ if (iy < 0 || iy >= H) continue;
67
+ const wb = ((o * I + c) * KH + ky) * KW, xb = (c * H + iy) * W;
68
+ for (let kx = 0; kx < KW; kx++) {
69
+ const ix = ox * sw + kx - pw;
70
+ if (ix < 0 || ix >= W) continue;
71
+ s += w[wb + kx] * xd[xb + ix];
72
+ }
73
+ }
74
+ }
75
+ out[(o * OH + oy) * OW + ox] = s;
76
+ }
77
+ }
78
+ }
79
+ return sq ? T([O, OW], out) : T([O, OH, OW], out);
80
+ }
81
+
82
+ function maxpool(x0, L) {
83
+ const { t: x, sq } = as3(x0);
84
+ const [C, H, W] = x.shape, [kh, kw] = pair(L.k);
85
+ const OH = Math.floor(H / kh), OW = Math.floor(W / kw);
86
+ const out = new Float64Array(C * OH * OW);
87
+ for (let c = 0; c < C; c++)
88
+ for (let oy = 0; oy < OH; oy++)
89
+ for (let ox = 0; ox < OW; ox++) {
90
+ let m = -Infinity;
91
+ for (let i = 0; i < kh; i++)
92
+ for (let j = 0; j < kw; j++) {
93
+ const v = x.data[(c * H + oy * kh + i) * W + ox * kw + j];
94
+ if (v > m) m = v;
95
+ }
96
+ out[(c * OH + oy) * OW + ox] = m;
97
+ }
98
+ return sq ? T([C, OW], out) : T([C, OH, OW], out);
99
+ }
100
+
101
+ function linear(x, L) {
102
+ const [O, I] = L.w_shape;
103
+ const out = new Float64Array(O);
104
+ for (let o = 0; o < O; o++) {
105
+ let s = L.b[o];
106
+ const base = o * I;
107
+ for (let i = 0; i < I; i++) s += L.w[base + i] * x.data[i];
108
+ out[o] = s;
109
+ }
110
+ return T([O], out);
111
+ }
112
+
113
+ function map(x, f) {
114
+ const d = new Float64Array(x.data.length);
115
+ for (let i = 0; i < d.length; i++) d[i] = f(x.data[i]);
116
+ return T(x.shape, d);
117
+ }
118
+
119
+ function forward(layers, x) {
120
+ if (!(x.data instanceof Float64Array)) x = T(x.shape, Float64Array.from(x.data));
121
+ for (const L of layers) {
122
+ switch (L.type) {
123
+ case "linear": x = linear(x, L); break;
124
+ case "conv2d": x = conv2d(x, L); break;
125
+ case "relu": x = map(x, v => (v > 0 ? v : 0)); break;
126
+ case "tanh": x = map(x, Math.tanh); break;
127
+ case "sigmoid": x = map(x, v => 1 / (1 + Math.exp(-v))); break;
128
+ case "maxpool": x = maxpool(x, L); break;
129
+ case "gap": {
130
+ const C = x.shape[0], n = x.data.length / C, d = new Float64Array(C);
131
+ for (let c = 0; c < C; c++) { let s = 0; for (let i = 0; i < n; i++) s += x.data[c * n + i]; d[c] = s / n; }
132
+ x = T([C], d); break;
133
+ }
134
+ case "flatten": x = T([x.data.length], x.data); break;
135
+ case "reshape": x = T(L.shape, x.data); break;
136
+ case "upsample": {
137
+ const s = L.s;
138
+ if (x.shape.length === 3) {
139
+ const [C, H, W] = x.shape, d = new Float64Array(C * H * s * W * s);
140
+ for (let c = 0; c < C; c++)
141
+ for (let y = 0; y < H * s; y++)
142
+ for (let xx = 0; xx < W * s; xx++)
143
+ d[(c * H * s + y) * W * s + xx] = x.data[(c * H + Math.floor(y / s)) * W + Math.floor(xx / s)];
144
+ x = T([C, H * s, W * s], d);
145
+ } else {
146
+ const [C, Ln] = x.shape, d = new Float64Array(C * Ln * s);
147
+ for (let c = 0; c < C; c++) for (let i = 0; i < Ln * s; i++) d[c * Ln * s + i] = x.data[c * Ln + Math.floor(i / s)];
148
+ x = T([C, Ln * s], d);
149
+ }
150
+ break;
151
+ }
152
+ case "l2norm": {
153
+ let s = 0; for (const v of x.data) s += v * v;
154
+ const n = Math.max(Math.sqrt(s), 1e-12);
155
+ x = map(x, v => v / n); break;
156
+ }
157
+ default: throw new Error("unknown layer " + L.type);
158
+ }
159
+ }
160
+ return x;
161
+ }
162
+
163
+ function mulberry32(seed) {
164
+ let a = seed >>> 0;
165
+ const random = function () {
166
+ a = (a + 0x6D2B79F5) >>> 0;
167
+ let t = a;
168
+ t = Math.imul(t ^ (t >>> 15), t | 1) >>> 0;
169
+ t = (t ^ ((t + (Math.imul(t ^ (t >>> 7), t | 61) >>> 0)) >>> 0)) >>> 0;
170
+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
171
+ };
172
+ return {
173
+ random,
174
+ uniform: (lo, hi) => lo + (hi - lo) * random(),
175
+ randint: n => Math.floor(random() * n),
176
+ normal: () => { const u = Math.max(random(), 1e-12), v = random(); return Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v); },
177
+ };
178
+ }
179
+
180
+ function softmax(arr) {
181
+ let m = -Infinity; for (const v of arr) if (v > m) m = v;
182
+ const e = Array.from(arr, v => Math.exp(v - m)); const s = e.reduce((a, b) => a + b, 0);
183
+ return e.map(v => v / s);
184
+ }
185
+
186
+ function argmax(arr) { let k = 0; for (let i = 1; i < arr.length; i++) if (arr[i] > arr[k]) k = i; return k; }
187
+
188
+ // Deep comparison used by parity tests: numbers within tol, everything else exact.
189
+ function compare(got, want, tol, path, acc) {
190
+ acc = acc || { maxDiff: 0, mismatches: [] };
191
+ path = path || "$";
192
+ if (typeof want === "number") {
193
+ const d = Math.abs(Number(got) - want);
194
+ if (!(d <= tol * Math.max(1, Math.abs(want)))) acc.mismatches.push(path + ": got " + got + " want " + want);
195
+ if (isFinite(d)) acc.maxDiff = Math.max(acc.maxDiff, d);
196
+ } else if (Array.isArray(want)) {
197
+ if (!got || got.length !== want.length) acc.mismatches.push(path + ": length " + (got && got.length) + " vs " + want.length);
198
+ else for (let i = 0; i < want.length; i++) compare(got[i], want[i], tol, path + "[" + i + "]", acc);
199
+ } else if (want && typeof want === "object") {
200
+ for (const k in want) compare(got ? got[k] : undefined, want[k], tol, path + "." + k, acc);
201
+ } else if (got !== want) acc.mismatches.push(path + ": got " + got + " want " + want);
202
+ return acc;
203
+ }
204
+
205
+ function runParity(core, ctx, parity) {
206
+ const results = [];
207
+ let maxDiff = 0, failed = 0;
208
+ for (const c of parity.cases) {
209
+ const out = core.runCase(ctx, c.input);
210
+ const r = compare(out, c.expected, c.tol != null ? c.tol : parity.tol);
211
+ maxDiff = Math.max(maxDiff, r.maxDiff);
212
+ if (r.mismatches.length) failed++;
213
+ results.push({ name: c.name, maxDiff: r.maxDiff, mismatches: r.mismatches.slice(0, 5) });
214
+ }
215
+ return { cases: parity.cases.length, failed, maxDiff, results };
216
+ }
217
+
218
+ return { b64d, decode, loadModel, T, forward, mulberry32, softmax, argmax, compare, runParity };
219
+ });
nlui.js ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* nlui.js: small DOM and canvas helpers shared by Next Lab Spaces. */
2
+ (function (root) {
3
+ "use strict";
4
+ const $ = id => document.getElementById(id);
5
+ function h(tag, attrs, ...kids) {
6
+ const e = document.createElement(tag);
7
+ for (const k in attrs || {}) {
8
+ if (k === "class") e.className = attrs[k];
9
+ else if (k.startsWith("on")) e.addEventListener(k.slice(2), attrs[k]);
10
+ else if (k === "html") e.innerHTML = attrs[k];
11
+ else e.setAttribute(k, attrs[k]);
12
+ }
13
+ for (const c of kids.flat()) if (c != null) e.append(c.nodeType ? c : document.createTextNode(String(c)));
14
+ return e;
15
+ }
16
+ const css = name => getComputedStyle(document.documentElement).getPropertyValue(name).trim();
17
+ const fmt = (v, d = 3) => (v == null || !isFinite(v) ? "–" : Number(v).toFixed(d));
18
+
19
+ function grayImage(canvas, data, w, h, scale, opts) {
20
+ opts = opts || {};
21
+ canvas.width = w * scale; canvas.height = h * scale;
22
+ const ctx = canvas.getContext("2d");
23
+ const img = ctx.createImageData(w, h);
24
+ let lo = opts.lo != null ? opts.lo : 0, hi = opts.hi != null ? opts.hi : 1;
25
+ for (let i = 0; i < w * h; i++) {
26
+ const v = Math.max(0, Math.min(255, Math.round(255 * (data[i] - lo) / (hi - lo || 1))));
27
+ img.data[4 * i] = img.data[4 * i + 1] = img.data[4 * i + 2] = v; img.data[4 * i + 3] = 255;
28
+ }
29
+ const tmp = document.createElement("canvas"); tmp.width = w; tmp.height = h;
30
+ tmp.getContext("2d").putImageData(img, 0, 0);
31
+ ctx.imageSmoothingEnabled = false;
32
+ ctx.drawImage(tmp, 0, 0, w * scale, h * scale);
33
+ return ctx;
34
+ }
35
+
36
+ function rgbImage(canvas, data, w, h, scale) { // data: CHW in [0,1]
37
+ canvas.width = w * scale; canvas.height = h * scale;
38
+ const ctx = canvas.getContext("2d");
39
+ const img = ctx.createImageData(w, h), n = w * h;
40
+ for (let i = 0; i < n; i++) {
41
+ for (let c = 0; c < 3; c++) img.data[4 * i + c] = Math.max(0, Math.min(255, Math.round(255 * data[c * n + i])));
42
+ img.data[4 * i + 3] = 255;
43
+ }
44
+ const tmp = document.createElement("canvas"); tmp.width = w; tmp.height = h;
45
+ tmp.getContext("2d").putImageData(img, 0, 0);
46
+ ctx.imageSmoothingEnabled = false;
47
+ ctx.drawImage(tmp, 0, 0, w * scale, h * scale);
48
+ return ctx;
49
+ }
50
+
51
+ // series: [{name, color, points: [[x,y],...], dash}]
52
+ function lineChart(canvas, series, opts) {
53
+ opts = opts || {};
54
+ const W = canvas.clientWidth || 600, H = opts.height || 240, dpr = window.devicePixelRatio || 1;
55
+ canvas.width = W * dpr; canvas.height = H * dpr; canvas.style.height = H + "px";
56
+ const ctx = canvas.getContext("2d"); ctx.scale(dpr, dpr);
57
+ const m = { l: 46, r: 10, t: 10, b: 34 };
58
+ let xs = [], ys = [];
59
+ for (const s of series) for (const [x, y] of s.points) { if (isFinite(y)) { xs.push(x); ys.push(y); } }
60
+ if (opts.hlines) for (const hl of opts.hlines) ys.push(hl.y);
61
+ let x0 = opts.xmin != null ? opts.xmin : Math.min(...xs), x1 = opts.xmax != null ? opts.xmax : Math.max(...xs);
62
+ let y0 = opts.ymin != null ? opts.ymin : Math.min(...ys), y1 = opts.ymax != null ? opts.ymax : Math.max(...ys);
63
+ if (y1 - y0 < 1e-9) { y0 -= 1; y1 += 1; }
64
+ if (x1 - x0 < 1e-9) x1 = x0 + 1;
65
+ const X = x => m.l + (x - x0) / (x1 - x0) * (W - m.l - m.r);
66
+ const Y = y => H - m.b - (y - y0) / (y1 - y0) * (H - m.t - m.b);
67
+ ctx.font = "11px system-ui"; ctx.fillStyle = css("--muted"); ctx.strokeStyle = css("--line"); ctx.lineWidth = 1;
68
+ for (let i = 0; i <= 4; i++) {
69
+ const y = y0 + (y1 - y0) * i / 4;
70
+ ctx.beginPath(); ctx.moveTo(m.l, Y(y)); ctx.lineTo(W - m.r, Y(y)); ctx.stroke();
71
+ ctx.fillText(Math.abs(y) >= 100 ? y.toFixed(0) : y.toFixed(2), 4, Y(y) + 4);
72
+ }
73
+ for (let i = 0; i <= 4; i++) {
74
+ const x = x0 + (x1 - x0) * i / 4;
75
+ ctx.fillText(Math.abs(x) >= 100 || Number.isInteger(x) ? x.toFixed(0) : x.toFixed(1), X(x) - 8, H - m.b + 14);
76
+ }
77
+ if (opts.xlabel) ctx.fillText(opts.xlabel, W / 2 - 30, H - 4);
78
+ if (opts.hlines) for (const hl of opts.hlines) {
79
+ ctx.setLineDash([4, 4]); ctx.strokeStyle = hl.color || css("--muted");
80
+ ctx.beginPath(); ctx.moveTo(m.l, Y(hl.y)); ctx.lineTo(W - m.r, Y(hl.y)); ctx.stroke(); ctx.setLineDash([]);
81
+ }
82
+ for (const s of series) {
83
+ ctx.strokeStyle = s.color; ctx.lineWidth = s.width || 2; ctx.setLineDash(s.dash || []);
84
+ ctx.beginPath(); let first = true;
85
+ for (const [x, y] of s.points) { if (!isFinite(y)) continue; if (first) ctx.moveTo(X(x), Y(y)); else ctx.lineTo(X(x), Y(y)); first = false; }
86
+ ctx.stroke(); ctx.setLineDash([]);
87
+ }
88
+ return { X, Y, ctx };
89
+ }
90
+
91
+ function legend(el, series) {
92
+ el.innerHTML = "";
93
+ for (const s of series) el.append(h("span", { class: "small", style: "margin-right:12px;white-space:nowrap" },
94
+ h("span", { style: `display:inline-block;width:14px;height:3px;background:${s.color};vertical-align:middle;margin-right:4px` }), s.name));
95
+ }
96
+
97
+ async function loadAssets(core) {
98
+ const assets = {};
99
+ for (const name of core.ASSETS) {
100
+ const r = await fetch(name);
101
+ if (!r.ok) throw new Error("cannot load " + name);
102
+ assets[name] = await r.json();
103
+ }
104
+ return assets;
105
+ }
106
+
107
+ // Standard bootstrap: load assets, init core, expose __parity for automated tests.
108
+ async function boot(core, start) {
109
+ try {
110
+ const assets = await loadAssets(core);
111
+ const ctx = core.init(assets);
112
+ const parity = await (await fetch("parity_cases.json")).json();
113
+ root.__parity = () => NLRT.runParity(core, ctx, parity);
114
+ await start(ctx, assets);
115
+ root.__ready = true;
116
+ } catch (e) {
117
+ console.error(e); root.__error = String(e && e.stack || e);
118
+ const s = document.getElementById("status"); if (s) s.textContent = "Failed to start: " + e;
119
+ }
120
+ }
121
+
122
+ root.NLUI = { $, h, css, fmt, grayImage, rgbImage, lineChart, legend, boot };
123
+ })(typeof self !== "undefined" ? self : this);
parity_cases.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"tol": 1e-06, "cases": [{"name": "zero", "input": {"labeled": [], "labels": []}, "expected": {"acc_next_shift": 0.3939393939393939, "acc_frozen": 0.4006734006734007, "prob_test0": [0.003781505214643981, 0.1582409769425243, 0.002016656441104725, 0.002293441894271149, 0.7015295107538247, 0.000994043394660472, 0.00404677155959714, 0.055751790995034925, 0.034146396516126026, 0.0371989062882126]}}, {"name": "one-each", "input": {"labeled": [22, 17, 4, 5, 2, 13, 20, 6, 0, 1], "labels": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]}, "expected": {"acc_next_shift": 0.5084175084175084, "acc_frozen": 0.2996632996632997, "prob_test0": [0.0182526747806825, 0.017605597219772808, 0.002037611893806424, 0.012354674889952674, 0.6179275119021989, 0.04110196117026401, 0.11629911262201108, 0.03747335490237712, 0.09269873872207833, 0.04424876189685623]}}, {"name": "twenty", "input": {"labeled": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19], "labels": [8, 9, 4, 8, 2, 3, 7, 3, 7, 3, 9, 2, 2, 5, 7, 9, 5, 1, 9, 8]}, "expected": {"acc_next_shift": 0.43097643097643096, "acc_frozen": 0.265993265993266, "prob_test0": [0.0014665526214078888, 0.009367635513964604, 0.0033721671916394596, 0.010361087334767139, 0.5185873662129094, 0.04508483145414255, 0.0026583457336206476, 0.1288656607641664, 0.14257980584051796, 0.1376565473328639]}}]}
preview.png ADDED
style.css CHANGED
@@ -1,28 +1,55 @@
1
- body {
2
- padding: 2rem;
3
- font-family: -apple-system, BlinkMacSystemFont, "Arial", sans-serif;
 
4
  }
5
-
6
- h1 {
7
- font-size: 16px;
8
- margin-top: 0;
9
- }
10
-
11
- p {
12
- color: rgb(107, 114, 128);
13
- font-size: 15px;
14
- margin-bottom: 10px;
15
- margin-top: 5px;
16
- }
17
-
18
- .card {
19
- max-width: 620px;
20
- margin: 0 auto;
21
- padding: 16px;
22
- border: 1px solid lightgray;
23
- border-radius: 16px;
24
- }
25
-
26
- .card p:last-child {
27
- margin-bottom: 0;
28
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ :root {
2
+ --bg: #f7f7f5; --panel: #ffffff; --ink: #1d1f22; --muted: #5f6670; --line: #dcdfe3;
3
+ --accent: #0b6e4f; --accent-soft: #e3f1ec; --warn: #a4471b; --warn-soft: #fbeee6;
4
+ --blue: #2f5fb3; --gold: #b07d12; --red: #c0392b;
5
  }
6
+ @media (prefers-color-scheme: dark) {
7
+ :root {
8
+ --bg: #15171a; --panel: #1e2125; --ink: #eceef0; --muted: #a5acb5; --line: #33383e;
9
+ --accent: #4cc39a; --accent-soft: #1b3229; --warn: #f0a070; --warn-soft: #3a2519;
10
+ --blue: #7ea6f0; --gold: #e0b44c; --red: #f07a6c;
11
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  }
13
+ * { box-sizing: border-box; }
14
+ html, body { margin: 0; padding: 0; background: var(--bg); color: var(--ink); }
15
+ body { font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
16
+ main { max-width: 1040px; margin: 0 auto; padding: 16px; }
17
+ header h1 { font-size: 1.6rem; margin: 4px 0 2px; letter-spacing: -0.01em; }
18
+ header p { margin: 0 0 8px; color: var(--muted); max-width: 70ch; }
19
+ .badges { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0 12px; }
20
+ .badge { font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); border: 1px solid var(--line); }
21
+ .badge.warn { background: var(--warn-soft); color: var(--warn); }
22
+ .grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 12px; }
23
+ .card { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; min-width: 0; }
24
+ .card h2 { font-size: 1.02rem; margin: 0 0 8px; }
25
+ .card h3 { font-size: .95rem; margin: 10px 0 6px; }
26
+ label { display: block; font-size: 13px; color: var(--muted); margin: 8px 0 3px; }
27
+ input[type=text], textarea, select { width: 100%; font: inherit; padding: 7px 9px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); }
28
+ textarea { min-height: 110px; resize: vertical; }
29
+ input[type=range] { width: 100%; }
30
+ button { font: inherit; padding: 7px 12px; border-radius: 8px; border: 1px solid var(--accent); background: var(--accent); color: #fff; cursor: pointer; }
31
+ button.ghost { background: transparent; color: var(--accent); }
32
+ button:disabled { opacity: .5; cursor: default; }
33
+ .row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
34
+ canvas { max-width: 100%; height: auto; display: block; border-radius: 6px; }
35
+ .card > canvas { width: 100%; }
36
+ .muted { color: var(--muted); }
37
+ .small { font-size: 13px; }
38
+ .status { font-size: 13px; color: var(--muted); min-height: 1.4em; }
39
+ table { border-collapse: collapse; width: 100%; font-size: 13px; }
40
+ th, td { text-align: left; padding: 4px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
41
+ th { color: var(--muted); font-weight: 600; }
42
+ td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
43
+ .scroll { overflow-x: auto; }
44
+ mark { background: #ffe58a; color: #1d1f22; padding: 0 2px; border-radius: 3px; }
45
+ mark.weak { background: #fff3c4; }
46
+ video { width: 100%; border-radius: 8px; }
47
+ footer { margin: 18px 0 8px; font-size: 13px; color: var(--muted); }
48
+ a { color: var(--blue); }
49
+ .chips { display: flex; flex-wrap: wrap; gap: 6px; }
50
+ .chip { border: 1px solid var(--line); border-radius: 999px; padding: 3px 10px; background: var(--bg); font-size: 13px; cursor: pointer; color: var(--ink); }
51
+ .chip.on { border-color: var(--accent); background: var(--accent-soft); }
52
+ .kpi { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 8px; }
53
+ .kpi div { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; }
54
+ .kpi b { display: block; font-size: 1.15rem; }
55
+ .kpi span { font-size: 12px; color: var(--muted); }