Experiment 4

Implement OR and AND Logic Functions using Perceptron Neural Network

Theory

The perceptron is a fundamental building block of neural networks used to solve binary classification problems. It consists of input values, weights and bias, net sum, and an activation function.

Perceptron Structure:

The perceptron takes input vectors, multiplies them by corresponding weight vectors, adds bias, and passes the result through an activation function.

Perceptron Algorithm:

The Perceptron Algorithm outputs binary values (0 or 1) and can only classify linearly separable vector sets.

Program


            from numpy import array, random, dot
            from random import choice
            from pylab import ylim, plot
            from matplotlib import pyplot as plt

            step_function = lambda x: 0 if x < 0 else 1

            training_dataset = [
                (array([0,0,1]), 0),
                (array([0,1,1]), 1),
                (array([1,0,1]), 1),
                (array([1,1,1]), 1),
            ]

            weights = random.rand(3)
            error = []
            learning_rate = 0.2
            n = 100

            for j in range(n):
                x, expected = choice(training_dataset)
                result = dot(weights, x)
                err = expected - step_function(result)
                error.append(err)
                weights += learning_rate * err * x

            for x, _ in training_dataset:
                result = dot(x, weights)
                print('{}: {} -> {}'.format(x[:2], result, step_function(result)))

            ylim([-1,1])
            plot(error)
            plt.show()

Student Instructions

  1. Execute the above program for the OR logic function.
  2. Make changes to the program and implement the AND logic function. Plot the error for that function.
  3. Attach the output for assessment with the experiment write-up.

Conclusion

The perceptron algorithm, implemented using Python, is the simplest form of artificial neural networks. It can create a single Neuron model to solve binary classification problems by taking input vectors, multiplying them by corresponding weight vectors, adding bias, and passing the result through an activation function.

Text/Reference Books

Exercises

  1. Implement the OR function using the MP neuron (take binary data).
  2. Implement the AND function using the MP neuron (take binary data).