Syllabus Lesson 10 of 246 · Module 1: Python for this course
Module 1: Python for this course

Shapes: rows, columns, batches

Why it matters Most bugs in neural network code are shape bugs. If you can predict a shape before running the line, you will find them fast.

Every array has a .shape: a tuple giving its size along each direction. A list of 5 numbers has shape (5,). A table of 3 rows and 4 columns has shape (3, 4). A batch of 32 images, each 28 by 28 pixels, has shape (32, 28, 28).

Tiny numbers

X = np.zeros((3, 4))
X.shape          # (3, 4)
X[0].shape       # (4,)   one row
X[:, 0].shape    # (3,)   one column
X.T.shape        # (4, 3) transposed
X.sum(axis=0).shape  # (4,)  summed down the rows: one total per column

Adding a direction with None

Indexing with None inserts a new direction of size 1 at that spot, without changing any numbers. For a vector p of shape (5,), p[:, None] has shape (5, 1): a column. That is how you multiply each row of a (5, 3) table by its own number: p[:, None] * X, because the size-1 direction gets copied across the 3 columns. Likewise p[None, :] is a row, shape (1, 5).

axis: which direction an operation runs along. axis=0 runs down the rows (one result per column); axis=1 runs along each row (one result per row).

Recap Say the shape out loud before you run the line.

Words and symbols in this lesson
array
a NumPy block of numbers, all the same type, that does maths on every item at once
shape
the size of an array along each direction, for example (32, 64) is 32 rows of 64
axis
which direction an operation runs along: axis=0 down the rows, axis=1 along each row
mean, spread
the average, and roughly how far values sit from it (standard deviation)
Your turn

Predict, without running them, the shape of each expression below for X = np.zeros((6, 3)) and w = np.zeros(3). Fill in predictions with tuples. Then run to check yourself.

  1. X[2]
  2. X[:, 1]
  3. X.T
  4. X.sum(axis=0)
  5. X.sum(axis=1)
  6. X @ w
  7. X[1:4]
  8. X.reshape(3, 6)
  9. X[:, None, :]
  10. X.mean() (use ())

Spotted a problem in this lesson? Report it · Ask about this lesson

This exercise opens soon

Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.