Phase 04 / Math & optimization
Vectors, norms, and projections
Revision v0.18.1 · Updated since your last visit. Review the changes; your progress is saved.
Lesson · about 60 minutes · CPU practice
Read a dot product geometrically and separate a vector into a projection and an orthogonal residual.
Phase study guide: preparation, milestones and project evidence
Before you start
Before this: State, randomness & control flow
Hardware: CPU
Setup help: Python, notebook, or Cloud TPU VM
Use the tested CPU packages (the recorded environment uses Python 3.14.3). Other Python versions require separate checks. Hardware receipts apply only to their recorded source, environment, hardware, and cases; they do not qualify every lesson or TPU configuration. See the TPU performance and hardware evidence guide. The optional Cloud TPU specialization explains target-specific execution boundaries. Logical CPU devices practice partitioning; they do not simulate TPU performance.
Download the repository ZIP or use your existing checkout. Extract it and open a terminal in the folder containing requirements-cpu.txt. The beginner ZIP is separate; these lessons use the full repository folder.
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements-cpu.txtpy -3 -m venv .venv
.\.venv\Scripts\python.exe -m pip install -r requirements-cpu.txtAfter activation, run the complete worked example from that same folder:
python phases/04-optimization/05-vectors-norms-and-projections/code/main.py.\.venv\Scripts\python.exe phases/04-optimization/05-vectors-norms-and-projections/code/main.pyFor an incremental build, create main.py in your own practice folder, append each build block in order, and run python main.py with this environment active. Keep that file separate from the downloaded reference solution.
Download this lesson’s notebook. Open it with a Python kernel using the course environment. Run cells from the top after restarting the kernel; experiments depend on earlier definitions.
For local Jupyter, first install the course requirements using the Local Python steps, then use your environment’s Python to start Jupyter:
python -m pip install jupyterlab
python -m jupyter lab.\.venv\Scripts\python.exe -m pip install jupyterlab
.\.venv\Scripts\python.exe -m jupyter labSelect the downloaded optimization-05.ipynb file in JupyterLab. The notebook contains reference solutions; make your prediction and attempt each practice task before opening them. Restart the kernel before changing logical device setup.
The worked reference for this lesson is CPU-validated. Some references explicitly configure logical CPU devices; setting JAX_PLATFORMS does not adapt them to a TPU. Follow the execution matrix and reviewed multi-host example before attempting a target run.
For accelerator preparation, study TPU setup, observed runtime receipts, checkpointed jobs and precision and retained traces. Current receipts cover bounded v5e1 launch, resume, and precision checks; four single-device Pallas cases; and a four-device v5e collective fixture. Each receipt is limited to its recorded source, environment, cases, and hardware. It does not qualify all lessons or TPU configurations. Cloud provisioning is optional for CPU learning.
Check: Projection , residual . Both orthogonality and the length identity pass.
Revisit the guided CPU setup · Open 19: TPU Setup. Package instructions are CPU instructions; Windows and accelerator environments need their own validation.
What you will be able to do
- Read a dot product geometrically and separate a vector into a projection and an orthogonal residual.
- Read length and alignment separately
- Find the closest point on a line
- Use orthogonality as an independent check
- Know when the formula is undefined
The problem
When a model multiplies features by weights, what is it measuring? Start with two arrows you can draw on paper. We will measure length, alignment and the part of one arrow that lies along the other. These ideas make regression and gradient directions easier to reason about.
The idea
Projecting a vector onto a direction keeps the part parallel to that direction. The residual is what the direction cannot explain. This small geometric idea will reappear in least squares, gradient components and constrained updates.
A projection explains what remains
Take and direction . The projection is , leaving residual . Their sum recovers , and the residual is perpendicular to . The original norm is , consistent with the right triangle.
Now replace by . The direction has not changed, so the projected vector should not change. The coefficient must adjust for the direction's squared norm. Forgetting that denominator makes the result depend on an arbitrary scaling of the direction.
Read the vector figure by following the projection and then the residual to the original endpoint. Use equal axis scale: otherwise a mathematically right angle may not look right on the page.
Read length and alignment separately
Let and . The length of is , and . The dot product is not a distance or a probability: reversing changes its sign. For nonzero vectors, dividing by both lengths gives the cosine of their angle, here . In higher dimensions we use exactly the same sum, even when we cannot draw all the axes.
Find the closest point on a line
Points on the line through have the form , where is a scalar. Choose the point nearest . Its coefficient is . For our axis direction, , so the projection is and the leftover is . Notice that the coefficient depends on how long we draw ; the projected point does not.
With the rescaled direction , the numerator and the denominator , so the coefficient is . The projected vector remains . Changing the length used to describe a line must not change the closest point on that line.
Use orthogonality as an independent check
Orthogonal means perpendicular; algebraically, the dot product is zero. Because and are perpendicular, their squared lengths add to the squared length of : . We check both this identity and the leftover dot product. Matching a library projection to a copy of the same formula would give less independent evidence. For float32 calculations, a small absolute tolerance allows rounding near zero.
Know when the formula is undefined
The zero vector has no direction. Projecting onto it would divide by , and cosine similarity with it is also undefined. Our eager teaching helper raises a clear error for that input. A compiled application needs a separately defined policy, such as a validity mask or input validation before compilation. Adding a tiny constant silently changes the mathematical question; explain that policy if you choose it.
Projection and leftover form a right triangle
Predict before looking: Which part of is explained by the horizontal direction?
Read the figure
Both axes are ordinary vector coordinates. The diagonal arrow goes from the origin to . The horizontal arrow reaches the projection , and the vertical arrow then goes from that point up to .
Follow the horizontal and vertical arrows tip to tail: together they reproduce the diagonal arrow. Although the residual arrow is drawn starting at , its displacement is , not .
Connect it to the computation
We projected onto the horizontal direction. The residual is perpendicular to that direction, so its dot product with is zero. This right angle explains why moving the projection along the horizontal line cannot remove the remaining vertical error.
The lengths form a familiar check: , , and , with . Projection separates the part represented by the chosen direction from the part that direction cannot represent.
Before changing the direction vector length, predict whether the projected endpoint should move; a correct projection onto the same line leaves it fixed.
Figure data and execution details
Open full-size figureDownload data and provenance
The notebook contains the data experiment, plotting code, and recorded figure output. Rerun it to test your changes.
Your build: Create a separate main.py in your practice folder. Append each block in order and run python main.py in the lesson environment. The downloaded script is the complete reference, so keep it separate from your attempt.
1 Draw the two vectors
Create main.py in your activated CPU course environment. Add the vectors and verify their lengths before adding a projection.
# Step 1 — Draw the two vectors
import jax.numpy as jnp
u = jnp.array([3.0, 4.0])
a = jnp.array([1.0, 0.0])
assert jnp.allclose(jnp.linalg.norm(u), 5.0)
assert jnp.allclose(jnp.dot(u, a), 3.0)The length is ; the component along the horizontal unit direction is .
Explain the APIs in this step
jnp.array(values, dtype=...): Constructs a JAX array from Python/NumPy values.jnp.allclose(actual, expected, rtol=..., atol=...): Returns a JAX boolean scalar indicating whether corresponding values are within the requested tolerance.
2 Build the projection
Append this eager helper. Keep the input check outside any jitted function in this exercise.
# Step 2 — Build the projection: The leftover r contains what the chosen direction cannot explain.
def project(u, a):
denominator = jnp.dot(a, a)
if float(denominator) == 0.0:
raise ValueError('Projection needs a nonzero direction')
return jnp.dot(a, u) / denominator * a
p = project(u, a)
r = u - pThe leftover contains what the chosen direction cannot explain.
3 Check with geometry
Append these assertions and run python main.py. Compare with your drawing before reading the experiment.
# Step 3 — Check with geometry: You should see [3,0] and [0,4]; the squared lengths sum to 25.
assert jnp.allclose(p, jnp.array([3.0, 0.0]))
assert jnp.allclose(jnp.dot(a, r), 0.0, atol=1e-06)
assert jnp.allclose(jnp.dot(p, p) + jnp.dot(r, r), jnp.dot(u, u))
print('projection / residual:', p, r)You should see and ; the squared lengths sum to .
Explain the APIs in this step
jnp.allclose(actual, expected, rtol=..., atol=...): Returns a JAX boolean scalar indicating whether corresponding values are within the requested tolerance.jnp.array(values, dtype=...): Constructs a JAX array from Python/NumPy values.
View the cumulative build in main.py
These blocks append in order to your separate practice file.
# Step 1 — Draw the two vectors
import jax.numpy as jnp
u = jnp.array([3.0, 4.0])
a = jnp.array([1.0, 0.0])
assert jnp.allclose(jnp.linalg.norm(u), 5.0)
assert jnp.allclose(jnp.dot(u, a), 3.0)
# Step 2 — Build the projection: The leftover r contains what the chosen direction cannot explain.
def project(u, a):
denominator = jnp.dot(a, a)
if float(denominator) == 0.0:
raise ValueError('Projection needs a nonzero direction')
return jnp.dot(a, u) / denominator * a
p = project(u, a)
r = u - p
# Step 3 — Check with geometry: You should see [3,0] and [0,4]; the squared lengths sum to 25.
assert jnp.allclose(p, jnp.array([3.0, 0.0]))
assert jnp.allclose(jnp.dot(a, r), 0.0, atol=1e-06)
assert jnp.allclose(jnp.dot(p, p) + jnp.dot(r, r), jnp.dot(u, u))
print('projection / residual:', p, r)Run the example
python3 phases/04-optimization/05-vectors-norms-and-projections/code/main.pyAbout this command
This shortcut uses an activated macOS/Linux course environment in the repository folder. For Windows, use the explicit interpreter command in “Setup help” above. The script includes worked examples and reference solutions.
Inspect the complete assembled example
# Step 1 — Draw the two vectors
import jax.numpy as jnp
u = jnp.array([3.0, 4.0])
a = jnp.array([1.0, 0.0])
assert jnp.allclose(jnp.linalg.norm(u), 5.0)
assert jnp.allclose(jnp.dot(u, a), 3.0)
# Step 2 — Build the projection: The leftover r contains what the chosen direction cannot explain.
def project(u, a):
denominator = jnp.dot(a, a)
if float(denominator) == 0.0:
raise ValueError('Projection needs a nonzero direction')
return jnp.dot(a, u) / denominator * a
p = project(u, a)
r = u - p
# Step 3 — Check with geometry: You should see [3,0] and [0,4]; the squared lengths sum to 25.
assert jnp.allclose(p, jnp.array([3.0, 0.0]))
assert jnp.allclose(jnp.dot(a, r), 0.0, atol=1e-06)
assert jnp.allclose(jnp.dot(p, p) + jnp.dot(r, r), jnp.dot(u, u))
print('projection / residual:', p, r)Expected result: Projection , residual . Both orthogonality and the length identity pass.
Recorded output and runtime details
Reference output captured from executing the complete lesson script (code/main.py) and its assertions in the course CPU environment.
projection / residual: [3. 0.] [0. 4.]
projection / residual: [3. 0.] [0. 4.]
PASS: optimization-05
Download execution record · The notebook includes executed cells and figure outputs. Compare your run with these values; timings depend on the machine.
Rescale the direction
# Experiment — Rescale the direction: A line does not change when its nonzero direction is rescaled.
assert jnp.allclose(project(u, 2 * a), p)
assert jnp.allclose(project(u, -a), p)
assert jnp.allclose(jnp.dot(u, 2 * a), 2 * jnp.dot(u, a))Expected: The projection stays fixed; the raw dot product doubles.
A line does not change when its nonzero direction is rescaled. A dot product alone does change.
Rotate the line
# Experiment — Rotate the line
diagonal = jnp.array([1.0, 1.0])
p2 = project(u, diagonal)
assert jnp.allclose(p2, jnp.array([3.5, 3.5]))
assert jnp.allclose(jnp.dot(diagonal, u - p2), 0.0, atol=1e-06)Expected: Projection , residual .
Changing direction changes what can be explained; merely changing its length does not.
Make it yours
Where to put your code: Modify your own main.py, or use the Make it yours cell in your downloaded notebook. Run your attempt in the declared environment and verify your prediction before opening the reference solution.
Project onto . Check the residual by hand first.
Show a writing guide and starter scaffold
How to write this exercise code
Learner-only inputs, outputs, checks and hints
Inputs
- Two same-shaped vectors and a nonzero projection direction.
Outputs
- The projection and orthogonal residual.
Invariants to check
- The residual is orthogonal to the direction; a zero direction is rejected.
Hints
- Compute the scalar coefficient from a dot product and squared norm.
- Use an inner-product check on the residual instead of checking only its length.
import jax.numpy as jnp
def project_onto(vector, direction):
"""The projection and orthogonal residual."""
# TODO: Guard against a zero direction, compute the projection coefficient, and verify the residual dot product.
raise NotImplementedErrorReveal the reference solution
# Exercise solution: Project u=(2,-1,2) onto a=(1,0,1).
u3 = jnp.array([2.0, -1.0, 2.0])
a3 = jnp.array([1.0, 0.0, 1.0])
p3 = project(u3, a3)
assert jnp.allclose(p3, jnp.array([2.0, 0.0, 2.0]))
assert jnp.allclose(jnp.dot(a3, u3 - p3), 0.0)Handle an undefined direction
Transfer / diagnosis
Show that a zero direction produces a clear input error instead of a misleading projection.
Show a writing guide and starter scaffold
How to write this practice solution
Learner-only inputs, outputs, checks and hints
Inputs
- Show that a zero direction produces a clear input error instead of a misleading projection.
Outputs
- A clear rejection for zero direction and a finite projection for a nonzero direction.
Invariants to check
- A projection is returned only when its direction has nonzero squared norm.
Hints
- Use a try/except block; a silent successful result should fail the check.
import jax.numpy as jnp
def practice_handle_an_undefined_direction(vector, direction):
"""Implement the handle an undefined direction check."""
# TODO: Check the direction norm before dividing and raise a clear ValueError for the zero vector.
raise NotImplementedErrorShow a hint
Use a try/except block; a silent successful result should fail the check.
Reveal reference solution and reasoning
# Handle an undefined direction (Transfer / diagnosis): Rejecting an undefined input is part of the function contract.
try:
project(u, jnp.zeros_like(a))
except ValueError as exc:
assert 'nonzero' in str(exc)
else:
raise AssertionError('Zero direction was accepted')Rejecting an undefined input is part of the function contract.
Use two perpendicular directions
Transfer / diagnosis
Reconstruct from projections onto and . Would adding independent projections still work for arbitrary nonorthogonal directions?
Show a writing guide and starter scaffold
How to write this practice solution
Learner-only inputs, outputs, checks and hints
Inputs
- Reconstruct from projections onto and . Would adding independent projections still work for arbitrary nonorthogonal directions?
Outputs
- Reconstructed vector and residual norm for the perpendicular basis.
Invariants to check
- The sum is interpreted as a reconstruction only for the stated orthogonal directions.
Hints
- Check the directions have zero dot product. Nonorthogonal directions can count the same component twice.
import jax.numpy as jnp
def practice_use_two_perpendicular_directions(vector, first_direction, second_direction):
"""Implement the use two perpendicular directions check."""
# TODO: Compute each orthogonal projection, add the two components, and measure the reconstruction residual.
raise NotImplementedErrorShow a hint
Check the directions have zero dot product. Nonorthogonal directions can count the same component twice.
Reveal reference solution and reasoning
# Use two perpendicular directions (Transfer / diagnosis)
a1 = jnp.array([1.0, 1.0])
a2 = jnp.array([1.0, -1.0])
assert jnp.allclose(project(u, a1) + project(u, a2), u)
assert not jnp.allclose(project(u, a) + project(u, a1), u)Adding separate projections reconstructs a vector in an orthogonal basis. Correlated feature directions require a joint least-squares solve.
Diagnose the result
If the leftover is not perpendicular, check the denominator: it is , not the length of . If cosine exceeds its expected range, check normalization and rounding; first reject zero-length inputs.
Carry forward
- A line does not change when its nonzero direction is rescaled. A dot product alone does change.
- Changing direction changes what can be explained; merely changing its length does not.
Keep your evidence
Keep the hand-computed projection, perpendicular-residual check, rescaling experiment, zero-direction repair and changed-basis calculation.
Save your modified code, its output, and an explanation of your prediction. Checkpoint answers and exercise evidence are tracked separately.
Saved in this browser. Export or back up your portfolio anytime in My learning.
Reference execution: CPU validation only. This worked reference is validated on CPU. Accelerator compatibility requires a separate source review and target execution receipt. Use the execution matrix to review device selection and data ownership before adapting a separate target program. Execution matrix and target qualification.