Skip to content

Commit

Permalink
v0
Browse files Browse the repository at this point in the history
  • Loading branch information
xnought committed Sep 27, 2024
1 parent c886030 commit 31a5cc7
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 34 deletions.
52 changes: 29 additions & 23 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import { VQVAE } from "./vectorQuantizer";
import Features from "./lib/Features.svelte";
import Matrix from "./lib/Matrix.svelte";
import Quantized from "./lib/Quantized.svelte";
import { hovering } from "./stores";
const inputOutputCanvasSize = 300;
const images = [1, 2, 3, 4, 5, 7].map((d) => `images/${d}.png`);
Expand All @@ -21,7 +23,6 @@
let inputDigit = Array(784).fill(0);
let outputDigit = Array(784).fill(0);
let embeddings;
let hovering;
let idxs;
let features;
Expand Down Expand Up @@ -82,32 +83,37 @@
></MnistDigit>
</div>
<div>
<Features
width={200}
height={200}
square={125}
mouseenter={(i, j) => (hovering = [i, j])}
mouseleave={() => (hovering = undefined)}
/>
<Features width={200} height={200} square={125} />
<div class="mt-5">
{#if features && $hovering}
<Matrix
data={[features[$hovering[0] * 7 + $hovering[1]]]}
width={140}
height={200 / 16}
/>
{/if}
</div>
</div>
<div style="width: 140px; height: 20px;">
{#if features && hovering}
<Matrix
data={[features[hovering[0] * 7 + hovering[1]]]}
width={140}
height={200 / 16}
<div>
<div>
<Codebook
width={200}
height={140}
{embeddings}
hoveringColumn={idxs && $hovering
? idxs[$hovering[0]][$hovering[1]]
: undefined}
/>
{/if}
</div>
<div class="mt-20">
{#if idxs}
<Quantized width={150} height={150} {idxs} />
{/if}
</div>
</div>

<div>
<Codebook
width={200}
height={140}
{embeddings}
hoveringColumn={idxs && hovering
? idxs[hovering[0]][hovering[1]]
: undefined}
/>
<Features width={200} height={200} square={125} />
</div>
<div>
<MnistDigit
Expand Down
7 changes: 7 additions & 0 deletions src/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@
body {
margin: 0;
}

svg text {
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
4 changes: 2 additions & 2 deletions src/color.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as d3 from "d3";

export function generateDiscreteColors(n, interp) {
export function generateDiscreteColors(n, interp, shiftL = 0, shiftR = 0) {
let out = Array(n);
const s = d3
.scaleLinear()
.domain([0, n - 1])
.range([0, 1]);
.range([0 + shiftL, 1 - shiftR]);
for (let i = 0; i < n; i++) {
out[i] = interp(s(i));
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Codebook.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
{#each { length: columns } as _, i}
{#if hoveringColumn === i || true}
{@const x = w * i}
{@const o = hoveringColumn === i ? 1.0 : 0.2}
{@const o = hoveringColumn === i ? 1.0 : 0.4}
<rect
{x}
width={w}
Expand Down
19 changes: 13 additions & 6 deletions src/lib/Features.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script>
import { hovering } from "../stores";
import Prism from "./Prism.svelte";
export let width;
Expand All @@ -14,9 +15,6 @@
export let W = 7;
export let C = 16;
export let mouseenter = (i, j) => {};
export let mouseleave = () => {};
const miniSquare = square / H;
const squareFront = {
Expand All @@ -33,7 +31,14 @@
};
</script>

<svg {width} {height} {x} {y} style="overflow: visible;">
<svg
{width}
{height}
{x}
{y}
style="overflow: visible;"
on:mouseleave={() => ($hovering = undefined)}
>
<Prism
x1={squareFront.x}
y1={squareFront.y}
Expand Down Expand Up @@ -65,8 +70,10 @@
prismFill="rgb(0,0,0,0.1)"
squareFill="rgba(0,0,0,0.1)"
hoverInteraction
mouseenter={() => mouseenter(i, j)}
{mouseleave}
mouseenter={() => ($hovering = [i, j])}
hovering={$hovering
? $hovering[0] === i && $hovering[1] === j
: false}
/>
{#if j === 0}
<line {x1} {y1} {x2} {y2} stroke="rgb(0,0,0,0.1)" />
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Prism.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
width: square,
height: square,
};
let hovering = false;
export let hovering = false;
</script>

<!-- svelte-ignore a11y-no-static-element-interactions -->
Expand Down
55 changes: 55 additions & 0 deletions src/lib/Quantized.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<script>
import { color } from "../color";
import { codeNames, codeColors, hovering } from "../stores";
export let width;
export let height;
export let x = 0;
export let y = 0;
export let idxs;
export let rows = idxs.length;
export let columns = idxs[0].length;
$: w = width / columns;
$: h = height / columns;
</script>

<!-- svelte-ignore a11y-no-static-element-interactions -->
<svg
{width}
{height}
{x}
{y}
style="overflow: visible;"
on:mouseleave={() => ($hovering = undefined)}
>
<rect {width} {height} fill="none" stroke="black" />
{#each { length: rows } as _, i}
{#each { length: columns } as _, j}
{@const idx = idxs[i][j]}
{@const c = codeColors[idx]}
{@const x = i * w}
{@const y = j * h}
{@const isHovering =
$hovering && $hovering[0] === i && $hovering[1] === j}
<g
opacity={isHovering || $hovering === undefined ? 1.0 : 0.6}
on:mouseenter={() => ($hovering = [i, j])}
>
<rect {x} {y} width={w} height={h} fill={c} />G
{#if isHovering}
<text
x={x + w / 2}
y={y + h / 2 + 5}
text-anchor="middle"
style="font-family: menlo;"
fill={"black"}>{codeNames[idx]}</text
>
{/if}
</g>
{/each}
{/each}
</svg>

<style>
</style>
8 changes: 7 additions & 1 deletion src/stores.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@ import { writable } from "svelte/store";
import { generateDiscreteColors } from "./color";
import * as d3 from "d3";

export const hovering = writable();
export const codeNames = "0123456789ABCDEF";
export const codeColors = generateDiscreteColors(16, d3.interpolatePlasma);
export const codeColors = generateDiscreteColors(
16,
d3.interpolateCool,
0,
0.2
);

0 comments on commit 31a5cc7

Please sign in to comment.