You are on page 1of 7

Artificial Intelligence and Data Science Department

Machine Learning / Even Sem 2023-24 / Experiment 06

Name: Abhishek Class/Roll No.: Grade:


Pattanayak D11AD / 48

Title of Experiment:
Implementation of Hebbian Learning (Logic Gates)

Objective of Experiment:
To develop Neural Network based learning models

Outcome of Experiment:
1. Implement various machine learning models
2. Apply suitable machine learning models for a given problem
statement

Theory:
Artificial Intelligence and Data Science Department

Machine Learning / Even Sem 2023-24 / Experiment 06


Artificial Intelligence and Data Science Department

Machine Learning / Even Sem 2023-24 / Experiment 06


Artificial Intelligence and Data Science Department

Machine Learning / Even Sem 2023-24 / Experiment 06


Artificial Intelligence and Data Science Department

Machine Learning / Even Sem 2023-24 / Experiment 06

Program:

import numpy as np

# initial values
INPUTS = np.array([[1, 1], [1, -1], [-1, 1], [-1, -1]])
LEARNING_RATE = 0.1

# step function (activation function)


def step_function(sum):
if sum >= 0:
return 1
return -1

# calculating output
def calculate_output(weights, instance, bias):
sum = instance.dot(weights) + bias
return step_function(sum)

# Hebb Algorithm
def hebb(outputs, weights, bias):
for i in range(4):

weights[0] = weights[0] + (INPUTS[i][0] * outputs[i])


weights[1] = weights[1] + (INPUTS[i][1] * outputs[i])
bias = bias + (1 * outputs[i])

print("Weight updated: " + str(weights[0]))


print("Weight updated: " + str(weights[1]))
Artificial Intelligence and Data Science Department

Machine Learning / Even Sem 2023-24 / Experiment 06

print("Bias updated: " + str(bias))


print("----------------------------------------")

return weights, bias

Output:

Please, enter which gate do you want? (and, or) (0 for


exit): and
Weight updated: 1.0
Weight updated: 1.0
Bias updated: 1
----------------------------------------
Weight updated: 0.0
Weight updated: 2.0
Bias updated: 0
----------------------------------------
Weight updated: 1.0
Weight updated: 1.0
Bias updated: -1
----------------------------------------
Weight updated: 2.0
Weight updated: 2.0
Bias updated: -2
----------------------------------------
prediction for [1, 1]: 1
prediction for [1, -1]: -1
prediction for [-1, 1]: -1
prediction for [-1, -1]: -1
Artificial Intelligence and Data Science Department

Machine Learning / Even Sem 2023-24 / Experiment 06

Please, enter which gate do you want? (and, or) (0 for


exit): or
Weight updated: 3.0
Weight updated: 3.0
Bias updated: 1
----------------------------------------
Weight updated: 4.0
Weight updated: 2.0
Bias updated: 2
----------------------------------------
Weight updated: 3.0
Weight updated: 3.0
Bias updated: 3
----------------------------------------
Weight updated: 4.0
Weight updated: 4.0
Bias updated: 2
----------------------------------------
prediction for [1, 1]: 1
prediction for [1, -1]: 1
prediction for [-1, 1]: 1
prediction for [-1, -1]: -1

Results and Discussions:


Successfully learnt and implemented Hebbian Learning for Logic
Gates in Python.

Citations:
1. https://github.com/hassanzadehmahdi/AND-OR-Gates-using
-Neural-Networks/blob/main/and-or.py

You might also like