Skip to content
Reading as a guest: this browser saves your progress. Create a free account and we’ll carry your progress over.

Foundations: Tensors & Data

Mission Map
SECTOR foundations, MISSION 5, 27 min, 1 exercise

Data Normalization

What happens when channels use different scales?

A reading from scaler to score

Follow the challenge's four-row batch. Apply its saved statistics to one row, then inspect the supplied neuron's calculation.

This JavaScript reference uses the challenge's two-feature practice fixture and fixed weights. The fixture specifies no physical units or timestamps. Changing a reading reuses the saved training statistics. This activity performs no TensorFlow.js prediction or training.

Experiment controls

The row [24, 0.4] appears in the supplied four-row training batch.

24
0.4

Current inputs

Two-feature scaler and fixed score ruleSupplied example

Selection
The row [24, 0.4] appears in the supplied four-row training batch.
Order
Temperature slot first, signal-strength slot second.
Units
No physical units or timestamps supplied. Standardized values and min-max ratios are dimensionless.
New row before scaling
Read the values
New row before scaling
PositionValue
Temperature24
Signal0.4

Make a prediction, or run the experiment to see the result.

Choose your inputs, then run the experiment.

Your result will appear here. After two runs, you can compare what changed.

Inspect the reference example

Use the challenge's four-row practice batch and fixed neuron. Trace one reading through the saved statistics, scaled inputs, and score.

Training batch: temperature, signal strength

200.2220.8240.4260.6

Raw inputs

200.2220.8240.4260.6
Fixed-neuron scores: 1.000, 1.000, 1.000, 1.000. The weights stay [0.9, -1.4] and bias -0.3. Only the input scale changes.
const scores = previewModel.predict(batch);
// Raw inputs saturate this fixed neuron.

Fit each column's scaler on training data, then reuse those statistics on later data. The supplied weights stay fixed.

JavaScript simulation with fixed practice data. It does not run or train the lesson's models. Your challenge uses its own data and tests.

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.

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.

fit-and-scale.tstypescript
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.

score-the-scaled-input.tstypescript
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.

min-max-columns.tstypescript
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.

Reference: population variance and the supplied neuron

JavaScript

Scroll progress calculation
const progress = scrollY / (docHeight - viewportHeight)

Machine Learning

Min-Max Normalization
const normalized = data.sub(min).div(max.sub(min))
Structural Bridge
Where the analogy ends
Scroll progress uses page dimensions. Sensor scaling uses chosen physical bounds or statistics fitted on training data. New observations can exceed fitted min/max values. Z-score uses a mean and standard deviation instead, but it is not automatically better or immune to drift and outliers.

Known physical bounds can also supply min-max limits. Here, the exercise fits its bounds on the supplied training rows.

The population variance used here divides by n. The usual unbiased sample variance divides by n minus 1. Its square root is not an unbiased estimator of standard deviation. Use the population convention required by this task.

The starter builds the fixed neuron this way:

supplied-preview-neuron.tstypescript
const previewModel = tf.sequential();
previewModel.add(tf.layers.dense({
units: 1, activation: 'sigmoid', inputShape: [2],
}));
tf.tidy(() => previewModel.setWeights([
tf.tensor2d([[0.9], [-1.4]]), tf.tensor1d([-0.3]),
]));

For [20, 0.2], the scaled row is approximately [-1.342, -1.342]. The weighted sum is about 0.371. Sigmoid returns about 0.592. Without scaling, the temperature term alone is 18 and the output saturates near 1. The weights have not changed. A confident score does not establish that an input is familiar or that the answer is correct.

Practice: a different scaler and reading

This optional example uses different readings and a model with different fixed weights. Use the statistics printed inside it, then return to the four-row batch in the challenge.

Try the calculation

The model and its scaler

These four synthetic training rows fit the scaler. The neuron keeps weights [0.6, -0.8] and bias 0.1 throughout. Change preprocessing or one reading and watch the score change. The example supplies the weights and performs no training. The sigmoid score has no established calibration.

The model and its scaler controls
Experiment inputs

Training rows [temperature, signal]: [20, 0], [20, 2], [24, 0], [24, 2]. Population mean [22, 1], standard deviation [2, 1]. A saved zero standard deviation uses divisor 1. The saved statistic stays zero.

artifact: demo-neuron-v1, scaler: training-v1, features: temperature, signal

Enter numbers separated by commas when there is more than one. Rounding to three decimal places is enough.

0/20 checks in this case. No prediction recorded yet. Viewing feedback also counts as help for later attempts. Reset keeps attempts, help and any frozen policy. Draft is in memory only. This practice does not complete the lesson.

This tab keeps the practice record. Sign in to save a summary across devices. Answers stay in this browser.

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.