The Dense (Fully Connected) Layer

Intuition

A dense layer is many linear models side by side: each output unit is a weighted sum of all inputs plus a bias. For a batch of inputs this is one matrix product.

Precise explanation

Input XX (n, d_in), weights WW (d_in, d_out), bias bb (d_out,):

Y=XW+bshape (n,dout),Y = XW + b \quad\text{shape } (n, d_\text{out}),

with bb broadcast over rows (NP-07). Parameters: dindout+doutd_\text{in} d_\text{out} + d_\text{out}. For layer sizes [d0,d1,…,dL][d_0, d_1, \dots, d_L] the total is ∑l(dl−1+1)dl\sum_l (d_{l-1}+1)d_l.

Initialisation: all-zero weights make every unit identical forever (symmetry). He initialisation for ReLU networks draws W∼N(0,2/din)W \sim \mathcal N(0, 2/d_\text{in}) so activations keep roughly constant variance across layers; biases start at 0.

Worked example

X = [[1, 2]], W = [[1, 0, −1], [2, 1, 0]], b = [0.5, 0, 0] → Y = [[5.5, 2, −1]]. Layers [784, 128, 10]: (784+1)·128 + (128+1)·10 = 100 480 + 1 290 = 101 770 parameters.

Common mistakes

  • W @ X instead of X @ W (row-vector convention used here).
  • Bias with shape (n,) instead of (d_out,).
  • Initialising all weights to the same constant.

Why this matters

Dense layers make up MLPs (DL-07), transformer feed-forward blocks and classifier heads.

References