Multi-Layer Networks

This Lesson Will Cover
  • Single-Layer Neural Networks
  • Non-Linear Data Modeling
  • XOR Problem
  • Two-Layer Architecture
  • Multi-Layer Networks
  • Forward Propagation
  • Matrix Operations

Single-Layer Architecture

Recall that a single perceptron computes the dot product:

z=w⋅x+b=∑i=1nwixi+b z = \mathbf{w} \cdot \mathbf{x} + b = \sum_{i=1}^n w_i x_i + b

where zz is a single value, which is passed through an activation function f(z)f(z).

Perceptron Architecture Diagram
Perceptron architecture diagram for an n-dimensional input vector.

We can generalize this to a neural-network layer with mm nodes:

z=W⋅x+b \mathbf{z} = \mathbf{W} \cdot \mathbf{x} + \mathbf{b}

where:

  • x\mathbf{x} is the input vector of dimension (n×1)(n \times 1)
  • W\mathbf{W} is a matrix of weights with dimensions (m×n)(m \times n)
  • b\mathbf{b} is a vector of biases with dimension (m×1)(m \times 1)
  • z\mathbf{z} is the output vector of dimension (m×1)(m \times 1)

The activation function is then applied element-wise to each component of z\mathbf{z}:

a=f(z) \mathbf{a} = f(\mathbf{z})
Multi-Node Perceptron Architecture Diagram
A single neural network layer with m nodes for an n-dimensional input vector.

Modeling Non-Linear Data: The XOR Problem

The XOR problem can be used to illustrate how it is possible for a simple neural network to model non-linearly separable data[1].

Non-Linear Activation

An example of a non-linear activation function is the ReLU (Rectified Linear Unit):

f(z)=max⁡(0,z) f(\mathbf{z}) = \max(0, \mathbf{z})

This function outputs zero for all negative inputs and otherwise returns the input value.

e.g. given z=[−12−34]z=\begin{bmatrix} -1 \\ 2 \\ -3 \\ 4 \end{bmatrix}, ReLU would output [0204]\begin{bmatrix} 0 \\ 2 \\ 0 \\ 4 \end{bmatrix}

XOR problem

The XOR problem has four inputs and corresponding outputs:

x1x_1 x2x_2 yy
0 0 0
0 1 1
1 0 1
1 1 0

Let’s model this with a multi-layer perceptron.

Architecture Design

Input Layer:

Two inputs x1x_1 and x2x_2.

First (Hidden) Layer:

  • Two neurons h1h_1 and h2h_2.
  • Activation function function: ReLU.

Output Layer:

  • One neuron yy (output).
  • Activation function: Step function (to output a binary classification).
Two-Layer XOR Neural Network Architecture
Two-layer (or "one hidden layer") XOR neural network architecture.

Network Equations

First Layer (Hidden Layer):

h1=ReLU(w11x1+w12x2+b1)h2=ReLU(w21x1+w22x2+b2) h_1 = \text{ReLU}(w_{11}x_1 + w_{12}x_2 + b_1) \\[5pt] h_2 = \text{ReLU}(w_{21}x_1 + w_{22}x_2 + b_2)

Where:

  • w11,w12,w21,w22w_{11}, w_{12}, w_{21}, w_{22} are weights.
  • b1,b2b_1, b_2 are biases.

Second Layer (Output Layer):

y=step ⁣(w31h1+w32h2+b3) y = \text{step}\!\bigl(w_{31}h_1 + w_{32}h_2 + b_3\bigr)

Where:

  • step(z)={1if z≥00otherwise\text{step}(z)=\begin{cases}1 & \text{if } z\ge 0\\[4pt]0 & \text{otherwise}\end{cases}
  • w31,w32w_{31}, w_{32} are weights for connections from the hidden layer to the output layer.
  • b3b_3 is the bias for the output layer.

Step-by-Step Walkthrough

  1. Input:
    Let’s take an input x1=1,x2=0x_1 = 1, x_2 = 0.
Input Layer - Two-Layer XOR Neural Network Architecture
Input layer of the two-layer XOR.
  1. First Layer Computations (Hidden Layer):
    Using ReLU (ReLU(z)=max⁡(0,z)\text{ReLU}(z) = \max(0, z)):

    h1=ReLU(w11⋅1+w12⋅0+b1)=ReLU(w11+b1)h2=ReLU(w21⋅1+w22⋅0+b2)=ReLU(w21+b2) h_1 = \text{ReLU}(w_{11} \cdot 1 + w_{12} \cdot 0 + b_1) = \text{ReLU}(w_{11} + b_1) \\[5pt] h_2 = \text{ReLU}(w_{21} \cdot 1 + w_{22} \cdot 0 + b_2) = \text{ReLU}(w_{21} + b_2)

    If w11+b1>0w_{11} + b_1 > 0 and w21+b2>0w_{21} + b_2 > 0, both neurons in the hidden layer activate.

Hidden Layer 1 - Two-Layer XOR Neural Network Architecture
Hidden layer of the two-layer XOR.
  1. Second Layer Computation (Output Layer):
    Using Step Function:

    y=step(w31h1+w32h2+b3) y = \text{step}(w_{31}h_1 + w_{32}h_2 + b_3)

    Here, h1h_1 and h2h_2 are outputs from the hidden layer. The step function outputs 1 if input ≥ 0, and 0 otherwise.

Output Layer - Two-Layer XOR Neural Network Architecture
Output layer of the two-layer XOR.
  1. Final Output:

    The step function directly produces the binary output 00 or 11.

Example Weights and Biases (XOR Solution)

Let’s consider some weights and biases that solve XOR:

First Layer:

w11=1,w12=1,b1=−0.5w21=1,w22=1,b2=−1.5 w_{11} = 1, w_{12} = 1, b_1 = -0.5 \\[5pt] w_{21} = 1, w_{22} = 1, b_2 = -1.5

These weights create two linear regions in the hidden layer:

  • h1h_1 activates when x1+x2>0.5x_1 + x_2 > 0.5.
  • h2h_2 activates when x1+x2>1.5x_1 + x_2 > 1.5.

Second Layer:

w31=1,w32=−3,b3=−0.25 w_{31} = 1, w_{32} = -3, b_3 = -0.25

These corrected output weights and bias give XOR outputs 0,1,1,00, 1, 1, 0 for inputs (0,0),(1,0),(0,1),(1,1)(0,0), (1,0), (0,1), (1,1) under the stated step-function convention. Use the values here when working through the recorded example.

Verification with XOR Inputs

Input x1=0,x2=0x_1 = 0, x_2 = 0

h1=ReLU(0+0−0.5)=0h2=ReLU(0+0−1.5)=0y=step(1⋅0−3⋅0−0.25)=step(−0.25)=0 h_1 = \text{ReLU}(0 + 0 - 0.5) = 0 \\[5pt] h_2 = \text{ReLU}(0 + 0 - 1.5) = 0 \\[5pt] y = \text{step}(1 \cdot 0 - 3 \cdot 0 - 0.25) = \text{step}(-0.25) = 0

Input x1=1,x2=0x_1 = 1, x_2 = 0

h1=ReLU(1+0−0.5)=0.5h2=ReLU(1+0−1.5)=0y=step(1⋅0.5−3⋅0−0.25)=step(0.25)=1 h_1 = \text{ReLU}(1 + 0 - 0.5) = 0.5 \\[5pt] h_2 = \text{ReLU}(1 + 0 - 1.5) = 0 \\[5pt] y = \text{step}(1 \cdot 0.5 - 3 \cdot 0 - 0.25) = \text{step}(0.25) = 1

Input x1=0,x2=1x_1 = 0, x_2 = 1

h1=ReLU(0+1−0.5)=0.5h2=ReLU(0+1−1.5)=0y=step(1⋅0.5−3⋅0−0.25)=step(0.25)=1 h_1 = \text{ReLU}(0 + 1 - 0.5) = 0.5 \\[5pt] h_2 = \text{ReLU}(0 + 1 - 1.5) = 0 \\[5pt] y = \text{step}(1 \cdot 0.5 - 3 \cdot 0 - 0.25) = \text{step}(0.25) = 1

Input x1=1,x2=1x_1 = 1, x_2 = 1

h1=ReLU(1+1−0.5)=1.5h2=ReLU(1+1−1.5)=0.5y=step(1⋅1.5−3⋅0.5−0.25)=step(−0.25)=0 h_1 = \text{ReLU}(1 + 1 - 0.5) = 1.5 \\[5pt] h_2 = \text{ReLU}(1 + 1 - 1.5) = 0.5 \\[5pt] y = \text{step}(1 \cdot 1.5 - 3 \cdot 0.5 - 0.25) = \text{step}(-0.25) = 0

As we can see, our neural network was able to model the XOR problem!

Multi-Layer Neural Networks

The generalized mathematical representation of a multi-layer neural network is pretty abstract, so let’s keep it simple for now with an example that we can easily imagine generalizing to bigger nodes and more layers.

Example: Forward Propagation for a Multi-layer Neural Network

We’ll consider a multi-layer network for binary classification as follows:

  • Input Layer: 5-dimensional input vector.
  • Hidden Layer: 1: 4-dimensional, activation is ReLU.
  • Hidden Layer: 2: 7-dimensional, activation is ReLU.
  • Output Layer: 2-dimensional, activation is Step function.
Multi-layer Neural Network with two ReLU for Binary Classification Architecture
Multi-layer neural network architecture diagram for our binary classification example.

We can visualize our network as follows:

Input Layer (x)[x1x2x3x4x5]→W1,b1,ReLU[a1[1]a2[1]a3[1]a4[1]]→W2,b2,Step[a1[2]a2[2]a3[2]a4[2]a5[2]a6[2]a7[2]]→W3,b3[y1y2] \begin{aligned} &\text{Input Layer (x)} \\ &\begin{bmatrix} x_1 \\ x_2 \\ x_3 \\ x_4 \\ x_5 \end{bmatrix} \quad \xrightarrow{W_1, b_1, \text{ReLU}} \quad \begin{bmatrix} a_1^{[1]} \\ a_2^{[1]} \\ a_3^{[1]} \\ a_4^{[1]} \end{bmatrix} \quad \xrightarrow{W_2, b_2, \text{Step}} \quad \begin{bmatrix} a_1^{[2]} \\ a_2^{[2]} \\ a_3^{[2]} \\ a_4^{[2]} \\ a_5^{[2]} \\ a_6^{[2]} \\ a_7^{[2]} \end{bmatrix} \quad \xrightarrow{W_3, b_3} \quad \begin{bmatrix} y_1 \\ y_2 \end{bmatrix} \end{aligned}

Here are the forward propagation equations, which model how input data (e.g., a feature vector to make a prediction for) pass through the network:

Input Layer - Multi-layer Neural Network with two ReLU for Binary Classification Architecture
Input layer of our example neural network architecture.

Starting from the 5-dimensional input layer x\mathbf{x}, we calculate:

z[1]=W[1]x+b[1]=[w11[1]w12[1]w13[1]w14[1]w15[1]w21[1]w22[1]w23[1]w24[1]w25[1]w31[1]w32[1]w33[1]w34[1]w35[1]w41[1]w42[1]w43[1]w44[1]w45[1]][x1x2x3x4x5]+[b1[1]b2[1]b3[1]b4[1]] \mathbf{z}^{[1]} = \mathbf{W}^{[1]}\mathbf{x} + \mathbf{b}^{[1]} = \begin{bmatrix} w_{11}^{[1]} & w_{12}^{[1]} & w_{13}^{[1]} & w_{14}^{[1]} & w_{15}^{[1]} \\ w_{21}^{[1]} & w_{22}^{[1]} & w_{23}^{[1]} & w_{24}^{[1]} & w_{25}^{[1]} \\ w_{31}^{[1]} & w_{32}^{[1]} & w_{33}^{[1]} & w_{34}^{[1]} & w_{35}^{[1]} \\ w_{41}^{[1]} & w_{42}^{[1]} & w_{43}^{[1]} & w_{44}^{[1]} & w_{45}^{[1]} \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \\ x_3 \\ x_4 \\ x_5 \end{bmatrix} + \begin{bmatrix} b_1^{[1]} \\ b_2^{[1]} \\ b_3^{[1]} \\ b_4^{[1]} \end{bmatrix}
Hidden Layer 1 - Multi-layer Neural Network with two ReLU for Binary Classification Architecture
Hidden layer 1 of our example neural network architecture.

We pass z[1]\mathbf{z^{[1]}} through an activation layer in order to get layer 1, a 4-dimensional vector denoted as a[1]\mathbf{a^{[1]}}:

a[1]=ReLU(z[1]) \mathbf{a}^{[1]} = \text{ReLU}(\mathbf{z}^{[1]})
Hidden Layer 2 - Multi-layer Neural Network with two ReLU for Binary Classification Architecture
Hidden layer 2 of our example neural network architecture.

Next, we continue with forward propagation from layer 1 (the first hidden layer) in order to get layer 2, a 7-dimensional vector denoted as a[2]\mathbf{a^{[2]}}:

z[2]=W[2]a[1]+b[2]=[w11[2]w12[2]w13[2]w14[2]w21[2]w22[2]w23[2]w24[2]w31[2]w32[2]w33[2]w34[2]w41[2]w42[2]w43[2]w44[2]w51[2]w52[2]w53[2]w54[2]w61[2]w62[2]w63[2]w64[2]w71[2]w72[2]w73[2]w74[2]][a1[1]a2[1]a3[1]a4[1]]+[b1[2]b2[2]b3[2]b4[2]b5[2]b6[2]b7[2]] \mathbf{z}^{[2]} = \mathbf{W}^{[2]}\mathbf{a}^{[1]} + \mathbf{b}^{[2]} = \begin{bmatrix} w_{11}^{[2]} & w_{12}^{[2]} & w_{13}^{[2]} & w_{14}^{[2]} \\ w_{21}^{[2]} & w_{22}^{[2]} & w_{23}^{[2]} & w_{24}^{[2]} \\ w_{31}^{[2]} & w_{32}^{[2]} & w_{33}^{[2]} & w_{34}^{[2]} \\ w_{41}^{[2]} & w_{42}^{[2]} & w_{43}^{[2]} & w_{44}^{[2]} \\ w_{51}^{[2]} & w_{52}^{[2]} & w_{53}^{[2]} & w_{54}^{[2]} \\ w_{61}^{[2]} & w_{62}^{[2]} & w_{63}^{[2]} & w_{64}^{[2]} \\ w_{71}^{[2]} & w_{72}^{[2]} & w_{73}^{[2]} & w_{74}^{[2]} \end{bmatrix} \begin{bmatrix} a_1^{[1]} \\ a_2^{[1]} \\ a_3^{[1]} \\ a_4^{[1]} \end{bmatrix} + \begin{bmatrix} b_1^{[2]} \\ b_2^{[2]} \\ b_3^{[2]} \\ b_4^{[2]} \\ b_5^{[2]} \\ b_6^{[2]} \\ b_7^{[2]} \end{bmatrix}
a[2]=ReLU(z[2]) \mathbf{a}^{[2]} = \text{ReLU}(\mathbf{z}^{[2]})
Output Layer - Multi-layer Neural Network with two ReLU for Binary Classification Architecture
Output layer of our example neural network architecture.

Finally, we do forward propagation one more time, from layer 2 (the second hidden layer), in order to get layer 3, a 2-dimensional vector denoted as a[3]\mathbf{a^{[3]}}:

z[3]=W[3]a[2]+b[3]=[w11[3]w12[3]w13[3]w14[3]w15[3]w16[3]w17[3]w21[3]w22[3]w23[3]w24[3]w25[3]w26[3]w27[3]][a1[2]a2[2]a3[2]a4[2]a5[2]a6[2]a7[2]]+[b1[3]b2[3]] \mathbf{z}^{[3]} = \mathbf{W}^{[3]}\mathbf{a}^{[2]} + \mathbf{b}^{[3]} = \begin{bmatrix} w_{11}^{[3]} & w_{12}^{[3]} & w_{13}^{[3]} & w_{14}^{[3]} & w_{15}^{[3]} & w_{16}^{[3]} & w_{17}^{[3]} \\ w_{21}^{[3]} & w_{22}^{[3]} & w_{23}^{[3]} & w_{24}^{[3]} & w_{25}^{[3]} & w_{26}^{[3]} & w_{27}^{[3]} \end{bmatrix} \begin{bmatrix} a_1^{[2]} \\ a_2^{[2]} \\ a_3^{[2]} \\ a_4^{[2]} \\ a_5^{[2]} \\ a_6^{[2]} \\ a_7^{[2]} \end{bmatrix} + \begin{bmatrix} b_1^{[3]} \\ b_2^{[3]} \end{bmatrix}
a[3]=y=step(z[3]) \mathbf{a}^{[3]} = \mathbf{y} = \text{step}(\mathbf{z}^{[3]})

Having passed through the layer 3 activation function (a step function), we get the final output y\mathbf{y}, which in our case contains binary values (0 or 1), representing the binary classification decision.

Generalized Forward Propagation

Having seen this example, the generalized expressions for forward propagation will be easy to understand:

For a neural network with LL layers, the generalized forward propagation equations are:

For the first layer (l=1)(l=1):

z[1]=W[1]x+b[1]a[1]=f[1](z[1]) \mathbf{z}^{[1]} = \mathbf{W}^{[1]}\mathbf{x} + \mathbf{b}^{[1]} \\ \mathbf{a}^{[1]} = f^{[1]}(\mathbf{z}^{[1]})

For subsequent layers (l=2,…,L)(l=2,\ldots,L):

z[l]=W[l]a[l−1]+b[l]a[l]=f[l](z[l]) \mathbf{z}^{[l]} = \mathbf{W}^{[l]}\mathbf{a}^{[l-1]} + \mathbf{b}^{[l]} \\ \mathbf{a}^{[l]} = f^{[l]}(\mathbf{z}^{[l]})

Where:

  • W[l]\mathbf{W}^{[l]} is the weight matrix for layer ll
  • b[l]\mathbf{b}^{[l]} is the bias vector for layer ll
  • z[l]\mathbf{z}^{[l]} is the pre-activation output for layer ll
  • a[l]\mathbf{a}^{[l]} is the post-activation output for layer ll
  • f[l]f^{[l]} is the activation function for layer ll
  • x\mathbf{x} is the input vector
  • a[L]\mathbf{a}^{[L]} is the final output of the network

Conclusion

When I was drafting this lesson, I started with the generalized formulas for expressing a multi-layer neural network, and quickly realized how difficult they were to understand without explicitly walking through an example.

I love learning through examples. In fact, I need them. I usually can’t grasp abstract concepts otherwise.

Key Takeaways

🧠 Single-layer neural networks can only model linearly separable data.

❌ The XOR problem demonstrates the limitations of single-layer networks.

🔄 Multi-layer networks with non-linear activation functions can model complex non-linear relationships.

➡️ Forward propagation involves matrix multiplication and activation functions at each layer.

🔍 Each layer transforms the data into new representations that help solve the problem.

📏 The dimensions of weight matrices must match the dimensions of the connected layers.

⚡ ReLU and step functions are examples of non-linear activation functions used in neural networks.

Next: Backpropagation

We’ll carry the example from this lesson forward into the next one, as we continue to learn about the architecture of a multi-layer neural network.

Next up: backpropagation.

Footnotes


  1. I made significant changes to the “Modeling Non-Linear Data” section (August 2025), removing a confusing example of a single-layer neural network with no activation (i.e., a single layer of perceptrons that collectively output a vector). The audio may still reflect the prior version. ↩︎

Comments (0)

No comments yet.