Bitpath

Boss lesson · Unit 01

A layer is a matrix

Shapes, matrix products, and one neural-network layer

13 exercisesabout 3 min

What you practise

  • Shapes, matrix-vector and matrix-matrix products, the shape rule (m×n times n×p), transpose, identity
  • A neural-network layer as W @ x + b

Try a question from this lesson

Pick one

A network layer turns 3 input numbers into 2 outputs. Each output is its own weighted sum of all 3 inputs. How many weights does the layer hold?

Hint

One output needs one weight per input. How many outputs are there?

The ideas

Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.

Concept

A matrix is a table of numbers

A matrix has rows and columns. Its shape is written (rows, columns), rows first.

Here W has shape (2, 3): 2 rows of 3 numbers. Each row holds the weights for one output of the layer.

W[1] is the second row (counting starts at 0). W[1, 2] is one number: row 1, column 2.

Python
import numpy as np

W = np.array([[1, 0, 2],
              [0, 3, 1]])
print(W.shape)   # (2, 3)
print(W[1])      # [0 3 1]
print(W[1, 2])   # 1

Concept

The shape rule

Two matrices can be multiplied when the inner sizes match:

(m, n) @ (n, p) gives (m, p).

The two inner numbers must be equal, and they vanish. The outer numbers survive. A vector of size n fits the same rule: (m, n) @ (n,) gives (m,).

When NumPy complains about shapes, write them side by side and look at the inner pair.

Python
import numpy as np

A = np.ones((2, 3))
B = np.ones((3, 4))
print((A @ B).shape)   # (2, 4)
# B @ A fails: inner pair is 4 and 2

10 more exercises in the app

  • 4× type the answer
  • 3× multiple choice
  • 1× fill the blanks
  • 1× true or false
  • 1× order the lines

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy