How to make your neural network Python
Build your own Python Neural Network w/ an example: Learn how to build, train and use a neural network to solve a real-world problem.
Building a Neural Network from Scratch with Python
In this article, we will be discussing how to create a neural network from scratch using Python. We will be using the NumPy
library for its mathematical functions and matplotlib
for its visualization capabilities. We will be using this neural network to solve a basic classification problem.
In order to create a neural network, we need to first define the architecture of our network. We will be creating a simple feed-forward neural network with one hidden layer. This hidden layer will contain three neurons and the output layer will contain one neuron. The following diagram illustrates this architecture:
Now that we have defined the architecture of our network, we need to create a data set to train it. We will be using the Iris dataset for this example. The Iris dataset consists of 150 examples, each containing 4 features (sepal length, sepal width, petal length, petal width) and a label (Iris setosa, Iris virginica, Iris versicolor). The following code snippet reads the Iris dataset and separates it into training and test sets:
# import necessary libraries
import numpy as np
import matplotlib.pyplot as plt
# read the Iris dataset
data = np.genfromtxt("iris.csv", delimiter=",")
# separate features and labels
X = data[:, :4]
y = data[:, 4]
# separate training and test sets
X_train = X[::2]
y_train = y[::2]
X_test = X[1::2]
y_test = y[1::2]
Now that we have our dataset, we need to define some parameters for our neural network. We will use the following parameters for this example: the learning rate, the number of epochs, and the number of neurons in the hidden layer. The following code snippet defines these parameters:
# define parameters
learning_rate = 0.1
num_epochs = 1000
num_neurons = 3
Now that we have defined our parameters, we can start building our neural network. We will be using the sigmoid
activation function for our neurons. The sigmoid
activation function is a logistic function that takes a real-valued input and outputs a value between 0 and 1. The following code snippet defines this function:
# define sigmoid activation function
def sigmoid(x):
return 1.0 / (1.0 + np.exp(-x))
Now that we have defined our activation function, we need to create our weights and biases. We will be using random values for our weights and biases. The following code snippet creates these weights and biases:
# create weights and biases
w1 = np.random.uniform(low=-1.0, high=1.0, size=(4, num_neurons))
b1 = np.random.uniform(low=-1.0, high=1.0, size=(num_neurons))
w2 = np.random.uniform(low=-1.0, high=1.0, size=(num_neurons, 1))
b2 = np.random.uniform(low=-1.0, high=1.0, size=(1))
Now that we have our weights and biases, we can start training our neural network. The following code snippet implements the training algorithm for our neural network:
# train the neural network
for epoch in range(num_epochs):
# forward pass
z1 = np.dot(X_train, w1) + b1
a1 = sigmoid(z1)
z2 = np.dot(a1, w2) + b2
a2 = sigmoid(z2)
# compute loss
loss = np.mean((y_train - a2)**2)
# backpropagation
dz2 = a2 - y_train
dw2 = np.dot(a1.T, dz2)
db2 = np.sum(dz2, axis=0)
dz1 = np.dot(dz2, w2.T) * sigmoid(z1) * (1 - sigmoid(z1))
dw1 = np.dot(X_train.T, dz1)
db1 = np.sum(dz1, axis=0)
# update weights and biases
w2 -= learning_rate * dw2
b2 -= learning_rate * db2
w1 -= learning_rate * dw1
b1 -= learning_rate * db1
Now that our neural network is trained, we can test it on the Iris test set. The following code snippet implements this:
# test the neural network
z1 = np.dot(X_test, w1) + b1
a1 = sigmoid(z1)
z2 = np.dot(a1, w2) + b2
a2 = sigmoid(z2)
print("Predictions:", np.round(a2))
print("Actual labels:", y_test)
We can also visualize the predictions of our neural network with a confusion matrix. The following code snippet creates this confusion matrix:
# create confusion matrix
cm = np.zeros((3, 3), dtype=int)
for i in range(a2.shape[0]):
cm[int(y_test[i]), int(np.round(a2[i]))] += 1
print("Confusion Matrix:")
print(cm)
We have now successfully created a neural network from scratch in Python. We have used the Iris dataset to train and test our model, and we have visualized the predictions of our model with a confusion matrix. This example illustrates the power of neural networks and how they can be used to solve complex problems.