Shapes follow the task
Choose tensor shapes around the task
Tensor shapes describe how numerical values are arranged, while your task determines what those axes mean. A text model, an image model, and a tabular classifier may all consume tensors but require different preparation and output interpretation. Use this guide to choose and document a layout before connecting components. The examples are illustrative contracts, not universal model requirements: check the selected model or runtime and adapt each shape to its actual input specification. Keep the task visible throughout that process.
Choose axes that describe the input
Begin by identifying what varies within one example and what separates examples. Text has a sequence axis; an image has spatial and channel axes; a prepared table has a feature axis. A batch groups compatible examples without changing the meaning of their individual fields.
| Illustrative input | Layout | Document explicitly |
|---|---|---|
| Token identifiers | [batch, sequence] | Tokenizer and mask convention |
| Image pixels | [batch, channels, height, width] | Channel order and pixel scaling |
| Prepared records | [batch, features] | Feature order and units |
Other layouts are valid when the model expects them. Do not infer axis meaning from dimension sizes alone: a channel count can happen to equal a spatial dimension. Keep named axes in documentation and use small fixtures with recognizable values to catch accidental transpositions.
Prepare values without losing their meaning
Shape validation establishes organization, not semantic correctness. A correctly shaped image can still have swapped color channels. A table can contain the expected number of features in the wrong order. Text can be tokenized with a vocabulary incompatible with the selected model.
Define preprocessing as a versioned sequence. For images, describe resizing, cropping, and scaling. For text, retain the tokenizer identity and explain padding or truncation. For tables, specify numerical units, category encoding, and how missing values become model inputs. Never treat a missing measurement as zero without a task-specific reason.
Use the same documented transformations during evaluation and deployment. When a transform changes, inspect both its immediate output and the final task result. Record enough metadata to distinguish an execution problem from a preparation problem without retaining every sensitive input in ordinary logs.
Choose an output shape the application needs
Decide whether the task produces one result per example, one result per token, or a dense spatial output. A document classifier may return one label vector per document, while a token classifier needs a label vector at each relevant position. Collapsing an axis is a modeling decision, not a formatting shortcut.
Describe any pooling or aggregation used to convert internal representations into the public result. Keep the label vocabulary or representation revision with the output. Preserve the original item identifier when batching or unpadding results so that every prediction stays attached to the right input.
Validate the whole path with a small set of recognizable examples: one normal input, a variable-length case, and a deliberately malformed shape. Inspect whether the final result answers the intended task before optimizing memory use or numerical precision.
Use the official reference for the documented interface; the workflow recommendations above are Tensor API Lab guidance.