File size: 5,430 Bytes
94769b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* Next Federation Space: change client skew and dropout, rerun FedAvg / FedProx / local training in the browser. */
const { $, h, fmt, lineChart, legend, css } = NLUI;
const ALPHAS = [0.05, 0.1, 0.2, 0.3, 0.5, 1, 2, 5, 10];
const COL = { fedavg: "#2f5fb3", fedprox: "#0b6e4f", local: "#8a8f98" };
const NAME = { fedavg: "FedAvg", fedprox: "FedProx", local: "Local only" };
const CLS = ["#4e79a7", "#f28e2b", "#e15759", "#76b7b2", "#59a14f", "#edc948", "#b07aa1", "#ff9da7", "#9c755f", "#bab0ac"];
let ctx, busy = false, lastRes = null;

const cfg = () => ({ alpha: ALPHAS[Number($("alpha").value)], p_drop: Number($("drop").value), n_clients: Number($("clients").value),
  rounds: Number($("rounds").value), seed: Number($("seed").value) });

function labels() {
  const c = cfg();
  $("alpha_v").textContent = c.alpha; $("drop_v").textContent = c.p_drop.toFixed(2);
  $("clients_v").textContent = c.n_clients; $("rounds_v").textContent = c.rounds; $("seed_v").textContent = c.seed;
}

function drawDist(parts) {
  const cv = $("dist"), W = cv.clientWidth || 500, H = 150, dpr = window.devicePixelRatio || 1;
  cv.width = W * dpr; cv.height = H * dpr; cv.style.height = H + "px";
  const g = cv.getContext("2d"); g.scale(dpr, dpr);
  const n = parts.length, bw = (W - 30) / n, maxN = Math.max(...parts.map(p => p.train.length + p.test.length), 1);
  g.font = "10px system-ui"; g.fillStyle = css("--muted");
  parts.forEach((p, k) => {
    const counts = new Array(10).fill(0); for (const i of p.train.concat(p.test)) counts[ctx.y[i]]++;
    let y0 = H - 16;
    counts.forEach((cnt, c) => { const hh = (H - 26) * cnt / maxN; g.fillStyle = CLS[c]; g.fillRect(30 + k * bw + 2, y0 - hh, bw - 4, hh); y0 -= hh; });
    g.fillStyle = css("--muted"); g.fillText(String(k + 1), 30 + k * bw + bw / 2 - 3, H - 3);
  });
  g.fillText(maxN, 2, 12);
}

function drawClients(res) {
  const cv = $("bars"), W = cv.clientWidth || 500, H = 170, dpr = window.devicePixelRatio || 1;
  cv.width = W * dpr; cv.height = H * dpr; cv.style.height = H + "px";
  const g = cv.getContext("2d"); g.scale(dpr, dpr);
  const n = res.fedavg.client_acc.length, gw = (W - 30) / n, ms = ["fedavg", "fedprox", "local"];
  g.strokeStyle = css("--line"); g.fillStyle = css("--muted"); g.font = "10px system-ui";
  for (const v of [0, 0.5, 1]) { const y = H - 16 - (H - 26) * v; g.beginPath(); g.moveTo(28, y); g.lineTo(W, y); g.stroke(); g.fillText(v.toFixed(1), 2, y + 3); }
  for (let k = 0; k < n; k++) ms.forEach((m, j) => {
    const v = res[m].client_acc[k]; if (v < 0) return;
    g.fillStyle = COL[m]; const hh = (H - 26) * v; g.fillRect(30 + k * gw + j * (gw - 4) / 3 + 1, H - 16 - hh, (gw - 4) / 3 - 1, hh);
  });
  g.fillStyle = css("--muted"); for (let k = 0; k < n; k++) g.fillText(String(k + 1), 30 + k * gw + gw / 2 - 3, H - 3);
}

function summary(r) {
  const acc = r.client_acc.filter(v => v >= 0);
  return { global: r.curve[r.curve.length - 1], worst: Math.min(...acc), mean: acc.reduce((a, b) => a + b, 0) / acc.length, mb: r.comm_bytes / 1e6, uploads: r.uploads };
}

async function runAll() {
  if (busy) return lastRes; busy = true; $("run").disabled = true;
  const c = cfg(), parts = Core.makeClients(ctx, c.n_clients, c.alpha, c.seed);
  drawDist(parts);
  const res = {}, curves = {};
  for (const m of ["fedavg", "fedprox", "local"]) {
    $("status").textContent = `Training ${NAME[m]} on ${c.n_clients} simulated clients…`;
    await new Promise(r => setTimeout(r, 15));
    res[m] = Core.run(ctx.X, ctx.y, parts, ctx.test, m, { ...ctx.hyper, rounds: c.rounds, p_drop: c.p_drop, seed: c.seed });
    curves[m] = res[m].curve;
  }
  const series = ["fedavg", "fedprox", "local"].map(m => ({ name: NAME[m] + (m === "local" ? " (mean of client models)" : ""), color: COL[m], points: curves[m].map((v, i) => [i + 1, v]) }));
  lineChart($("curve"), series, { xlabel: "round", ymin: 0, ymax: 1 }); legend($("cl"), series);
  drawClients(res);
  const tb = $("tbl"); tb.innerHTML = "";
  tb.append(h("tr", {}, ["Method", "Global test acc", "Worst client", "Mean client", "Traffic (MB)", "Uploads"].map((x, i) => h("th", { class: i ? "num" : "" }, x))));
  for (const m of ["fedavg", "fedprox", "local"]) {
    const s = summary(res[m]);
    tb.append(h("tr", {}, h("td", {}, NAME[m]), h("td", { class: "num" }, fmt(s.global, 3)), h("td", { class: "num" }, fmt(s.worst, 3)),
      h("td", { class: "num" }, fmt(s.mean, 3)), h("td", { class: "num" }, fmt(s.mb, 2)), h("td", { class: "num" }, s.uploads)));
  }
  $("status").textContent = `Done: α=${c.alpha}, dropout ${c.p_drop}, ${c.n_clients} clients, ${c.rounds} rounds, seed ${c.seed}. Simulation in one browser tab; no real devices.`;
  busy = false; $("run").disabled = false; lastRes = res;
  return res;
}

window.__liveTest = async function () {
  $("alpha").value = "1"; $("drop").value = "0.4"; $("clients").value = "8"; $("rounds").value = "6"; $("seed").value = "9"; labels();
  const res = await runAll();
  return { ok: res.fedavg.curve.length === 6 && res.fedavg.client_acc.length === 8 && $("tbl").rows.length === 4 && res.fedavg.uploads < 6 * 8,
    fedavg: summary(res.fedavg), fedprox: summary(res.fedprox), local: summary(res.local) };
};

NLUI.boot(Core, async c => {
  ctx = c;
  for (const id of ["alpha", "drop", "clients", "rounds", "seed"]) $(id).addEventListener("input", labels);
  $("run").addEventListener("click", runAll);
  labels();
  await runAll();
});