Deep Neural Networks

Lesson 2

Layers in a deep neural network

Input, hidden, and output, on one canonical i → h → o network

Layers in a deep neural network

In Lesson 1, one neuron was a single linear score plus an activation. A neural network is what you get when you stack many neurons into layers: raw inputs enter on one side, hidden layers build intermediate representations, and the output layer produces the prediction. Researchers including Geoffrey Hinton popularized the view that depth lets a network learn hierarchical features (simple patterns early, composed patterns deeper) without hand-crafting rules.

This whole course uses one canonical network so the symbols never change under you. It has exactly three layers:

  • Input layer: i neurons, the feature vector x.
  • One hidden layer: h neurons, activation a₁.
  • Output layer: o neurons, activation a₂ (the prediction).

The two weight matrices are W₁ (size i × h, input → hidden) and W₂ (size h × o, hidden → output), with bias vectors b₁ and b₂. Activations are sigmoid σ. This lesson is about fully connected (dense) layers: every neuron in one layer connects to every neuron in the next.


The three layer roles

LayerWhat it holdsLearnable part?
Input (i)Your features x (pixels, tabular values, embeddings…)No weights here, just the signal you feed in
Hidden (h)The activation a₁ = σ(W₁x + b₁), the network's internal representationYes, W₁, b₁ sit before it
Output (o)The prediction a₂ = σ(W₂a₁ + b₂)Yes, W₂, b₂ sit before it

Input = where the story starts; output = what you score against the label; hidden = everything in between that makes the model more than one linear map.


Visual: the canonical fully connected stack

Each column is a layer; each pill is one neuron. In a dense net, each pill connects to all pills in the previous column (only a few lines are drawn so the picture stays readable; the rule is all-to-all).

Input · i

x₁x₂x₃

x (i = 3)

Hidden · h

h₁h₂h₃h₄

a₁ (h = 4)

Output · o

ŷ₁ŷ₂

a₂ (o = 2)

Fully connected: each violet neuron receives a weighted sum from all input neurons, and each output neuron from all hidden neurons.


Forward pass through the layers

The signal moves left to right: a linear step (weights and bias), then the sigmoid σ, once per layer (this is the full subject of Lesson 5):

z1=W1x+b1,a1=σ(z1)\mathbf{z}_1 = W_1\mathbf{x} + \mathbf{b}_1, \qquad \mathbf{a}_1 = \sigma(\mathbf{z}_1) z2=W2a1+b2,a2=σ(z2)\mathbf{z}_2 = W_2\mathbf{a}_1 + \mathbf{b}_2, \qquad \mathbf{a}_2 = \sigma(\mathbf{z}_2)

The hidden activation a₁ is literally the input to the output layer. That is what "stacking layers" means: the output of one layer is the input of the next.


Where weights and biases live

Parameters sit between layers, on the edges, not "inside" the circles (the circles are just values passing through):

  • W₁ (i × h) and b₁ (length h) connect input → hidden.
  • W₂ (h × o) and b₂ (length o) connect hidden → output.

So a fully connected layer from n_in neurons to n_out neurons holds

params=nin×nout+nout\text{params} = n_{\text{in}} \times n_{\text{out}} + n_{\text{out}}

(weights + one bias per output neuron).


Counting parameters (our i = 3, h = 4, o = 2 net)

ConnectionMatrixCount
Input 3 → Hidden 4W₁ (3×4), b₁ (4)3×4 + 4 = 16
Hidden 4 → Output 2W₂ (4×2), b₂ (2)4×2 + 2 = 10

Total for this tiny net: 16 + 10 = 26 trainable scalars. Wider layers and more layers multiply this quickly, that is capacity.

Count them yourself

Press Run to count the parameters of the canonical net, then change the sizes and watch the total grow:

Python
First run loads the Python runtime (~10 MB) — takes ~5–10 seconds. Subsequent runs are instant.

Going deeper: what depth and width buy

Our canonical net has one hidden layer. "Deep" just means stacking more hidden layers, repeating the same linear → σ block. Each extra hidden layer adds its own W and b, and the general pattern for layer is the same idea:

z()=W()a(1)+b(),a()=σ(z())\mathbf{z}^{(\ell)} = W^{(\ell)} \mathbf{a}^{(\ell-1)} + \mathbf{b}^{(\ell)}, \qquad \mathbf{a}^{(\ell)} = \sigma\bigl(\mathbf{z}^{(\ell)}\bigr)

More neurons per layer (width)

A bigger h gives more linear combinations at once, a richer slice of functions at each step.

More hidden layers (depth)

Compose nonlinearities into features of features. This is the heart of deep learning's hierarchy story.

Tradeoff: more parameters → more flexible but easier to overfit and costlier to train. The number and size of hidden layers is a design choice, not something the network learns.


Nonlinearity between layers

If you only stacked linear maps, the whole network would collapse to one linear map. The σ between layers breaks that: each hidden layer can bend the space so later layers see new directions. No nonlinearity in the hidden layers means no extra power from depth.

Activations ON — the network can bend space into curves.
Nonlinearity (σ between layers)
Hidden layers
Layer 14 n
Layer 23 n
0
params · 2 → 4 → 3 → 1

Toggle activations OFF and retrain: no matter how deep, the boundary snaps back to one straight line.


Hyperparameters vs learned weights

You set before training (hyperparameters)The optimizer learns from data
i, h, o, and how many hidden layersThe entries of W₁, b₁, W₂, b₂
Choice of activation, learning rate, batch sizeThe values that minimize the loss on examples

The activations a₁, a₂ change every step as the input changes; the sizes i, h, o are fixed unless you redesign the net.


Takeaway

IdeaOne line
Input layeri neurons, the features x you provide
Hidden layerh neurons, the learned vector a₁; W₁, b₁ sit before it
Output layero neurons, the prediction a₂; W₂, b₂ sit before it
DenseFull connectivity layer to layer
Parametersn_in × n_out + n_out per layer (here 26 total)
Depth / widthComplexity and cost knobs you set by hand

Next lessons train this stack: activations (Lesson 3), loss (Lesson 4), and the forward and backward passes (Lessons 5–6) on this exact i → h → o network.


Check your understanding

1 / 7

A dense (fully-connected) layer maps n_in = 5 inputs to n_out = 3 outputs. How many trainable parameters does it have?


Practice it yourself

Count the parameters and run the signal through the canonical i → h → o net — real Python, hidden tests, no setup.

Test your understanding

Prof is ready

Prof will ask you questions about Layers in a deep neural network — not explain it. You'll be surprised what you don't know until you have to say it.

Finished this lesson?

Read through the lesson first (0/20s).