Unit 8 of 8 · about 6 hours · derive, measure

Batched shapes, operation costs and conditioning

Shapes determine what is computed and how much work it takes. Conditioning determines how much information small input errors can destroy even when the algorithm is stable.

You'll be able to

  • Trace batched products, transposes, reductions and broadcasting with explicit axis names.
  • Derive parameter, activation and arithmetic counts for dense maps and distinguish factorization from solve costs.
  • Prove the fixed- perturbation bound and condition-number squaring from the SVD.
  • Measure relative residual and solution error on a controlled failure case without confusing conditioning with algorithmic stability.

Covers

Batched shapes, broadcasting, parameter counts and operation costsCondition number, residual error, solution error and numerical stability
1

Read

In this order, only the parts listed

0 of 6 done

  1. Broadcast-compatible axesNumPy broadcasting and matmul shape rules

    General broadcasting rules and Broadcastable arrays

    Stop before the vector-quantization application.

  2. Batched matrix productsNumPy broadcasting and matmul shape rules

    Notes: two-dimensional multiplication, stacked matrices, one-dimensional promotion, and the comparison with dot

  3. Count products and QR workIntroduction to Applied Linear Algebra

    Chapter 10, section 10.1, Complexity of matrix multiplication and Complexity of matrix triple product, printed pp. 182 to 183; Chapter 12, section 12.3, Complexity, p. 232

    Use dense arithmetic counts. Leave specialized sparse products and do not interpret these counts as measured runtimes.

  4. Factor once, solve repeatedlyFundamentals of Numerical Computation: conditioning and operation counts

    Python edition, section 2.5.3, Solution of linear systems: Lemma 2.5.1, equations (2.5.7) to (2.5.8), and Theorem 2.5.1

    Derive the loop sums for triangular solves and LU. Skip the unseeded timing demonstrations and leave general asymptotic-definition proofs to Algorithms.

  5. Condition number and the residual-error gapFundamentals of Numerical Computation: conditioning and operation counts

    Python edition, section 2.8, equations (2.8.1) to (2.8.6), Definition 2.8.1, then section 2.8.2, equations (2.8.9) to (2.8.11)

    Prove the finite bound for perturbing b with A fixed. Skip the unseeded Hilbert example and the first-order matrix-perturbation formula (2.8.7); a finite bound for simultaneous perturbations is outside this unit.

  6. Norms and solver difficultyMIT 18.065 - Applied matrix bridge

    Lecture 8, Norms of Vectors and Matrices; Lecture 10, Survey of Difficulties with

    Focus on the induced 2-norm, conditioning and loss of accuracy. Leave iterative and large-data solution methods.

2

Focus on

  • Write the contracted axes separately from broadcast batch axes. For a dense -to--to- map, shared parameters with biases total ; retaining input, hidden and output for vectors stores scalar activations.
  • The standard dot-product algorithm costs operations for an by times by product. Modified Gram-Schmidt QR costs about for ; optimized LU costs about and each triangular solve about . These are algorithm-specific counts, not timing claims.
  • For invertible square , . From and derive for nonzero , using compatible induced norms.
  • For full-column-rank rectangular , define over its positive singular values. The SVD gives eigenvalues for , hence . Rank deficiency makes the Gram matrix singular.
Common mistakes · 3
  • A successful broadcast can be wrong: subtracting a (3,) target from a (3,1) prediction produces (3,3), not three residuals. Assert shapes before arithmetic.
  • Array .T reverses all axes. For batched matrix transpose use swapaxes(-1,-2); transpose only the two matrix axes.
  • A small residual is backward-error evidence, not a guarantee of accurate . Conditioning belongs to the mathematical problem; stability belongs to the algorithm.
3

Try it yourself

Closed-book, before checking anything

