AND Gate using Single Perceptron Neural Network

Micheal Keines
3 min readFeb 10, 2024

--

The fundamental concept behind machine learning is to discover a function that effectively models the problem at hand. This process entails utilizing training data and a chosen loss function to gradually refine an initial function, which initially generates random or inaccurate outputs. Through each iteration, the loss function guides adjustments to the function, progressively bringing it closer to accurately producing valid outputs.

In practical scenarios, the final function derived from machine learning can be incredibly complex and challenging to comprehend visually. However, for our purposes, we aim to simplify and visualize the process. To achieve this, we’ll opt for a straightforward function that we already understand and know will replicate the logic of an AND Gate when provided with input values.

AND Truth Table

Perceptrons serve as the foundational elements of Neural Networks, encompassing both a function and activation logic.

You may be familiar with the concept of hyper-planes. A common decision boundary function for a hyperplane determines whether input values lie on, to the left, or to the right of the plane. If the inputs lie precisely on the hyperplane, solving the equation yields zero; negative values indicate placement to the left, while positive values denote placement to the right.

Here, we have three components: a weight vector (W), an input values vector (X), and a constant bias (B).

The weight determines the orientation of the hyperplane, while the bias indicates its distance from the origin.

We could straightforwardly use an if condition to assign zero to output values equal to or less than zero, and one to positive values. However, this task aligns with the role of the activation function, which maps the output to the desired range.

This setup constitutes our entire Neural Network.

Let’s break it down:

our perceptron performs one calculation followed by activation.

The calculation involves matrix multiplication of the weight (W) with the input (X), plus the bias (B).

Here, ( X = [input, input] ), (W = [1, 1] ), and ( B = [-1.5] ).

We’ll utilize the built-in Torch method heaviside to implement the step function.

python code using torch

The code is fairly self-explanatory. For any unfamiliar methods, you can easily find explanations by searching online — they’re standard Torch functions used for matrix manipulation.

output of our neural network

Output as excepted mimics the AND gate logic.

I will continue working on add more interesting model in the series, please share any suggestions in comments.

--

--