Week 2 Part I · Foundations
Tensors & Data Representation
Tensor operations, shapes, broadcasting, devices; representing images, text, and tabular data as tensors.
Learning goals
- Manipulate tensors fluently (reshape, permute, reduce, index).
- Reason about shapes, broadcasting, and device placement.
- Encode real data into correctly shaped tensors.
This is the weekly
homework lab, completed independently after the lecture and the practice lesson. It follows the course's
Build / Predict & probe / Explain & defend model: use an AI assistant freely for the Build; the graded learning is in Predict and Explain. See the
AI-use policy and a
fully worked sample submission.
⚙Exercise
Part A · AI assistant welcomeBuild
- Implement a set of tensor manipulations (reshape, permute, broadcasting, reductions, indexing) with an AI assistant's help.
- Encode a small dataset (a few images and a small table) into tensors with the right dtype and device.
Part B · student reasoningPredict & probe
- Predict the output shape of the eight provided broadcasting and reshape expressions before running them.
- Predict which operations return a view versus copy memory.
Part C · in plain languageExplain & defend
- Explain the three expressions whose result shape is surprising, and why broadcasting produced it.
- Fix a seeded shape-mismatch bug and explain its root cause.
✓Deliverables
- A notebook with the operations and a predicted-versus-actual shape table.
- The fixed bug with a one-paragraph explanation.
Hints.- Print .shape and .dtype often; broadcasting aligns dimensions from the trailing axis.
- .view needs contiguous memory; .reshape may copy.
❓Self-check
Answer each before expanding it. If one is unclear, revisit the lab and the references.
State the broadcasting rule.
Shapes align from the trailing dimension; each pair of sizes must be equal or one of them 1.
What is the difference between .view and .reshape?
.view needs contiguous memory and shares storage; .reshape may copy.
What is the conventional shape of a batch of images in PyTorch?
(N, C, H, W): batch, channels, height, width.
How is a tensor moved to the GPU?
tensor.to(device), with device chosen from torch.cuda.is_available().
Why does a tensor's dtype matter?
Layers and losses expect specific types (e.g., float32 inputs, long class indices).
Instructor lesson plan (with references)