Next-Shift / core.js
Lamapi's picture
Publish Next-Shift space
28b4298 verified
Raw History Blame Contribute Delete
5.77 kB
/* Next Shift compute core: port of shift_core.py (conv features via nlrt, top-MLP fine-tuning, linear probe). */
(function (root, factory) {
if (typeof module === "object" && module.exports) module.exports = factory();
else root.Core = factory();
})(typeof self !== "undefined" ? self : this, function () {
"use strict";
const ASSETS = ["model.json", "data.json"];
function img(u8) { return NLRT.T([1, 16, 16], Float64Array.from(u8, v => v / 255)); }
function init(assets) {
const m = NLRT.decode(assets["model.json"]), d = assets["data.json"];
const t = m.tensors, top = { w1: t.w1.data, b1: t.b1.data, w2: t.w2.data, b2: t.b2.data };
const ctx = { m, d, top, hyper: m.meta.hyper, cache: {} };
return ctx;
}
// Features are computed lazily and cached: target-BN conv features (512) and frozen source features (64).
function feats(ctx, split, i) {
const key = split + i;
if (ctx.cache[key]) return ctx.cache[key];
const x = img(ctx.d[split + "_images"][i]);
const ft = NLRT.forward(ctx.m.nets.conv_target, x).data;
const fs = NLRT.forward(ctx.m.nets.conv_source, x).data;
const z = new Float64Array(64);
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; }
return (ctx.cache[key] = { ft, z });
}
function mlp(p, F, h, out) {
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; }
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; }
}
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; }
function finetuneTop(p0, Fs, y, steps, lr) {
const p = { w1: Float64Array.from(p0.w1), b1: Float64Array.from(p0.b1), w2: Float64Array.from(p0.w2), b2: Float64Array.from(p0.b2) };
const n = y.length, H = Fs.map(() => new Float64Array(64)), P = Fs.map(() => new Float64Array(10));
const gw1 = new Float64Array(64 * 512), gb1 = new Float64Array(64), gw2 = new Float64Array(640), gb2 = new Float64Array(10), dh = new Float64Array(64);
for (let s = 0; s < steps; s++) {
gw1.fill(0); gb1.fill(0); gw2.fill(0); gb2.fill(0);
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; }
for (let i = 0; i < n; i++) {
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]; }
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; }
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]; }
}
for (let i = 0; i < gw1.length; i++) p.w1[i] -= lr * gw1[i];
for (let i = 0; i < 64; i++) p.b1[i] -= lr * gb1[i];
for (let i = 0; i < 640; i++) p.w2[i] -= lr * gw2[i];
for (let i = 0; i < 10; i++) p.b2[i] -= lr * gb2[i];
}
return p;
}
function trainLinear(Zs, y, steps, lr) {
const n = y.length, d = 64, W = new Float64Array(10 * d), b = new Float64Array(10), P = Zs.map(() => new Float64Array(10));
for (let s = 0; s < steps; s++) {
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; }
const gW = new Float64Array(10 * d), gb = new Float64Array(10);
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]; }
for (let i = 0; i < W.length; i++) W[i] -= lr * gW[i];
for (let c = 0; c < 10; c++) b[c] -= lr * gb[c];
}
return { W, b };
}
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; }
// Adapt on labeled pool items, then predict the whole target test set.
function adapt(ctx, labeled, labels) {
const hy = ctx.hyper, nTest = ctx.d.test_images.length;
const h = new Float64Array(64), out = new Float64Array(10);
let ft = ctx.top, lin = null;
if (labeled.length) {
ft = finetuneTop(ctx.top, labeled.map(i => feats(ctx, "pool", i).ft), labels, hy.ft_steps, hy.ft_lr);
lin = trainLinear(labeled.map(i => feats(ctx, "pool", i).z), labels, hy.lin_steps, hy.lin_lr);
}
const predFt = [], predLin = [], prob0 = [];
for (let i = 0; i < nTest; i++) {
const f = feats(ctx, "test", i);
mlp(ft, f.ft, h, out); predFt.push(NLRT.argmax(out));
if (i === 0) { const z = Float64Array.from(out); softmaxInPlace(z); prob0.push(...z); }
if (lin) predLin.push(NLRT.argmax(linLogits(lin, f.z)));
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)); }
}
const y = ctx.d.test_y, acc = p => p.reduce((a, v, i) => a + (v === y[i] ? 1 : 0), 0) / y.length;
return { predFt, predLin, accFt: acc(predFt), accLin: acc(predLin), prob0 };
}
function runCase(ctx, input) {
const r = adapt(ctx, input.labeled, input.labels);
return { acc_next_shift: r.accFt, acc_frozen: r.accLin, prob_test0: r.prob0 };
}
return { ASSETS, init, adapt, runCase, feats };
});