Next-Ask / app.js
Lamapi's picture
Publish Next-Ask space
c3dba1e verified
Raw History Blame Contribute Delete
3.8 kB
/* Next Ask Space: the user labels the digits the selector asks for; the model retrains in the browser. */
const { $, h, fmt, grayImage, lineChart, legend } = NLUI;
const COLORS = { random: "#8a8f98", uncertainty: "#2f5fb3", hybrid: "#0b6e4f", you: "#c0392b" };
const B = 5;
let ctx, curves, S;
function reset() {
const strategy = $("strategy").value;
const labeled = curves.initial.slice();
S = { strategy, labeled, labels: labeled.map(i => ctx.poolY[i]), queue: [], answers: {}, history: [], round: 0 };
retrain();
}
function retrain() {
S.W = Core.fit(ctx, S.labeled, S.labels);
S.acc = Core.accuracy(ctx, S.W);
S.history.push([S.labeled.length, S.acc]);
S.queue = Core.select(ctx, S.strategy, S.W, S.labeled, B, 9000 + S.round++);
S.answers = {};
render();
}
function answer(idx, label) {
S.answers[idx] = label;
renderQueue();
if (S.queue.every(i => S.answers[i] != null)) {
for (const i of S.queue) { S.labeled.push(i); S.labels.push(S.answers[i]); }
retrain();
}
}
function renderQueue() {
const box = $("queue"); box.innerHTML = "";
for (const i of S.queue) {
const p = Core.proba(S.W, ctx.pool[i]);
const guess = NLRT.argmax(p), m = Core.margin(p);
const c = h("canvas", { width: 64, height: 64, "aria-label": "digit to label" });
grayImage(c, ctx.poolPixels[i], 8, 8, 8, { lo: 0, hi: 16 });
const btns = h("div", { class: "chips", style: "margin-top:6px" });
for (let k = 0; k < 10; k++) {
const b = h("button", { class: "chip" + (S.answers[i] === k ? " on" : ""), "data-item": i, "data-label": k,
onclick: () => answer(i, k) }, String(k));
btns.append(b);
}
const hint = $("hint").checked ? ` · dataset label ${ctx.poolY[i]}` : "";
box.append(h("div", { class: "card", style: "padding:8px" }, c,
h("div", { class: "small muted" }, `model guesses ${guess} (margin ${fmt(m, 2)})${hint}`), btns));
}
}
function render() {
$("acc").textContent = fmt(100 * S.acc, 1) + "%";
$("nlab").textContent = S.labeled.length;
$("round").textContent = S.history.length - 1;
renderQueue();
const series = ["random", "uncertainty", "hybrid"].map(k => ({
name: `${k} (simulated mean, ${curves.seeds} seeds)`, color: COLORS[k], dash: [5, 4], width: 1.5,
points: curves.mean[k].map((v, j) => [curves.n[j], v]) }));
series.push({ name: `your session (${S.strategy})`, color: COLORS.you, width: 2.5, points: S.history });
lineChart($("chart"), series, { xlabel: "labeled examples", ymin: 0.3, ymax: 1 });
legend($("legend"), series);
$("status").textContent = `Strategy: ${S.strategy}. Label the ${B} digits above; the classifier retrains in your browser after each batch.`;
}
function autoLabel() { for (const i of S.queue.slice()) if (S.answers[i] == null) answer(i, ctx.poolY[i]); }
window.__liveTest = function () {
$("strategy").value = "hybrid"; reset();
const acc0 = S.acc, n0 = S.labeled.length, seen = new Set(S.labeled);
let fresh = true;
for (let r = 0; r < 3; r++) {
for (const i of S.queue.slice()) {
if (seen.has(i)) fresh = false;
seen.add(i);
const btn = document.querySelector(`button[data-item="${i}"][data-label="${ctx.poolY[i]}"]`);
btn.click();
}
}
return { ok: S.labeled.length === n0 + 3 * B && S.history.length === 4 && fresh && S.queue.length === B,
acc_before: acc0, acc_after: S.acc, labeled: S.labeled.length };
};
NLUI.boot(Core, async (c) => {
ctx = c;
curves = await (await fetch("curves.json")).json();
$("strategy").addEventListener("change", reset);
$("reset").addEventListener("click", reset);
$("auto").addEventListener("click", autoLabel);
$("hint").addEventListener("change", renderQueue);
window.addEventListener("resize", () => S && render());
reset();
});