Introduction to Deep Learning · HIT

Week 2   Part I · Foundations

Tensors & Data Representation

Tensor operations, shapes, broadcasting, devices; representing images, text, and tabular data as tensors.

Learning goals

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

  1. Implement a set of tensor manipulations (reshape, permute, broadcasting, reductions, indexing) with an AI assistant's help.
  2. Encode a small dataset (a few images and a small table) into tensors with the right dtype and device.

Part B · student reasoningPredict & probe

  1. Predict the output shape of the eight provided broadcasting and reshape expressions before running them.
  2. Predict which operations return a view versus copy memory.

Part C · in plain languageExplain & defend

  1. Explain the three expressions whose result shape is surprising, and why broadcasting produced it.
  2. Fix a seeded shape-mismatch bug and explain its root cause.

Deliverables

Hints.

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)

PreviousWeek 1: Deep Learning Overview & ML-to-Network FramingNextWeek 3: MLPs & Backpropagation