0.2 NumPy and PyTorch tensors
1. Learn
Section titled “1. Learn”- NumPy: the absolute basics for beginnersall sections up to "Working with mathematical formulas"
- NumPy user guide: Broadcastingwhole page, especially "General broadcasting rules"
- NumPy user guide: Indexing on ndarrays"Basic indexing" and "Integer array indexing"
- PyTorch: Learn the Basics, Tensorswhole page; note how every NumPy idea maps across
2. Bridge
Section titled “2. Bridge”Almost every bug in ML code is a shape bug, so this module is about shapes.
An array has a shape (a tuple, one entry per axis) and a dtype. X.shape == (n, d) means n rows of d features. Reductions take an axis: X.mean(axis=0) averages down the rows and leaves shape (d,). Add keepdims=True to keep that axis as length 1 so the result still broadcasts against X (NumPy basics).
Broadcasting lets arrays of different shapes combine without copying. NumPy lines the shapes up from the right. Each pair of sizes must be equal, or one of them must be 1 (which stretches) (broadcasting rules). So (n, d) - (d,) subtracts a vector from every row, and (n, 1, d) - (1, m, d) gives all n × m pairwise differences, shape (n, m, d). That one trick replaces most double loops.
Indexing with integer arrays picks elements, not slices: S[np.arange(n), idx] takes one entry per row, S[i, idx[i]] for every i. This is exactly how a classification loss picks out each example’s correct-class score (indexing). Elementwise product elementwise productMultiply matching entries: (a ⊙ b)_i = a_i b_i. NumPy writes a * b.Introduced in 0.2 NumPy and PyTorch tensors. is a * b, while matrix product is a @ b.
PyTorch tensors follow the same rules and nearly the same names (torch.zeros, .shape, .sum(dim=0)). Two differences matter from Part 3 on: tensors live on a device (.to("cuda")), and they can record operations for automatic differentiation. torch.from_numpy shares memory with the NumPy array, so changing one changes the other (PyTorch tensors). The exercises here are NumPy only, so they run in your browser. Everything transfers.
Habit to build now: before running a line, say its output shape out loud. The tracer below checks you.
3. Explore
Section titled “3. Explore”A scratch cell for your own experiments (cells on a page share variables):
a = np.arange(3) # shape (3,) b = np.arange(4)[:, None] # shape (4, 1) (a + b).shape
Tab indents · Esc then Tab leaves the editor · Shift+Enter runs
Seven drills, no Python loops allowed. Each one is a pattern you will use again.
stubs/m0_2_tensors.pyLoading starter file…
Prefer your own editor? Edit exercises/stubs/m0_2_tensors.py and run mlc check 0.2. The tests are identical; import the results on the progress page.
Locally: mlc check 0.2.
5. Check
Section titled “5. Check”- not yet: All tests pass (0/?)
- not yet: Checkpoint quiz ≥ 80%
Learner log: time spent and points of confusion
Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.