# Hebbian Learning Rule with Implementation of AND Gate

Hebbian Learning Rule, also known as Hebb Learning Rule, was proposed by Donald O Hebb. It is one of the first and also easiest learning rules in the neural network. It is used for pattern classification. It is a single layer neural network, i.e. it has one input layer and one output layer. The input layer can have many units, say n. The output layer only has one unit. Hebbian rule works by updating the weights between neurons in the neural network for each training sample.

**Hebbian Learning Rule Algorithm : **

- Set all weights to zero, w
_{i }= 0 for i=1 to n, and bias to zero. - For each input vector, S(input vector) : t(target output pair), repeat steps 3-5.
- Set activations for input units with the input vector X
_{i}= S_{i}for i = 1 to n. - Set the corresponding output value to the output neuron, i.e. y = t.
- Update weight and bias by applying Hebb rule for all i = 1 to n:

**Implementing AND Gate : **

There are 4 training samples, so there will be 4 iterations. Also, the activation function used here is Bipolar Sigmoidal Function so the range is [-1,1].

**Step 1 : **

Set weight and bias to zero, w = [ 0 0 0 ]^{T }and b = 0.

**Step 2 : **

Set input vector X_{i} = S_{i }for i = 1 to 4.

X_{1} = [ -1 -1 1 ]^{T}

X_{2} = [ -1 1 1 ]^{T}

X_{3} = [ 1 -1 1 ]^{T}

X_{4} = [ 1 1 1 ]^{T}

**Step 3 : **

Output value is set to y = t.

**Step 4 : **

Modifying weights using Hebbian Rule:

First iteration –

w(new) = w(old) + x_{1}y_{1} = [ 0 0 0 ]^{T} + [ -1 -1 1 ]^{T }. [ -1 ] = [ 1 1 -1 ]^{T}

For the second iteration, the final weight of the first one will be used and so on.

Second iteration –

w(new) = [ 1 1 -1 ]^{T} + [ -1 1 1 ]^{T} . [ -1 ] = [ 2 0 -2 ]^{T}

Third iteration –

w(new) = [ 2 0 -2]^{T} + [ 1 -1 1 ]^{T} . [ -1 ] = [ 1 1 -3 ]^{T}

Fourth iteration –

w(new) = [ 1 1 -3]^{T} + [ 1 1 1 ]^{T} . [ 1 ] = [ 2 2 -2 ]^{T}

So, the final weight matrix is [ 2 2 -2 ]^{T}

**Testing the network : **

For x_{1} = -1, x_{2} = -1, b = 1, Y = (-1)(2) + (-1)(2) + (1)(-2) = -6

For x_{1} = -1, x_{2} = 1, b = 1, Y = (-1)(2) + (1)(2) + (1)(-2) = -2

For x_{1} = 1, x_{2} = -1, b = 1, Y = (1)(2) + (-1)(2) + (1)(-2) = -2

For x_{1} = 1, x_{2} = 1, b = 1, Y = (1)(2) + (1)(2) + (1)(-2) = 2

The results are all compatible with the original table.

**Decision Boundary : **

2x_{1} + 2x_{2} – 2b = y

Replacing y with 0, 2x_{1} + 2x_{2} – 2b = 0

Since bias, b = 1, so 2x_{1} + 2x_{2} – 2(1) = 0

2( x_{1} + x_{2} ) = 2

The final equation, x_{2} = -x_{1} + 1