Perceptron
The perceptron is the fundamental building block of more complex architectures in neural networks. In this lesson, we’ll break down the perceptron’s components and see how it’s used for binary classification.
This Lesson Will Cover
- 🏗️ Perceptron Architecture
- 🔢 Mathematical Formulation
- 🐍 Python Implementation
- 🚀 Practical Applications
- ⚠️ Limitations and Insights
Introduction and Historical Context
The perceptron is one of the earliest models in the history of artificial intelligence and machine learning. Introduced by Frank Rosenblatt in 1958, the perceptron was designed as a simplistic model of a biological neuron, capable of binary classification.
The perceptron basically attempts to determine whether an input belongs to one class or another by applying a set of weights and thresholds.
The Modern Perceptron
The modern perceptron[1] consists of a linear classifier that calculates a weighted sum of the input features and applies a step function to determine the output.
Mathematical Formulation
Inputs and Weights:
Given an input vector and a weight vector be
Weighted Sum:
The perceptron computes the dot product:
where is the bias term.
Activation Function:
The perceptron uses a step function:
Learning Rule:
The perceptron updates weights iteratively during training using:
where is the learning rate, is the true label, and is the predicted label.
Python Implementation of a Perceptron
This math should all look familiar if you’ve gone through the earlier sections of this course.
In order to really understand how this works, let’s implement a perceptron for binary classification with Python.
import numpy as np
class Perceptron:
def __init__(self, learning_rate=0.01, n_iter=1000):
"""
Initialize perceptron with learning rate and number of iterations
"""
self.learning_rate = learning_rate # Controls how much weights are updated
self.n_iter = n_iter # Number of passes over the training data
self.weights = None # Weight coefficients for each feature
self.bias = None # Bias term
def fit(self, X, y):
"""
Train the perceptron on training data X and labels y
X: array of shape (n_samples, n_features)
y: array of shape (n_samples,) with binary labels 0/1
"""
# Initialize weights to zeros and bias to 0
n_samples, n_features = X.shape
self.weights = np.zeros(n_features)
self.bias = 0
# Training process---iterate n_iter times over all samples
for _ in range(self.n_iter):
for idx, x_i in enumerate(X):
# Calculate predicted output for current sample
linear_output = np.dot(x_i, self.weights) + self.bias
y_pred = self._activation(linear_output)
# Update weights and bias based on prediction error
update = self.learning_rate * (y[idx] - y_pred)
self.weights += update * x_i # Update each weight
self.bias += update # Update bias term
def predict(self, X):
"""
Make predictions on new samples X
Returns binary predictions 0/1
"""
linear_output = np.dot(X, self.weights) + self.bias
return self._activation(linear_output)
def _activation(self, x):
"""
Step activation function that returns:
1 if input >= 0
0 if input < 0
"""
return np.where(x >= 0, 1, 0)
Now we can train the perceptron on some input data with labels .
# Create simple linearly-separable dataset
X = np.array([
[2, 4], [1, 1], [2, 1], [3, 5], [1, 5],
[4, 1], [5, 3], [4, 5], [2, 2], [3, 2]
])
y = np.array([1, 0, 0, 1, 1, 0, 1, 1, 0, 0])
# Instantiate and train the perceptron
perceptron = Perceptron(learning_rate=0.1, n_iter=10)
perceptron.fit(X, y)
# Predict and evaluate
print("Predictions (10 iters):", perceptron.predict(X))
# Increase number of learning iterations
perceptron = Perceptron(learning_rate=0.1, n_iter=100)
perceptron.fit(X, y)
print("Predictions (100 iter):", perceptron.predict(X))
# Outputs:
# Predictions (10 iters): [0 0 0 0 1 0 0 0 0 0]
# Predictions (100 iter): [1 0 0 1 1 0 1 1 0 0]
Insights and Limitations
-
Strengths:
- Simple to implement and computationally efficient.
- Effective for linearly separable problems.
-
Limitations:
- Cannot solve non-linear problems (e.g., XOR problem - more on this in the coming lessons).
- Sensitive to feature scaling.
Overcoming Limitations
Practical Applications
Perceptrons can:
- Classify data that is linearly separable (e.g., determining if a point lies above or below a line).
- Serve as building blocks for more complex networks (e.g., multi-layer perceptrons).
As we mentioned above, perceptrons cannot classify data that isn’t linearly separable.
This limitation is overcome by more advanced architectures, such as using multi-layer neural networks with non-linear activation functions.
Conclusion
The perceptron is where it all started—it’s the OG of today’s incredibly complex and powerful AI models.
Take a moment and make sure that you’ve got a good grasp on how it works (specifically, understanding the Python implementation from this lesson) before moving on. I found this “hand-drawn” illustration on YouTube[2] really helpful.
Key Takeaways
🧠 The perceptron is a fundamental building block of neural networks, inspired by biological neurons.
➕ It works by computing weighted sums of inputs and applying a step activation function.
🔄 The learning process involves iteratively adjusting weights based on prediction errors.
✂️ Perceptrons can only solve linearly separable problems.
🐍 Python implementation requires just a few core components: weights, bias, and an activation function.
🚀 While limited, perceptrons laid the groundwork for modern deep learning architectures.
Next: Multi-Layer Architectures
Next, we’ll talk about multi-layer neural networks and explain how they allow us to model non-linear data.
Comments (0)
No comments yet.