Foundations: Tensors & Data
Loading and Preparing Data
needs to read the sensor archive without filling the computer's memory.

The raw sensor data comes in faster than we can process it in one pass. But this is just infinite scroll, right? Fetch everything once, hold the list, render a window. I pulled the full archive, raw capture blocks and all, into one array overnight.
RangeError: Array buffer allocation failed. Engineering rig, overnight: the loop dies 19,204 readings into a 28,800-reading archive and takes 's diagnostic queue down with it.
Each reading includes a 33-second raw capture: 256 samples per second across 64 channels, about two megabytes per reading. By the time the loop stops, the array holds about forty gigabytes. now owes engineering a four-hour queue rebuild.

Confirmed. I can read the data in small groups without keeping the whole archive in memory. I note that I said the same thing about my parsing rules.
With lazy loading, the pipeline reads each record when the next stage requests it. Limit how much data waits in memory, and dispose of after use. That keeps memory use from growing with the archive.
A is a small group of readings that the model processes together. .batch(32) makes groups of 32. The model processes one group, then requests the next. Unlike 's failed version, the pipeline does not keep the whole archive in memory.
needs a loader he can trust before opening the full archive again. Use traceable row IDs to check that it reads on demand, keeps with their , and leaves later readings out of training.
Reading samples on demand
Use the same 16-row practice archive in the activity and challenge. Each row has three features, the numbers a model would read, and one label, the target it would learn. This task tests the loader without training a model.
The supplied reader generates each row from its ID. The three feature slots stand in for amplitude, zero-crossing count, and phase. Their generated values have no physical units. The label alternates between 0 and 1 so you can check that each label stays with its row. It says nothing about an .
| Practice row ID | Feature values, in order | Label | Assigned split |
|---|---|---|---|
| 0 | [0, 0, 0] | 0 | Training |
| 1 | [100, 1, 0.1] | 1 | Training |
| 12 | [1200, 12, 1.2] | 0 | Validation |
These are loader test values. They do not come from 's sensor recording. The ship's 28,800-reading archive and its raw captures remain a separate source.
let readCount = 0;
function readSample(id) {
readCount++;
return { features: [id * 100, id, id / 10], label: id % 2 };
}Keep the archive read inside the generator loop. Each request then reads one row without loading the full archive into an array.
function* rowsById(ids) {
for (const id of ids) {
const row = readSample(id);
yield {
xs: tf.tensor(row.features), // one sample: [3]
ys: tf.tensor([row.label]), // one label: [1]
};
}
}Wrap each label in an array, [row.label]. Four samples then make feature
[4, 3] and label shape [4, 1]: four rows, one label per row.
The first read and the next pass
Creating a generator does no reading. Calling .next() runs its loop until
the next yield. Here, use practice IDs 0 and 1 to inspect the first request.
const probe = rowsById([0, 1]);
console.log(readCount); // 0
const first = probe.next();
console.log(readCount); // 1
if (!first.done) tf.dispose(first.value);
probe.return(undefined);
const restarted = rowsById([0, 1]);
const firstAgain = restarted.next(); // starts at row 0 again
console.log(readCount); // 2 total, one read in each iterator
if (!firstAgain.done) tf.dispose(firstAgain.value);
restarted.return(undefined);Give tf.data.generator a function that creates a fresh iterator for each
pass. Reusing an exhausted iterator leaves the next pass with no rows.
The challenge checks both on-demand reading and repeated passes.
Shuffling before batching
readCount = 0; // count the batching pass separately from the probe
const ids = Array.from({ length: 16 }, (_, i) => i);
const source = tf.data.generator(() => rowsById(ids));
const batches = source.shuffle(10).batch(4);
const batchShapes = [];
await batches.forEachAsync((group) => {
batchShapes.push(group.xs.shape);
console.log(group.xs.arraySync(), group.ys.arraySync());
});
console.log(batchShapes); // four [4, 3] shapes
console.log(readCount); // 16Shuffle before batching so individual rows can move between groups. A buffer of 10 holds ten rows to choose from. This mixes the ten rows currently in the buffer. A full shuffle would consider all 16 rows at once.
In the challenge, save each shape in batchShapes. Wait for forEachAsync()
to finish before reading that array. This example reads all 16 practice rows
to show batching. It does not train a model.
The shuffle buffer reads ahead, so the first batch can require more than four
archive reads. Check the generator alone to see one read for one request.
forEachAsync() disposes each batch's tensors after its callback. Save shapes
or copied values there if you need to inspect them afterward.
Training and validation periods
To test predictions on later readings, assign the earlier 12 row IDs to training and the last four to validation. Shuffle only inside the training set:
const trainIdx = Array.from(tf.util.createShuffledIndices(12));
const valIdx = Array.from({ length: 4 }, (_, i) => i + 12);A training pipeline would read only trainIdx. Validation would read only
valIdx. Neighboring records can share information, even for a model that
reads one row at a time. Keep shared captures together and leave a gap when
windows overlap. Fit on training rows only.
Challenge
Help test the loader on the 16-row archive stand-in. Read one row at a time, shuffle the rows, then form batches. Reserve the earlier rows for training and the later rows for validation. Inspect the yielded readings, batch shapes, and saved split to check what will reach training. No model is trained in this task.
The loader's results
After a run, inspect the tables from your code:
- Batches read by your loader should contain four rows, each with four readings and three features. A correct shape alone cannot confirm the values.
- Readings yielded by your generator shows the verifier's separate,
unshuffled pass. For row 1, check
[100, 1, 0.1]and label 1. Check all 16 IDs for missing rows, duplicates, changed feature order, or mismatched labels. - The saved time split should contain training IDs 0 through 11 and validation IDs 12 through 15. Training order can vary. Membership stays fixed.
The verifier reads the dataset again to test restart behavior. These checks produce actual results from your generator. The Concept activity uses a fixed illustration of the same practice rows, so its shuffled order can differ. If a check fails, use its message and the available tables to locate the issue.
Separate transfer practice: build-service data
The next activity applies the split decision to build-service records. It uses a different source so you can test when the same rule makes sense elsewhere.
