Matrices in Neural Network Layers
A basic neural network layer uses a matrix.
One common form is:
Read it as:
multiply input x by weight matrix W
add bias b
apply activation function sigma
Shapes
If:
x has length 3
W has shape 2 x 3
b has length 2
then:
Wx has length 2
Wx + b has length 2
h has length 2
The weight matrix controls how input features are mixed into output features.
Read each output coordinate as one learned feature detector. One row of decides how to mix the input features for that output coordinate. The bias then shifts that coordinate before the activation function changes it.
If has length 4 and has shape 6 x 4, what is the length of ?
Compute it first, then check your number.
HintUse the rows
The output length is the number of rows of .
SolutionOutput length
The input length 4 matches the number of columns of . The output length
is the number of rows: 6.
Rows Become Output Features
Each row of computes one weighted sum of the input.
For example, if:
and:
then the first entry of is:
The second entry is:
So:
For the and above, what is the first entry of ?
Compute it first, then check your number.
HintUse the first row
Compute .
SolutionFirst weighted sum
This is one pre-activation value. The first row of chooses how to mix the three input features for the first output coordinate.
Bias And Activation
The matrix product gives pre-activation values.
The bias shifts those values. The activation function then changes them coordinate by coordinate.
If:
then:
If and , what is the second entry of ?
Compute it first, then check your number.
HintAdd matching entries
Use the second coordinate of both vectors.
SolutionBias addition
The second entry is 12. Bias addition is coordinate-wise: the second bias
shifts the second pre-activation from 14 down by 2.
Why The Matrix Matters
This pattern is small, but it appears throughout deep learning.
Transformers use many matrix multiplications: projections, feed-forward layers, attention scores, and output logits all rely on the same shape discipline.
The names change, but the reading habit remains:
check the shape
read rows as weighted sums
track the output length
Enter 1 if a length-5 input can multiply a weight matrix with shape
3 x 4, or 0 if it cannot.
Compute it first, then check your number.
HintCheck columns
A 3 x 4 matrix expects a length-4 input vector.
SolutionInput mismatch
Enter 0. A 3 x 4 matrix has 4 columns, so it expects a length-4
input. A length-5 input does not fit.
Next, we close the chapter and collect the important rules.