Building a Simple Perceptron in Python: A Step-by-Step Guide
Perceptrons are the foundation of neural networks and are an excellent starting point for beginners venturing into machine learning and artificial intelligence. In this tutorial, we’ll build a simple perceptron model in Python to understand how it works under the hood. We’ll train it to solve a basic binary classification task.
What is a Perceptron?
A perceptron is a single-layer neural network that maps input features to an output using weights, a bias, and an activation function. It’s one of the simplest forms of artificial neural networks, invented by Frank Rosenblatt in 1958.
The key steps in a perceptron are:
Step-by-Step Implementation
Here’s how you can implement a perceptron in Python:
1. Import Libraries
We’ll use numpy for numerical computations.
import numpy as np
2. Define the Activation Function
We’ll use the sigmoid function, which outputs values between 0 and 1, making it suitable for binary classification tasks.
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
return x * (1 - x)
The derivative of the sigmoid function helps in updating weights during training.
3. Prepare Training Data
We’ll use a small dataset with binary inputs and outputs.
training_inputs = np.array([[0, 0, 1],
[1, 1, 1],
[1, 0, 1],
[0, 1, 1]])
training_outputs = np.array([[0, 1, 1, 0]]).T
Each row of training_inputs represents one training example, and the corresponding value in training_outputs is the target output.
4. Initialize Weights
We assign random values to the weights, ensuring they’re initially small.
np.random.seed(1)
synaptic_weights = 2 * np.random.random((3, 1)) - 1
print("Random starting synaptic weights...")
print(synaptic_weights)
5. Train the Perceptron
We’ll iterate through the training data and adjust the weights to minimize errors.
Recommended by LinkedIn
for iterations in range(100000):
# Forward pass
input_layer = training_inputs
outputs = sigmoid(np.dot(input_layer, synaptic_weights))
# Calculate error
error = training_outputs - outputs
# Adjust weights
adjustments = error * sigmoid_derivative(outputs)
synaptic_weights += np.dot(input_layer.T, adjustments)
6. Display Results
After training, we’ll print the updated weights and final outputs.
print("Synaptic weights after training")
print(synaptic_weights)
print("Outputs after training: ")
print(outputs)
Full Code
Here’s the complete implementation:
import numpy as np
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
return x * (1 - x)
training_inputs = np.array([[0, 0, 1],
[1, 1, 1],
[1, 0, 1],
[0, 1, 1]])
training_outputs = np.array([[0, 1, 1, 0]]).T
np.random.seed(1)
synaptic_weights = 2 * np.random.random((3, 1)) - 1
print("Random starting synaptic weights...")
print(synaptic_weights)
for iterations in range(100000):
input_layer = training_inputs
outputs = sigmoid(np.dot(input_layer, synaptic_weights))
error = training_outputs - outputs
adjustments = error * sigmoid_derivative(outputs)
synaptic_weights += np.dot(input_layer.T, adjustments)
print("Synaptic weights after training")
print(synaptic_weights)
print("Outputs after training: ")
print(outputs)
Explanation of the Code
2. Forward Pass:
3. Error Calculation:
4. Backpropagation:
Outputs
Extending the Model
This perceptron works for binary classification with a single layer. To handle more complex datasets, you can:
Conclusion
Congratulations! You’ve just built and trained a simple neural network from scratch. This foundational understanding will help you grasp more advanced neural networks and deep learning concepts. Experiment with different datasets and parameters to see how the perceptron performs. Happy coding!
Python Developer | Data Science Expert | Liverpool John Moores University
4moThank you Hemangi Suthar for the kind words.. It motivates me to create more
Data Science PM at BU Spark | Product & Data Science | MS in Data Science, Boston University
4moThis is incredibly helpful! I recently completed a project where I implemented a perceptron from scratch for a binary classification task. It was a great way to dive deep into the fundamentals of neural networks, from weight initialization and forward propagation to backpropagation and weight updates. Posts like these are such valuable resources—thanks for sharing!