31
Datasets
Tensors
DataLoader — Batching & Shuffling
Demonstrates the DataLoader class for efficient batch iteration over datasets. This example uses deepbox/datasets, deepbox/ndarray and focuses on DataLoader; tensor.
Deepbox Modules Used
deepbox/datasetsdeepbox/ndarrayWhat You Will Learn
- Use deepbox/datasets for DataLoader.
- Use deepbox/ndarray for tensor.
- Demonstrates the DataLoader class for efficient batch iteration over datasets.
Source Files
index.ts
1/**2 * Example 31: DataLoader — Batching & Shuffling3 *4 * Demonstrates the DataLoader class for efficient batch iteration over datasets.5 * Essential for training loops where data must be batched and optionally shuffled.6 */78import { DataLoader } from "deepbox/datasets";9import { tensor } from "deepbox/ndarray";1011console.log("=== DataLoader: Batching & Shuffling ===\n");1213// ---------------------------------------------------------------------------14// Part 1: Basic batching15// ---------------------------------------------------------------------------16console.log("--- Part 1: Basic Batching ---");1718const X = tensor([19 [1, 2],20 [3, 4],21 [5, 6],22 [7, 8],23 [9, 10],24 [11, 12],25 [13, 14],26 [15, 16],27 [17, 18],28 [19, 20],29]);30const y = tensor([0, 1, 0, 1, 0, 1, 0, 1, 0, 1]);3132const loader = new DataLoader(X, y, { batchSize: 3 });33console.log(`Dataset size: ${X.shape[0]} samples`);34console.log(`Batch size: 3`);35console.log(`Expected batches: 4 (last batch has 1 sample)\n`);3637let batchIdx = 0;38for (const [xBatch, yBatch] of loader) {39 console.log(40 ` Batch ${batchIdx}: X shape [${xBatch.shape.join(", ")}], y shape [${yBatch.shape.join(", ")}]`41 );42 batchIdx++;43}4445// ---------------------------------------------------------------------------46// Part 2: Shuffling with deterministic seed47// ---------------------------------------------------------------------------48console.log("\n--- Part 2: Shuffled Iteration ---");4950const shuffledLoader = new DataLoader(X, y, {51 batchSize: 5,52 shuffle: true,53 seed: 42,54});55console.log("DataLoader(batchSize=5, shuffle=true, seed=42)");5657console.log("\nFirst iteration:");58for (const [xBatch, yBatch] of shuffledLoader) {59 console.log(` X first row: ${xBatch.toString().split("\n")[0]}, y: ${yBatch.toString()}`);60}6162console.log("\nSecond iteration (same seed = same order):");63for (const [xBatch, yBatch] of shuffledLoader) {64 console.log(` X first row: ${xBatch.toString().split("\n")[0]}, y: ${yBatch.toString()}`);65}6667// ---------------------------------------------------------------------------68// Part 3: dropLast — discard incomplete final batch69// ---------------------------------------------------------------------------70console.log("\n--- Part 3: Drop Last Batch ---");7172const dropLoader = new DataLoader(X, y, {73 batchSize: 3,74 dropLast: true,75});76console.log("DataLoader(batchSize=3, dropLast=true)");77console.log(`Dataset: ${X.shape[0]} samples, batch: 3, dropLast: true`);7879let dropBatchCount = 0;80for (const [xBatch] of dropLoader) {81 console.log(` Batch ${dropBatchCount}: shape [${xBatch.shape.join(", ")}]`);82 dropBatchCount++;83}84console.log(`Total batches: ${dropBatchCount} (incomplete last batch dropped)`);8586// ---------------------------------------------------------------------------87// Part 4: Inference without labels88// ---------------------------------------------------------------------------89console.log("\n--- Part 4: Inference Without Labels ---");9091const testLoader = new DataLoader(X, undefined, {92 batchSize: 4,93 shuffle: false,94});95console.log("DataLoader(X, undefined, { batchSize: 4 })");9697let testBatchIdx = 0;98for (const [xBatch] of testLoader) {99 console.log(` Batch ${testBatchIdx}: X shape [${xBatch.shape.join(", ")}]`);100 testBatchIdx++;101}102103console.log("\n=== DataLoader Complete ===");104Console Output
$ npx tsx 31-dataloader/index.ts
Console output showing batched iteration, shuffling, dropLast, and label-free inference