Layer Composition

An MLP composes layers.

For a one-hidden-layer MLP:

h = relu(xW1 + b1)
scores = hW2 + b2

Read this from left to right:

  1. The input x enters the first affine map.
  2. ReLU transforms the first layer's output into hidden activations h.
  3. The second affine map turns h into output scores.

The whole model is still one function:

scores = f(x; W1, b1, W2, b2)

But internally it has stages.

Shape trace

Suppose:

x shape:  (3)
W1 shape: (3, 4)
b1 shape: (4)
W2 shape: (4, 2)
b2 shape: (2)

Then:

h shape:      (4)
scores shape: (2)

The hidden layer changes the representation from 3 input features to 4 hidden features. The output layer changes 4 hidden features to 2 scores.

DL-C05-T03-001Exercise: Hidden shape

For one example, x has shape (5) and W1 has shape (5, 7). What is the shape size of h = relu(xW1 + b1)?

Compute it first, then check your number.

HintUse the output side of W1

(5) x (5, 7) produces (7).

SolutionWork it out

Multiplying a (5) input by a (5, 7) weight matrix produces 7 hidden values. ReLU keeps the shape, so h has shape (7).

DL-C05-T03-002Exercise: Output score shape

Continuing from h shape (7), if W2 has shape (7, 3), how many output scores are produced?

Compute it first, then check your number.

HintUse W2 output dimension

(7) x (7, 3) produces (3).

SolutionWork it out

The hidden vector has 7 values. Multiplying by W2 with shape (7, 3) produces 3 output scores.