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:
- The input
xenters the first affine map. - ReLU transforms the first layer's output into hidden activations
h. - The second affine map turns
hinto 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.
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).
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.