# Part 3 – Single-layer perceptron

I hope you read part 1 and 2, we will now look at implementing an ANN with 3 input neurons. This means that our problem has 3 features. Here is the architecture.

is the weight between the neuron in layer and the neuron in layer .

Feedforward

The activation of the predicted output can be written as follows,

(1)

Where

(2)

Backpropagation

The MSE cost function is defined as follows,

(3)

Let’s apply Gradient Descent to the first weight .

(4)

The chain rule now becomes,

(5)

Isolating each terms, we have

If we repeats the same steps for the other 2 weights and the bias, we get

We are now ready to implement!

Numpy implementation

The implementation is inspired from this article.

The problem

Before tackling the implementation itself, we need to define a problem to solve. Let’s build a toy dataset for a simple classification problem. Suppose we have some information about obesity, smoking habits, and exercise habits of five people. We also know whether these people are diabetic or not. We can encode this information as follows:

PersonSmokingObesityExerciseDiabetic
Person 10101
Person 20010
Person 31000
Person 41101
Person 51111

“In the above table, we have five columns: Person, Smoking, Obesity, Exercise, and Diabetic. Here 1 refers to true and 0 refers to false. For instance, the first person has values of 0, 1, 0 which means that the person doesn’t smoke, is obese, and doesn’t exercise. The person is also diabetic.

It is clearly evident from the dataset that a person’s obesity is indicative of him being diabetic. Our task is to create a neural network that is able to predict whether an unknown person is diabetic or not given data about his exercise habits, obesity, and smoking habits. This is a type of supervised learning problem where we are given inputs and corresponding correct outputs and our task is to find the mapping between the inputs and the outputs.”

from [1]

The code

We will base our implementation on the neural network architecture described above.

We start by importing some libraries and defining the Sigmoid function and its derivative. We then define our data set and the hyperparameters of the model. During the training phase, we perform feedforward and backpropagation steps. We can then plot the evolution of the cost, weights and bias with the number of iterations (epoch). Finally, we test our neural network on some unseen examples.

In example 1, a person who is smoking, not obese and does not exercise is classified as not diabetic. In example 2, a person who is not smoking, obese and does not exercise is classified as diabetic.

The training error (MSE) keeps decreasing with the number of iterations, which is a good sign. We can also notice that the weight becomes predominant after many iterations. This is because the 2nd feature (obesity) is very highly correlated with the output variable (diabetic).

Conclusion

This article described the theory of a very simple neural network with one input layer and one output layer. It was implemented in plain Numpy and applied to a simple classification problem with 3 features and 5 observations.

This type of neural network is classed a Perceptron and it is capable of classify linearly separable data. However, most real-world problems require to identify non-linear decision boundaries. In the next article, I will describe how multi-layer perceptron can be used to estimate non-linear decision boundaries.