# Implementation of Perceptron Algorithm for NOT Logic in Python

*Fig: NOT gate*

In this article, you’ll learn how to implement the** perceptron algorithm for NOT logic **in python. The steps that we’ll follow will also enable you to easily implement any other logic function using the perceptron algorithm.

## Perceptron algorithm for **NOT** logic in Python

*Fig: A perceptron with two inputs*

A** Perceptron** can simply be defined as a **feed-forward neural network** with a single hidden layer. It takes a certain number of inputs (** x1 and x2 **in this case), processes them using the

**perceptron algorithm**, and then finally produce the output

*which can either be 0 or 1. As*

**y***can take only two values, a perceptron can also act as a linear classifier. According to the perceptron algorithm,*

**y***y = Wx + b, **where** Wx = w1x1 + w2x2, W = perceptron model weights **and** b = bias
*

*Also,*

*y = 1 if Wx + b > 0*

*and*

*0 if Wx + b ≤ 0*

The steps that we’ll use to implement the **NOT** logic using a perceptron is similar to how a neural network is trained.

- First, we’ll initialize the
**weights**and the**bias**of the perceptron. - Then the input will be forward propagated through the network and output
will be produced.**‘y’** - This obtained result will then be compared with the actual result and the error obtained will be backpropagated through the network to adjust the weights and the bias of the model. This will minimize the error obtained.
- We’ll then repeat the above steps for all the inputs present.

**NOT** Logic

A **NOT** gate performs complementation function i.e. its output is just the complement of its input. They are also termed as inverters because they simply invert the input signal. The truth table of a **NOT** gate is shown below:

```
+---+---+
| x | y |
+---+---+
| 0 | 1 |
+---+---+
| 1 | 0 |
+---+---+
```

### Perceptron Algorithm

As discussed above, according to the perceptron algorithm * y = Wx+ b*. As

**NOT**gate is a single input gate,

*in this case. To begin with, let us assume*

**Wx = w1x1**

**w1 = 1***Let, the result obtained using the perceptron algorithm be*

**and b = 1.***and the actual result be*

**y’**

**y**(given in the truth table).- Now, using the first row of the
*x = 0*) as our input, we getwhich is same as**y’ = 1.0 + 1 = 1**.**y** - Using the second row as our input (
*x=1*) , we get**y’ = 1.1 + 1 = 2 ≠****y***.*To make, let**y = y’****w1****= -1***.*This makes**y’ = (-1).1 + 1 =****0***= y**.*By calculation, you’ll observe that these values of the weight and the bias satisfy the**NOT**logic for both the rows.

Therefore, the model to implement the **NOT** logic using the perceptron algorithm will be:

**y = (-1).x1 +** **1**

### Code

# Importing the required libraries import numpy as np # Defining the activation function def activation_function(y): if y > 0: y = 1 elif y <= 0: y = 0 return y # W = weights of the perceptron model W = np.array([-1]) # b = bias of the model b = 1 # Defining the perceptron algorithm def perceptron_algorithm(x): # y = w1x1 + b y = np.dot(W, x) + b # y = 1 if Wx+b > 0 else y = 0 y = activation_function(y) return y # Input values to verify the NOT logic input1 = np.array([0]) input2 = np.array([1]) # Printing the results print('NOT Logic: \n') print(f'x = 0 => y = {perceptron_algorithm(input1)}') print(f'x = 1 => y = {perceptron_algorithm(input2)}')

**OUTPUT:**

NOT Logic:x = 0 => y = 1x = 1 => y = 0

As we can see, the model predictions are the same as the actual results. Hence, we successfully implemented the Perceptron Algorithm for **NOT** Logic. You can also try implementing other logic functions by following the same steps and obtaining the correct values of the model weights and bias.

Also read:

Predicting video game sales using Machine Learning in Python

Understanding Artificial Neural network (ANN)

How to choose number of epochs to train a neural network in Keras

## Leave a Reply