Closed-book, derive the product count, triangular/LU loop sums, the fixed- perturbation bound, and condition-number squaring from . For a deterministic NumPy check, use float64 X=np.arange(24,dtype=np.float64).reshape(2,3,4), W=np.arange(20,dtype=np.float64).reshape(4,5), bias=np.arange(5,dtype=np.float64), with no random draws. Predict Y=X@W+bias has shape (2,3,5), Y[1,2,4] approximately 1018, and Y.sum(axis=1,keepdims=True) shape (2,1,5). The standard product count is 2*3*5*(2*4-1)=210 scalar operations, plus 30 bias additions, with 25 shared parameters. Then use A=diag(1,1e-6), b=(1,0), db=(0,1e-8), all float64; x=solve(A,b), xp=solve(A,b+db). Measure approximately 1e6, relative solution change norm(xp-x,2)/norm(x,2) approximately 0.01, and original-system relative residual norm(b-A@xp,2)/norm(b,2) approximately 1e-8. These attain the bound by design; use absolute tolerance 1e-12 for the two ratios and entry, and relative tolerance 1e-12 for . This changes the right-hand side, not the solver.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.What shapes result from (2,1,3,4) @ (1,5,4,6), and from swapping only the last two axes of the result?Answer

    The matrix product is (2,5,3,6): 4 contracts and batch shapes (2,1) and (1,5) broadcast to (2,5). Swapping the matrix axes gives (2,5,6,3). Reversing every axis with .T would instead give (6,3,5,2).

  2. 2.Why does forming square the 2-norm condition number, and which rank assumption is needed?Answer

    The SVD gives . With full column rank all eigenvalues are positive, so their extreme ratio is . A wide full-row-rank matrix still has a singular Gram matrix, so the finite full-column-rank formula cannot be used there.

  3. 3.Where is invertibility used in the perturbation proof, and why does a residual give the same bound?Answer

    Invertibility lets become . Induced-norm inequalities bound the two products, and nonzero permits relative normalization. If , then solves the fixed- system with right-hand side ; substitute . No claim is made about the algorithm producing .

  4. 4.Why is reusing factors cheaper for several right-hand sides?Answer

    For dense by , one LU costs about . Each subsequent right-hand side requires two triangular solves, about operations. Recomputing the factorization for every vector repeats cubic work. Pivot comparisons and data movement are not captured by these arithmetic counts.

5

Practice

  • VMLS Exercise 10.11: trace, shapes and computation cost

    Chapter 10, Exercise 10.11(a) to (d), printed p. 192

    Derive the direct elementwise trace computation and its cost instead of forming the whole Gram product. The book's matrix norm here is Frobenius, not the induced 2-norm. The section mini-project should add batched shape assertions and a table of residual versus solution error as conditioning varies.

  • FNC Exercise 2.8.7: condition number of a product

    Python edition, section 2.8.3, Exercise 2.8.7(a) and (b)

    Use submultiplicativity and , then construct a strict-inequality example. State the induced norm and nonsingularity assumptions.

You're done when

You predict the batched output and reduction shapes, derive the operation counts and both conditioning bounds, and explain how the worked case has a tiny residual but a one-percent solution change.

Only when you can do this without notes.

Leads to

Notes

Resources · 4 for this unit, 4 more in the section
  • MIT 18.065 - Applied matrix bridgeMIT OpenCourseWare · required

    Use lectures 3-10 after 18.06 for SVD, PCA, low-rank approximation, matrix norms and solver difficulty.

  • Introduction to Applied Linear AlgebraBoyd & Vandenberghe · support

    Sample Chapters 1-13 and the extra exercises for computational practice in fitting, least squares and pseudoinverses.

  • NumPy broadcasting and matmul shape rulesNumPy Developers · reference

    Read General broadcasting rules and Broadcastable arrays, then matmul Notes for stacked matrices and one-dimensional promotion. These close the pool's array-semantics gap.

  • Fundamentals of Numerical Computation: conditioning and operation countsDriscoll & Braun · reference

    Python edition, section 2.8 through Residual and backward error for the fixed-matrix perturbation bound; section 2.5.3 for triangular-solve and LU flop counts. Use Exercise 2.8.7 for a condition-number proof, not the unseeded Hilbert experiment.

Also in this section

  • MIT 18.06 - Linear AlgebraMIT OpenCourseWare · primary

    Follow lectures 1-10, 14-17, 21-22, 25, 27, 29-31 and 33 with matching problems. This is the ordered first pass.

  • MIT 6.042J - Proof strandMIT OpenCourseWare · required

    Study textbook Chapters 1 and 3, §§4.1-4.4 and §§5.1-5.4 with matching Unit 1 problems. Leave the rest of the course.

  • Mathematics for Machine LearningDeisenroth, Faisal & Ong · reference

    Use Chapters 2-4 and §10.2 to check notation or re-derive a result. Do not treat it as a second course.

  • Essence of Linear Algebra3Blue1Brown · optional

    Open only when a geometric picture is missing; it does not replace derivation or problem solving.