Foundations: Tensors & Data
Data Normalization
orders all sensor data normalized before the deep scan begins.


Before the deep scan runs, normalize every sensor feed. One scale, no exceptions.

The thermal channel reads in the thirties, signal strength lives under one. If we feed both in raw, the model's going to think temperature is the whole story.

If the starting weights are similar, the larger temperature numbers affect the result more. Scaling makes training easier. We still need to learn which sensor gives useful information.
Prepare temperature and signal strength for 's supplied preview model. Save the scaling statistics so later readings enter that model in the same units as the training rows.
Loading beat…
Scaling each feature
supplies a four-row practice : [20, 0.2], [22, 0.8],
[24, 0.4], and [26, 0.6]. Columns are temperature and signal strength.
The fixture gives no physical units or timestamps. It checks the scaling
routine separately from 's 64-channel recording.
Treat these as training rows when fitting the scaler. Their numerical scales differ, so calculate one set of statistics per column.
A subtracts the column mean and divides by its ,
its spread around that mean. tf.moments(batch, 0) reduces down the rows,
returning each column's mean and population . The square root of
variance is the standard deviation.
const { mean, variance } = tf.moments(batch, 0);
const std = variance.sqrt();
variance.dispose();
const normalized = tf.tidy(() => {
const divisor = tf.where(std.equal(0), tf.onesLike(std), std);
return batch.sub(mean).div(divisor);
});The challenge separates this calculation into fitStats(rows) and
scaleFeatures(rows, offset, scale). Return the saved mean and std from the
first function. Use the supplied offset and scale in the second.
For this batch, mean is [23, 0.5] and std is approximately [2.236, 0.224].
applies each column's statistics to every row.
Row 3, [24, 0.4], becomes approximately [0.447, -0.447]. Temperature
sits above its column mean while signal strength sits below its own.
tf.tidy() runs a synchronous callback and releases its temporary .
It keeps any tensors you return, including tensors inside an object such as
{ mean, std }. Keep those saved statistics available for later readings.
If a column is constant, its scale is zero. tf.where chooses divisor 1, so
the scaled training values become zero. Keep the saved scale unchanged. If a
future value changes, scaling returns its difference from the saved offset.
The model has not learned how to handle that new variation.
Fit statistics on training rows only. Reuse them for validation and future readings so the same raw value keeps the same meaning.
The fixed preview neuron
The starter supplies 's model: two [0.9, -1.4], -0.3,
and , a function that maps the into 0 to 1. You are
preparing its inputs and running it. Building and training models comes next.
The score is not a calibrated probability.
For the scaled row [0.447, -0.447], the calculates
0.447 * 0.9 + (-0.447) * (-1.4) - 0.3, about 0.729.
Sigmoid turns that into a score of about 0.6745. The Concept activity
shows each term. Changing the scaler changes these inputs while the weights
stay fixed.
const scores = previewModel.predict(normalized); // [4, 1]
// Inspect one of those readings as a one-row batch.
const reading = tf.tensor1d([24, 0.4]);
const oneScore = tf.tidy(() => {
const divisor = tf.where(std.equal(0), tf.onesLike(std), std);
const scaled = reading.sub(mean).div(divisor);
return previewModel.predict(scaled.expandDims(0));
});
const oneValue = oneScore.dataSync()[0]; // About 0.6745 for this row.
reading.dispose();
oneScore.dispose();predict() expects a batch. expandDims(0) makes one [2] reading into a
[1, 2] batch. The returned prediction belongs to the caller. Here,
dataSync() reads its value before cleanup. Your scoreReading(reading)
function can return that prediction tensor or its numerical value.
The saved mean and std remain separate from batch. Reuse them even after
the caller disposes of the original training rows. Fitting statistics on the
new reading would give that reading a different meaning.
Min-max scaling
Min-max uses (value - min) / (max - min). Fit each column's bounds with
batch.min(0) and batch.max(0). The calculation below uses the same
zero-scale rule. Your scaleFeatures function will accept this minimum and
range in place of the mean and standard deviation.
const min = batch.min(0);
const max = batch.max(0);
const range = max.sub(min);
const unitRange = tf.tidy(() => {
const divisor = tf.where(range.equal(0), tf.onesLike(range), range);
return batch.sub(min).div(divisor);
});
range.dispose();
// Keep min and max if you will scale later readings.Nonconstant training columns span 0 to 1. A later value outside the fitted bounds can fall outside that range. Scaling changes numerical units. It does not decide which contains useful information.
Z-score expresses distance from the training mean in standard-deviation units. Min-max expresses position within the fitted bounds. Choose the input format that the model expects and preserve it for later readings. affect both methods by changing the mean and spread, or stretching the range.
Challenge
gives you a model with fixed weights. Scale the two measurements in each row, then run the scaled batch through it. Inspect your saved statistics, scaled inputs, and returned scores. The exercise supplies the model. Your job is to keep its input format consistent for this batch and later readings.
