SPB Git

Repository Public

neural-forward.tsx 7.0 KB 119 lines tsx

1import type { Locale } from "@/schema";2import { createRng } from "../core";3import { option, optionError, param, type SimConfig, type SimDef } from "../types";4import { fmt, px } from "../ui/format";5import { Readouts } from "../ui/Readouts";67export const ACTIVATIONS = ["tanh", "relu", "sigmoid"] as const;8export type Activation = (typeof ACTIVATIONS)[number];9export function activate(kind: Activation, z: number): number {10  return kind === "relu" ? Math.max(0, z) : kind === "sigmoid" ? 1 / (1 + Math.exp(-z)) : Math.tanh(z);11}1213export interface NeuralModel {14  activation: Activation;15  /** Units per layer: input, hidden…, output. */16  sizes: number[];17  /** weights[l][j][i]: from unit i of layer l to unit j of layer l + 1. */18  weights: number[][][];19  biases: number[][];20  /** Pre-activations z[l][j] (layer 0 = the inputs themselves). */21  z: number[][];22  /** Activations a[l][j]; hidden layers use `activation`, the output layer is linear. */23  a: number[][];24  maxAbsWeight: number;25  maxAbsActivation: number;26}2728export function computeNeural(p: Readonly<Record<string, number>>, seed: number, config: SimConfig): NeuralModel {29  const activation = option(config, "activation", ACTIVATIONS);30  const sizes = [Math.round(p.inputs), ...Array.from({ length: Math.round(p.layers) }, () => Math.round(p.width)), 2];31  const wr = createRng(seed, "nn-weights"), xr = createRng(seed, "nn-inputs");32  const weights: number[][][] = [], biases: number[][] = [];33  for (let l = 0; l + 1 < sizes.length; l++) {34    const sdw = p.scale / Math.sqrt(sizes[l]);35    weights.push(Array.from({ length: sizes[l + 1] }, () => Array.from({ length: sizes[l] }, () => wr.normal(0, sdw))));36    biases.push(Array.from({ length: sizes[l + 1] }, () => wr.normal(0, 0.1)));37  }38  const x = Array.from({ length: sizes[0] }, () => xr.uniform(-1, 1));39  const z: number[][] = [x], a: number[][] = [x];40  for (let l = 0; l < weights.length; l++) {41    const last = l === weights.length - 1;42    const zl = weights[l].map((row, j) => row.reduce((s, w, i) => s + w * a[l][i], biases[l][j]));43    z.push(zl); a.push(last ? zl : zl.map((v) => activate(activation, v)));44  }45  let mw = 0, ma = 0;46  for (const W of weights) for (const row of W) for (const w of row) mw = Math.max(mw, Math.abs(w));47  for (const layer of a) for (const v of layer) ma = Math.max(ma, Math.abs(v));48  return { activation, sizes, weights, biases, z, a, maxAbsWeight: mw || 1, maxAbsActivation: ma || 1 };49}5051const TXT = {52  fr: { input: "Entrée", hidden: "Cachée", output: "Sortie", aria: "Propagation avant dans un perceptron multicouche", act: "Activation des couches cachées", layer: "Couche calculée", out: "Sorties", pos: "valeur ou poids positif", neg: "valeur ou poids négatif", pending: "pas encore calculé" },53  en: { input: "Input", hidden: "Hidden", output: "Output", aria: "Forward pass through a multilayer perceptron", act: "Hidden activation", layer: "Layer computed", out: "Outputs", pos: "positive value or weight", neg: "negative value or weight", pending: "not computed yet" },54} as const;5556function NeuralView({ model: m, locale, step }: { model: NeuralModel; locale: Locale; step: number }) {57  const L = TXT[locale];58  const nL = m.sizes.length, rows = Math.max(...m.sizes);59  const W = 560, top = 34, rowH = 48, H = top + rows * rowH + 8, r = 18;60  const s = Math.min(Math.max(0, step), nL - 1);61  const X = (l: number) => 44 + ((W - 88) * l) / (nL - 1);62  const Y = (l: number, j: number) => top + (rows * rowH) / 2 + (j - (m.sizes[l] - 1) / 2) * rowH;63  const name = (l: number) => (l === 0 ? L.input : l === nL - 1 ? L.output : `${L.hidden} ${l}`);64  const mix = (v: number, scale: number, cap: number) => `color-mix(in srgb, var(--sh-t-${v >= 0 ? "blue" : "red"}) ${Math.round(cap * Math.min(1, Math.abs(v) / scale))}%, var(--sh-surface))`;65  return (66    <>67      <svg viewBox={`0 0 ${W} ${H}`} role="img" aria-label={L.aria} className="sim-svg sim-nn">68        {m.weights.map((Wl, l) => Wl.map((row, j) => row.map((w, i) => {69          const on = l + 1 <= s;70          return <line key={`${l}-${j}-${i}`} x1={px(X(l) + r)} y1={px(Y(l, i))} x2={px(X(l + 1) - r)} y2={px(Y(l + 1, j))} className="sim-nn-edge" data-on={on}71            stroke={`var(--sh-t-${w >= 0 ? "blue" : "red"})`} strokeWidth={px(0.6 + 2.4 * (Math.abs(w) / m.maxAbsWeight))} strokeOpacity={on ? px(0.15 + 0.75 * (Math.abs(w) / m.maxAbsWeight)) : 0.07} />;72        })))}73        {m.sizes.map((n, l) => (74          <g key={l} className="sim-nn-layer" data-on={l <= s} data-current={l === s}>75            <text x={px(X(l))} y={16} textAnchor="middle" className="sim-label">{name(l)}</text>76            {Array.from({ length: n }, (_, j) => (77              <g key={j}>78                <circle cx={px(X(l))} cy={px(Y(l, j))} r={r} className="sim-nn-node" style={l <= s ? { fill: mix(m.a[l][j], m.maxAbsActivation, 46) } : undefined} />79                <text x={px(X(l))} y={px(Y(l, j) + 4)} textAnchor="middle" className="sim-nn-value">{l <= s ? fmt(locale, m.a[l][j], 2) : "·"}</text>80              </g>81            ))}82          </g>83        ))}84      </svg>85      <ul className="sim-legend">86        <li><span className="sim-swatch" style={{ background: "var(--sh-t-blue)" }} />{L.pos}</li>87        <li><span className="sim-swatch" style={{ background: "var(--sh-t-red)" }} />{L.neg}</li>88        <li><span className="sim-swatch" style={{ background: "var(--sh-raised)", borderColor: "var(--sh-line)" }} />{L.pending}</li>89      </ul>90      <Readouts items={[91        { label: L.layer, value: name(s) },92        { label: L.act, value: m.activation },93        { label: L.out, value: s === nL - 1 ? m.a[nL - 1].map((v) => fmt(locale, v, 3)).join(" ; ") : "—" },94      ]} />95    </>96  );97}9899export const neuralForward: SimDef<NeuralModel> = {100  kind: "neural-forward",101  title: { fr: "Propagation avant dans un réseau de neurones", en: "Forward pass through a neural network" },102  description: {103    fr: "Chaque unité calcule $a = f\\big(\\sum w\\, a_{\\text{préc.}} + b\\big)$, couche après couche. Les poids (normaux, écart-type $\\text{échelle}/\\sqrt{\\text{entrées}}$) et les entrées (uniformes sur $[-1, 1]$) sont tirés de la graine ; la couche de sortie est linéaire. Le réseau n'est pas entraîné : il illustre le calcul, pas un apprentissage.",104    en: "Each unit computes $a = f\\big(\\sum w\\, a_{\\text{prev}} + b\\big)$, one layer after the other. Weights (normal, sd $\\text{scale}/\\sqrt{\\text{fan-in}}$) and inputs (uniform on $[-1, 1]$) are drawn from the seed; the output layer is linear. The network is untrained: it illustrates the computation, not learning.",105  },106  params: [107    param("layers", "Couches cachées", "Hidden layers", 2, 4, 1, 2),108    param("width", "Unités par couche", "Units per layer", 2, 6, 1, 4),109    param("inputs", "Entrées", "Inputs", 2, 4, 1, 3),110    param("scale", "Échelle des poids", "Weight scale", 0.2, 3, 0.1, 1.5),111  ],112  stochastic: true,113  configKeys: ["activation"],114  validateConfig: (c) => optionError(c, "activation", ACTIVATIONS),115  compute: computeNeural,116  steps: (m) => m.sizes.length,117  View: NeuralView,118};119
Files main
Menu

Theme