Tensors, Shapes, Dtypes, and Broadcasting
Representing Data as Tensors
Tensor Structure
A tensor combines shape, data type, and values. For example, a vector of length has shape , while a batch of RGB images of size has shape if channels are last.
Tensor Rank Examples
| Rank | Example | Shape |
|---|---|---|
| 0 | Scalar | () |
| 1 | Vector | (n,) |
| 2 | Matrix | (m, n) |
| 3 | Color image batch | (batch, height, width, channels) |
Broadcasting
Broadcasting lets TensorFlow align tensors with different shapes when one dimension is or missing, reducing the need for manual reshaping.
Why Shape Discipline Matters
Most tensor errors come from mismatched shapes or incompatible ranks. Knowing when to use reshape, expand_dims, squeeze, transpose, and concatenate is essential for stable model code.
Useful Tensor Operations
Which shape most likely represents a batch of grayscale images?
Grayscale images usually have one channel, so a batch shape commonly includes a trailing dimension of .
Correct answer: (batch, height, width, 1)
What does broadcasting help TensorFlow do?
Broadcasting lets TensorFlow perform elementwise operations even when one tensor has size-1 dimensions or lower rank.
Correct answer: Apply operations across compatible shapes
Why is dtype important?
Data type affects memory usage, numerical precision, and compatibility across operations.
Correct answer: It determines how values are stored and computed