Part IV · Ch. 20 — Getting Data into Tensors

Part IV · Chapter 20 of 29

Getting Data into Tensors

Shape, split, and normalize your examples


Start with the examples

Our model needs arrays, but our data arrives as a file full of rows, images, or measurements. Which entries belong together, which one is the answer, and which information will be available when we predict? These decisions happen before the first gradient. We’ll make each conversion small enough to inspect and preserve a route back to its source.

A sample is one example used as input or evaluation. A feature is a measured or encoded input value. A row can be a sample without every column being a feature: the target must be separated before constructing inputs. An identifier can also be useful for splitting without being a useful prediction feature. Ask what each column means before converting everything that looks numeric into a tensor.

Five actual miniature data forms beside labeled array silhouettes and axis legends. Graph panel includes both features and connectivity; text IDs use a separate integer dtype label.
Different input forms require different named axes.

Read the axes aloud. A table X has shape (N,F): N samples, F features. Images have (N,C,H,W): samples, channels, height, width. Token IDs have (N,T), with T counting sequence positions. Numerical sequences have (N,T,F). Graphs combine node features X=(N,F) with adjacency A=(N,N). Here T is a sequence length, not softmax temperature. The same letter can carry a local meaning only when we state it.

A tiny table

Our CSV columns are length,color,target, with rows (1,small,0), (3,large,1), and (2,small,0). The strings “small” and “large” are category values despite the illustrative column name. Remove target from the features. Fit a category vocabulary using training rows: small=(1,0), large=(0,1). Each row now has a numeric length plus two category indicators. Unknown categories become all zeros in this runner; a missing cell is an error, not a category we silently invent.

Keep information in its proper split

The training split fits the weights. Validation guides development choices. A test set is reserved for the final assessment after those choices are complete. leakage occurs when evaluation information enters fitting or selection. This includes preprocessing statistics as well as model updates. If we compute a mean on the whole file and then split it, the training representation already contains information from the evaluation rows.

Draw repeated-person record cards grouped into one split, beside an explicitly leaking split that scatters one person across train/test. A second panel marks a chronological boundary before window construction.
Split the independent units before fitting preprocessing or constructing overlapping windows.

Independent rows are an assumption, not a consequence of storing data in a CSV. Repeated observations from the same person or device can be strongly related. Keep such groups together when evaluating transfer to new groups. For future prediction, split chronologically. stratification preserves class representation in a random split, which can be useful, but it does not replace grouping or time order. The right split follows the question we want the evaluation to answer.

Fit the scale on training only

Training values [1,3] have mean 2. Their population standard deviation is sqrt(((1−2)²+(3−2)²)/2)=1. Subtracting 2 and dividing by 1 gives [−1,1]. A held-out value 4 becomes (4−2)/1=2, using those same training statistics. Including 4 when calculating the mean would leak evaluation information. A held-out value outside the transformed training range is allowed; we do not refit the scale to make it look familiar.

Make the representation explicit

standardization subtracts the training mean and divides by its training standard deviation. normalization is the broader idea of transforming values to a chosen numerical scale or convention. These words can describe different operations, so our export records the actual recipe. We use m for the training mean, s for the training standard deviation, x for a raw value, and z for the transformed value:

Numeric table for [1,3] training and 4 validation: mean 2, standard deviation 1, transformed values −1,1,2. Add the exact preprocessing keys read from a real CLI model export in chapter 23.
Validation data uses preprocessing fitted on training data.

The numeric ledger preserves a feature’s name, whether it was treated as numeric, its fitted mean, and its fitted scale. Category columns preserve their training vocabulary instead. The runner stores this preprocessing in model.json because the weights expect exactly that input representation later. Reordering categories or changing a numeric scale can change every weighted sum even when the exported weights are identical.

$$z=\frac{x-m}{s}$$

In words: subtract the training mean and divide by the training standard deviation; handle constant features explicitly.

Images, text, and windows

A 2×2 grayscale image [[0,1],[1,0]] has shape (1,2,2) before adding the sample axis. A tokenizer is a rule converting text into coded items. In our hand vocabulary, a,b,a maps to [1,2,1]; a,b pads to [1,2,0] with mask [1,1,0]. These token IDs belong to the teaching lookup, not E6. For the ordered sequence [1,2,3] and window two, input [1,2] predicts target 3.

The mask says which positions are real inputs for the relevant operation. It is separate metadata, not a guarantee that a zero-valued feature should be ignored. A legitimate pixel can have brightness zero and still matter to a convolution. Likewise, a graph needs its connectivity in addition to node features. If we keep only the feature matrix, the message-passing neighborhood has disappeared even though every node still has numbers.

Inspect each conversion

Start with the three-row CSV and use the first two rows for training. The length feature uses their mean and scale; the third row is transformed afterward. Select a numeric output to trace it back to its CSV row and column. Then change the training-row count and watch both numeric statistics and category vocabulary change. A split choice affects the representation, not just the rows used for the loss.

Where this shows up when you train

augmentation makes modified training examples intended to preserve the target’s meaning. A slight image shift can be appropriate when the class should survive that shift. It can be inappropriate when position itself defines the label. Define the allowed transformation using the task before making extra examples. Keep all versions of an original example in its training group, rather than allowing a transformed copy to leak into validation.

More rows do not necessarily provide more independent evidence. A thousand near-duplicate frames can describe less variation than a smaller collection from distinct conditions. A random split can hide that problem by placing nearly identical records on both sides. Inspect group identifiers and time ranges, not just split percentages. The arithmetic of the model cannot repair a misleading evaluation boundary chosen before training began.

What you now know

  • Data axes have meanings as well as lengths.
  • Fit preprocessing on training examples only.
  • Splits must respect the independence and time structure of the task.

Where we’re headed

We can now choose the objective, model, update settings and stopping rule for a concrete run. Continue the story.