Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Tensor and Contraction

A tensor maps each named index in its shape to a value. A shape is an unordered set of named axes, so { N = 4, C = 3 } and { C = 3, N = 4 } identify the same tensor. An ordered representation of that shape behaves like a familiar multidimensional array. A tensor index supplies one value for every axis, so { N = 4, C = 3 } includes indices such as { N: 0, C: 0 } and { N: 0, C: 1 }.

TensorDimensionExampleNamed shape
Scalar0D5.2{}
Vector1D[1, 2, 3]{ I = 3 }
Matrix2Da 2 × 4 grid{ I = 2, J = 4 }
Image batch4Dfour RGB images{ N = 4, C = 3, H = 256, W = 512 }

Tensor contraction multiplies two inputs elementwise and reduces every shared axis that is absent from the output. Every contraction consists of broadcast, multiply, and reduce steps.

OperationEinsumBroadcastMultiplyReduce
Dot product\(I, I \rightarrow 1\)None.\(x_i y_i\)\(\sum_i x_i y_i\)
GEMV\(IJ, J \rightarrow I\)\(x\) across \(I\).\(A_{ij} x_j\)\(y_i = \sum_j A_{ij} x_j\)
GEMM\(IK, KJ \rightarrow IJ\)\(A\) across \(J\) and \(B\) across \(I\).\(A_{ik} B_{kj}\)\(C_{ij} = \sum_k A_{ik} B_{kj}\)

This page owns the introductory math only. Mapping, movement, and engine contracts remain in their respective reference chapters.