Tensors: axes, ranks, and model weight blocks
Assistance allowed only after prediction and observed evidence are recorded.
Prerequisites
Complete Day 23; use the listed recovery link if matrix shapes are shaky.
Learning Objective
Describe a tensor as an n-dimensional array whose rank is its number of axes; name the batch, time, and channel axes of model tensors; and trace an embedding lookup from a table E of shape (V x d) to an output of shape (B, T, d).
Instructions
A scalar is rank 0, a vector rank 1, a matrix rank 2, and a batch of sequences of embeddings is a rank-3 tensor of shape (B, T, d): batch axis, time axis, channel axis. An embedding lookup gathers rows: ids of shape (B, T) into table E of shape (V, d) produce output (B, T, d). Weight matrices are rank-2 tensors; a hidden state carrying batch and time is rank 3.
Practice & Prediction Log
A small model has vocabulary V = 50000 and embedding width d = 512. Predict: (a) the number of scalar weights in table E; (b) the output shape when ids of shape (B=2, T=3) are looked up; (c) how many float32 values that output holds and its byte size.
Weight count is V * d. The lookup result carries one d-vector per (batch, time) position. float32 uses 4 bytes per value.
(a) E holds 50000 * 512 = 25,600,000 scalar weights. (b) Output shape (2, 3, 512): each of the 2*3 = 6 token positions is replaced by a 512-vector. (c) The output holds 2*3*512 = 3072 float32 values, i.e. 3072 * 4 = 12,288 bytes. The batch and time axes are new axes the lookup prepends to the row vector.
Your ledger derives (B, T, d) from (V, d) and (B, T) without prompting, you compute both element counts exactly, and you can name the physical meaning of each axis (which item, which position, which feature).
Authoritative Source Resources & Provenance
NumPy Reference: The N-dimensional array (ndarray)
Relevant Section / Range: ndarray.ndim / ndarray.shape and axis ordering
Verified Version & Date: NumPy 2.x documentation (Verified: 2026-10-30)
Primary semantics for ndarray shape, strides, reshape orders, and view-versus-copy behavior used in tensor memory accounting.
Basic Python
Shape, stride, and memory-ledger exercises are fully explained with local runnable examples.
Compared Candidate Sources & Selection Analysis (2)
Equivalent tensor semantics in the framework used later in the curriculum; NumPy reference selected to keep Days 22-24 dependency-free.
Selected as co-primary for Day 24: the broadcasting rules page pairs with the ndarray attribute reference.
Essence of Linear Algebra
Relevant Section / Range: Chapter 3 review: a matrix is a rank-2 tensor
Verified Version & Date: 2016-2023 (Verified: 2026-10-30)
Unequaled geometric and visual intuition for vectors, linear transformations, matrix multiplication, and inverses before tensor operations.
High-school coordinate geometry
Geometric explanations, worked formulas, and coordinate exercises are fully provided as original lesson text.
Compared Candidate Sources & Selection Analysis (2)
Established textbook/lecture approach, strong mathematical rigor alongside 3Blue1Brown visual intuition.
Theoretical determinant-free proof structure is mathematically elegant but less aligned with computational tensor engineering.