Phase 01: Math Foundations

Tensor Operations

Tensors are the common language between data and deep learning. Every image, every sentence, every gradient flows through them. Language: Python Implement a tensor class with shape, strides, reshape, transpose, and element-wise operations from scratch. Apply broadcasting rules to operate on tensors of different shapes without copying data. Write einsum expressions for dot products, matrix multiplications, outer products, and batched operations. Trace the exact tensor shapes through every step of multi-head attention. You build a transformer. The forward pass looks clean. You run it and get: RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x768 and 512x768). You stare at the shapes. You try a transpose. Now it says Expected 4D input (got 3D input). You add an unsqueeze. Something else breaks. Shape errors are the most common bug in deep learning code. They are not hard conceptually -- each operation has a shape contract -- but they multiply fast. A transformer has dozens of reshapes, transposes, and broadcasts chained together. One wrong axis and the error cascades. Worse, some shape mistakes do not throw errors at all. They silently produce garbage by broadcasting along the wrong dimension or summing over the wrong axis. Matrices handle pairwise relationships between two sets of things. Real data does not fit into two dimensions. A batch of 32 RGB images at 224x224 is…

Tensor Operations: Tensors are the common language between data and deep learning. Every image, every sentence, every gradient flows through them.

This free lesson is part of the AI Engineering from Scratch curriculum. Read the full explanation, run the lesson code, and verify the result in the interactive reader or from the repository source.

Browse the complete course catalog or open this lesson on GitHub.