Perceptron Algorithm for NAND Logic Gate with 2-bit Binary Input


Introduction

Within the domain of Artificial Intelligence and Machine Learning, one of the foremost basic components is the Artificial Neural Network (ANN). ANNs are motivated by the human brain's neural systems and are designed to imitate the way neurons prepare data. At the center of an ANN lies the perceptron, an essential building square that serves as a basic numerical model of a neuron. In this article, we'll investigate the Perceptron NAND Logic Gate with 2−bit Binary Input, and basic however fundamental concept within the world of ANNs.

Understanding the Perceptron

The perceptron, proposed by Frank Rosenblatt in 1957, could be a sort of feedforward neural arrangement. It takes different input signals, applies weights to each input, wholes them up, and passes the result through an actuation work to deliver a yield.

The NAND Logic Gate: In computerized electronics, a NAND gate (NOT−AND) may be a crucial logic gate that performs the Boolean operation of refutation and conjunction. It returns a True (1) yield when both inputs are False (0), and for any other combination of input bits, it returns a False (0) yield. The truth table for a 2−input NAND entryway is as takes after:

Input A

Input B

Output

0

0

1

0

1

1

1

0

1

1

1

0

Implementation of NAND Logic Gate

The objective is to discover the proper combination of weights and predisposition for the perceptron so that it can create the right yield for all conceivable input combinations.

Algorithm

Step 1 :Initialize the perceptron with random weights and predisposition.

Step 2 :Show the input information to the perceptron and compute the weighted entirety of the inputs.

Step 3 :Apply an actuation function to the weighted entirety to get the yield (0 or 1).

Step 4 :Contrast the output and the target yield (NAND truth table).

Step 5 :Modify the weights and biases based on the error to bring the perceptron closer to the right yield.

Rehash steps 2−5 for numerous iterations (epochs) until the perceptron learns to create the right yield for all inputs.

Example

# import the required module
import numpy as np

#create the activation function
def step_function(x):
    return 1 if x >= 0 else 0

#define class
class PerceptronNAND:
    def __init__(self, input_size):
        self.weights = np.random.rand(input_size)
        self.bias = np.random.rand()
        
    def predict(self, inputs):
        summation = np.dot(inputs, self.weights) + self.bias
        return step_function(summation)
    
    def train(self, inputs, target_output, learning_rate=0.1, epochs=100):
        for epoch in range(epochs):
            total_error = 0
            for input_data, target in zip(inputs, target_output):
                prediction = self.predict(input_data)
                error = target - prediction
                total_error += abs(error)
                self.weights += learning_rate * error * input_data
                self.bias += learning_rate * error
            if total_error == 0:
                break

#Intilaize array
inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])


target_output = np.array([1, 1, 1, 0])


nand_gate = PerceptronNAND(input_size=2)
nand_gate.train(inputs, target_output)


print("Testing Perceptron NAND gate:")
for input_data in inputs:
    output = nand_gate.predict(input_data)
    print(f"Input: {input_data}, Output: {output}")

Output

Testing Perceptron NAND gate:
Input: [0 0], Output: 1
Input: [0 1], Output: 1
Input: [1 0], Output: 1
Input: [1 1], Output: 0

Conclusion

The Perceptron NAND Logic Gate with 2−bit Binary Input is a foundational concept that lays the basis for more complex neural systems. By understanding how a perceptron learns the NAND operation through preparation, we pick up bits of knowledge into the broader field of Artificial Neural Systems. It grandstands the ability of neural systems to perform coherent operations, opening the entryway to endless applications, from design acknowledgment to natural language processing. As we dig more profound into the world of AI and Machine Learning, it is basic to get a handle on these crucial concepts to construct more modern and powerful models. The perceptron NAND gate serves as an update of the straightforwardness and class that underlies the most progressed innovations within the world of AI.

Updated on: 28-Jul-2023

667 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements