← Deep learning course

Deep learning / Lesson 03 of 06 / 30–40 min

Meet PyTorch & tensors

Set up a real PyTorch notebook and learn to read tensor shapes without guessing.

By the end

Run real PyTorch on a CPU and represent examples, features, weights, and targets as tensors.

Step 1

Start with a real Python environment

Use Open in Colab above for the ready-to-run notebook. Sign in if prompted, connect to a runtime, and run the code cells from top to bottom with Shift + Enter. The default CPU is enough; no paid GPU or API key is needed for these examples. Colab availability and usage limits are controlled by Google. Save a copy to keep your changes.

Prefer your own computer? Follow the linked PyTorch installation guide for your operating system, using a virtual environment and the CPU option where offered. Download this lesson's Python file and run it with that environment's Python. In an existing notebook where import torch fails, run %pip install torch in a separate code cell, restart the kernel if asked, and run the lesson again.

Room310's standard Python cells do not include PyTorch. Do not paste these examples into the ordinary assignment runner. The complete lesson notebook includes all previous steps; it does not depend on another lesson's kernel state.

PyTorch · run in your notebook
import torch

torch.manual_seed(7)
print("PyTorch:", torch.__version__)
print("A tensor on:", torch.tensor([1.0]).device)

Step 2

A tensor is an array with a shape

A tensor stores numbers with a shape. A scalar has no axes; a vector has one; a matrix has two. For our course, rows represent examples and columns represent features.

Here we have three examples, each with two features, so X.shape is [3, 2]. The single target for each example is stored as [3, 1]. Keep the column dimension: mixing predictions of shape [3, 1] with targets of shape [3] can accidentally broadcast into a [3, 3] calculation.

Use floating-point tensors for these inputs and targets. Not every task uses float targets—multiclass classification with CrossEntropyLoss usually uses integer class indices—but our binary examples will use floats.

PyTorch · run in your notebook
X = torch.tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
y = torch.tensor([[0.0], [1.0], [1.0]])

print("Input shape:", X.shape)
print("Target shape:", y.shape)
print("Input dtype:", X.dtype)
print("First example:", X[0])

Step 3

Run the same neuron on a whole batch

The @ operator is matrix multiplication, not element-by-element multiplication. [3, 2] @ [2, 1] produces [3, 1]: three predictions. The inner dimensions must match. This calculation replaces a Python loop over neurons and examples.

The scalar bias is broadcast across the three predictions. Broadcasting is convenient when intentional, but always inspect your shapes before relying on it.

PyTorch · run in your notebook
weights = torch.tensor([[0.5], [1.0]])
bias = torch.tensor(0.25)
predictions = X @ weights + bias

print("Predictions:", predictions)
assert predictions.shape == y.shape
print("One prediction per target:", predictions.shape)

Your turn

Practice & explain

Use the practice section at the end of this lesson's Colab notebook, or work in your local PyTorch environment. Keep a copy of your changes.

3.1 · Add an example

Add [7.0, 8.0] to X and one target row to y. Rerun the batch calculation. Write down each shape and the new prediction before checking it.

Need a hint?

The weights stay [2, 1] because the number of features has not changed. The new prediction is 11.75.

3.2 · Catch a shape bug

Make bad_targets = torch.tensor([0.0, 1.0, 1.0]) for the original three examples. Compare its shape with y.shape. Explain why assert predictions.shape == bad_targets.shape should fail.

Need a hint?

A length-three vector and a three-row column matrix are different shapes. Use bad_targets.reshape(-1, 1) to make a column.

Keep exploring