Open In App

Implementation of Perceptron Algorithm for NOR Logic Gate with 2-bit Binary Input

Last Updated : 06 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A perceptron is a simplest neural network which takes several input value and multiplies them by weights and bias values and then passes the result through a step function. It is used for binary classification. In this article, we'll see how perceptron can be used to simulate a NOR logic gate using 2-bit binary inputs.

A NOR gate gives an output of 1 only when both inputs are 0, otherwise output is 0. Truth table for NOR gate is:

Input 1

Input 2

Output

001
010
100
110

Perceptron for NOR Gate

A perceptron works using this simple formula:

y = \Theta(w_1 x_1 + w_2 x_2 + b)

where:

  • x_1, x_2 = input values (0 or 1)
  • w_1, w_2 = weights
  • b = bias
  • Θ(z) = Step function

Python Code for NOR Gate

Below are the steps to be followed for making a perception for NOR gate:

Step 1: Importing library and Defining Step Function

Step Function: we will use

  • 1 if z >= 0
  • 0 if z < 0
Python
import numpy as np

def step_function(z):
    return 1 if z >= 0 else 0

Step 2: Defining Perceptron Model

Python
def perceptron(x, w, b):
    z = np.dot(w, x) + b
    return step_function(z)

Step 3: OR Gate using Perceptron

For the OR gate let’s choose:

  • w1=1
  • w2=1
  • b=-0.5
Python
def OR_gate(x):
    w = np.array([1, 1])
    b = -0.5
    return perceptron(x, w, b)

Step 4: NOT Gate using Perceptron

For the NOT operation on the OR output we use:

  • w = -1
  • b = 0.5
Python
def NOT_gate(x):
    w = np.array([-1])
    b = 0.5
    return perceptron(np.array([x]), w, b)

Step 5: NOR Gate using Combination of OR and NOT

Python
def NOR_gate(x):
    or_output = OR_gate(x)
    nor_output = NOT_gate(or_output)
    return nor_output

Step 6: Testing all Input Combinations

Python
print("NOR Gate Output:")
inputs = [[0, 0], [0, 1], [1, 0], [1, 1]]
for x in inputs:
    result = NOR_gate(np.array(x))
    print(f"NOR({x[0]}, {x[1]}) = {result}")

Output:


Output
NOR gate Output

The code gives the correct output for all NOR gate input combinations. The perceptron works as expected and follows the truth table.


Next Article

Similar Reads