MCPcopy Create free account
hub / github.com/DFin/Neural-Network-Visualisation / loadMnistTestSamples

Function loadMnistTestSamples

assets/main.js:30–131  ·  view source on GitHub ↗
(manifestPath = MNIST_SAMPLE_MANIFEST_URL)

Source from the content-addressed store, hash-verified

28});
29
30async function loadMnistTestSamples(manifestPath = MNIST_SAMPLE_MANIFEST_URL) {
31 const manifestUrl = new URL(manifestPath, window.location.href);
32 const manifestResponse = await fetch(manifestUrl.toString());
33 if (!manifestResponse.ok) {
34 throw new Error(`Konnte MNIST-Manifest nicht laden (${manifestResponse.status}).`);
35 }
36 const manifest = await manifestResponse.json();
37 const rows = Number(manifest?.imageShape?.[0]) || 28;
38 const cols = Number(manifest?.imageShape?.[1]) || 28;
39 const numSamples = Number(manifest?.numSamples) || 0;
40 const sampleSize = rows * cols;
41 const imageFile = manifest?.image?.file;
42 const labelFile = manifest?.labels?.file;
43 if (!imageFile || !labelFile) {
44 throw new Error("Manifest enthält keine gültigen Dateipfade für Bilder oder Labels.");
45 }
46
47 const [imageBuffer, labelBuffer] = await Promise.all([
48 fetch(new URL(imageFile, manifestUrl).toString()).then((response) => {
49 if (!response.ok) {
50 throw new Error(`Konnte MNIST-Bilddaten nicht laden (${response.status}).`);
51 }
52 return response.arrayBuffer();
53 }),
54 fetch(new URL(labelFile, manifestUrl).toString()).then((response) => {
55 if (!response.ok) {
56 throw new Error(`Konnte MNIST-Labeldaten nicht laden (${response.status}).`);
57 }
58 return response.arrayBuffer();
59 }),
60 ]);
61
62 const imageBytes = new Uint8Array(imageBuffer);
63 const labelBytes = new Uint8Array(labelBuffer);
64 if (numSamples <= 0) {
65 if (sampleSize > 0) {
66 const inferredSamples = Math.floor(imageBytes.length / sampleSize);
67 if (inferredSamples <= 0) {
68 throw new Error("Aus den MNIST-Bilddaten konnte keine Stichprobengröße abgeleitet werden.");
69 }
70 if (labelBytes.length !== inferredSamples) {
71 throw new Error("Anzahl der Labels stimmt nicht mit den abgeleiteten Stichproben überein.");
72 }
73 } else {
74 throw new Error("Manifest enthält keine gültige Stichprobengröße.");
75 }
76 }
77
78 const totalSamples = numSamples > 0 ? numSamples : Math.floor(imageBytes.length / sampleSize);
79 if (imageBytes.length !== totalSamples * sampleSize) {
80 throw new Error("MNIST-Bilddatenlänge stimmt nicht mit der erwarteten Größe überein.");
81 }
82 if (labelBytes.length !== totalSamples) {
83 throw new Error("MNIST-Labeldatenlänge stimmt nicht mit der erwarteten Größe überein.");
84 }
85
86 const digitBuckets = Array.from({ length: 10 }, () => []);
87 for (let index = 0; index < totalSamples; index += 1) {

Callers 1

setupMnistSampleButtonsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected