flowchart LR
X1["Input x₁"] --> S["Weighted sum"]
X2["Input x₂"] --> S
W["Weights"] --> S
B["Bias"] --> S
S --> Z["z = w₁x₁ + w₂x₂ + b"]
Z --> A["Step activation"]
A --> Y["Prediction ŷ"]
A worked example with a small dataset
By the end of this lesson, you should be able to:
A perceptron is a simple artificial neuron used for binary classification.
It receives inputs, multiplies them by weights, adds a bias, and applies an activation function.
\[ z=w_1x_1+w_2x_2+b \]
The step activation function is
\[ \hat{y}= \begin{cases} 1, & z\geq 0,\\ 0, & z<0. \end{cases} \]
flowchart LR
X1["Input x₁"] --> S["Weighted sum"]
X2["Input x₂"] --> S
W["Weights"] --> S
B["Bias"] --> S
S --> Z["z = w₁x₁ + w₂x₂ + b"]
Z --> A["Step activation"]
A --> Y["Prediction ŷ"]
The perceptron first computes a score, then converts it into a class.
We will learn the logical AND function.
| Sample | (x_1) | (x_2) | Target (y) |
|---|---|---|---|
| 1 | 0 | 0 | 0 |
| 2 | 0 | 1 | 0 |
| 3 | 1 | 0 | 0 |
| 4 | 1 | 1 | 1 |
Question: When should the output be 1?
Only when both inputs are 1.
Start with these parameters:
\[ w_1=0,\qquad w_2=0,\qquad b=0 \]
Choose the learning rate:
\[ \eta=1 \]
The learning rate determines how much the parameters change after an error.
We will process the samples in the order shown in the dataset.
For each training example:
The error is
\[ e=y-\hat{y} \]
The weight update rule is
\[ w_i^{\text{new}}=w_i^{\text{old}}+\eta e x_i \]
The bias update rule is
\[ b^{\text{new}}=b^{\text{old}}+\eta e \]
Input:
\[ x_1=0,\quad x_2=0,\quad y=0 \]
\[ z=(0)(0)+(0)(0)+0=0 \]
Since (z),
\[ \hat{y}=1 \]
\[ e=y-\hat{y}=0-1=-1 \]
The prediction is incorrect.
Update the first weight:
\[ w_1=0+(1)(-1)(0)=0 \]
Update the second weight:
\[ w_2=0+(1)(-1)(0)=0 \]
Update the bias:
\[ b=0+(1)(-1)=-1 \]
The new parameters are
\[ \boxed{w_1=0,\quad w_2=0,\quad b=-1} \]
Teaching point: The weights do not change because both input features are zero. Only the bias changes.
Input:
\[ x_1=0,\quad x_2=1,\quad y=0 \]
Using the updated parameters:
\[ z=(0)(0)+(0)(1)-1=-1 \]
Since (z<0),
\[ \hat{y}=0 \]
The error is
\[ e=0-0=0 \]
The prediction is correct, so no update is required.
Parameters remain
\[ w_1=0,\quad w_2=0,\quad b=-1 \]
Input:
\[ x_1=1,\quad x_2=0,\quad y=0 \]
Calculate the weighted sum:
\[ z=(0)(1)+(0)(0)-1=-1 \]
The prediction is
\[ \hat{y}=0 \]
The error is
\[ e=0-0=0 \]
The prediction is correct. The parameters remain unchanged.
Input:
\[ x_1=1,\quad x_2=1,\quad y=1 \]
Calculate the weighted sum:
\[ z=(0)(1)+(0)(1)-1=-1 \]
The prediction is
\[ \hat{y}=0 \]
The error is
\[ e=1-0=1 \]
The prediction is incorrect, so update the parameters.
Update the first weight:
\[ w_1=0+(1)(1)(1)=1 \]
Update the second weight:
\[ w_2=0+(1)(1)(1)=1 \]
Update the bias:
\[ b=-1+(1)(1)=0 \]
The parameters after Epoch 1 are
\[ \boxed{w_1=1,\quad w_2=1,\quad b=0} \]
The perceptron has changed its decision boundary, but training is not finished.
| Sample | (z) | Prediction | Target | Error |
|---|---|---|---|---|
| 1 | 0 | 1 | 0 | -1 |
| 2 | -1 | 0 | 0 | 0 |
| 3 | -1 | 0 | 0 | 0 |
| 4 | -1 | 0 | 1 | 1 |
Parameters at the end of Epoch 1:
\[ w_1=1,\quad w_2=1,\quad b=0 \]
There were two incorrect predictions and two parameter updates.
Start with the parameters from Epoch 1.
Input:
\[ x_1=0,\quad x_2=0,\quad y=0 \]
Weighted sum:
\[ z=(1)(0)+(1)(0)+0=0 \]
Prediction:
\[ \hat{y}=1 \]
Error:
\[ e=0-1=-1 \]
Update the parameters:
\[ w_1=1,\qquad w_2=1,\qquad b=-1 \]
Input:
\[ x_1=0,\quad x_2=1,\quad y=0 \]
Weighted sum:
\[ z=(1)(0)+(1)(1)-1=0 \]
Prediction:
\[ \hat{y}=1 \]
Error:
\[ e=0-1=-1 \]
Update the parameters:
\[ w_1=1 \]
\[ w_2=1+(1)(-1)(1)=0 \]
\[ b=-1+(1)(-1)=-2 \]
The new parameters are
\[ \boxed{w_1=1,\quad w_2=0,\quad b=-2} \]
Input:
\[ x_1=1,\quad x_2=0,\quad y=0 \]
Weighted sum:
\[ z=(1)(1)+(0)(0)-2=-1 \]
Prediction:
\[ \hat{y}=0 \]
Error:
\[ e=0-0=0 \]
The prediction is correct. No update is required.
Input:
\[ x_1=1,\quad x_2=1,\quad y=1 \]
Weighted sum:
\[ z=(1)(1)+(0)(1)-2=-1 \]
Prediction:
\[ \hat{y}=0 \]
Error:
\[ e=1-0=1 \]
Update the parameters:
\[ w_1=1+(1)(1)(1)=2 \]
\[ w_2=0+(1)(1)(1)=1 \]
\[ b=-2+(1)(1)=-1 \]
The parameters after Epoch 2 are
\[ \boxed{w_1=2,\quad w_2=1,\quad b=-1} \]
| Sample | (z) | Prediction | Target | Error |
|---|---|---|---|---|
| 1 | 0 | 1 | 0 | -1 |
| 2 | 0 | 1 | 0 | -1 |
| 3 | -1 | 0 | 0 | 0 |
| 4 | -1 | 0 | 1 | 1 |
At the end of Epoch 2:
\[ w_1=2,\quad w_2=1,\quad b=-1 \]
The perceptron has not yet converged.
The perceptron predicts class 1 whenever
\[ w_1x_1+w_2x_2+b\geq 0 \]
With the current parameters, the prediction for the input ((1,1)) is still incorrect.
Training must continue until a complete epoch has no classification errors.
The AND dataset is linearly separable, so the classical perceptron algorithm can find a separating solution with continued training under the standard assumptions.
Suppose
\[ w_1=0.5,\quad w_2=-0.2,\quad b=-0.1 \]
and the input is
\[ x_1=1,\quad x_2=2 \]
Calculate:
Solution
\[ z=(0.5)(1)+(-0.2)(2)-0.1=0 \]
Because (z),
\[ \hat{y}=1 \]
Suppose
\[ w_1=0.2,\quad w_2=0.3,\quad b=-0.4 \]
The input is ((1,1)), the target is (y=1), and the learning rate is (eta=0.1).
Calculate the prediction, error, and updated parameters.
Solution
Weighted sum:
\[ z=(0.2)(1)+(0.3)(1)-0.4=0.1 \]
Prediction:
\[ \hat{y}=1 \]
Error:
\[ e=1-1=0 \]
No update is needed:
\[ w_1=0.2,\quad w_2=0.3,\quad b=-0.4 \]
Use this dataset:
| (x_1) | (x_2) | Target (y) |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
Initialize:
\[ w_1=0,\quad w_2=0,\quad b=0,\quad \eta=1 \]
Process the first two samples in order. Calculate the weighted sum, prediction, error, and updated parameters after each sample.
Solution
Sample 1: Input ((0,0)), target (0).
\[ z=0,\qquad \hat{y}=1,\qquad e=-1 \]
Update:
\[ w_1=0,\quad w_2=0,\quad b=-1 \]
Sample 2: Input ((0,1)), target (1).
\[ z=(0)(0)+(0)(1)-1=-1 \]
Thus,
\[ \hat{y}=0,\qquad e=1 \]
Update:
\[ w_1=0,\quad w_2=1,\quad b=0 \]
\[ \boxed{ \text{Forward pass} \rightarrow \text{Error} \rightarrow \text{Update} \rightarrow \text{Repeat} } \]