Sitemap

algorithm of Hebb Net

2 min readJan 19, 2023

--

Hebb net is the simplest and earliest learning algorithm. If the interconnected neurons are both “on” or “off“, then learning occurs by modification of the weights.

algorithm

  1. initialize all weigths. w[i]=0
  2. for each input training vector and targer output pair, s:t, do steps 3–5
  3. set activation for output units: x[i]=s[i] i= 1 to n
  4. set activation for output unit : y=t
  5. adjust the weights (for each i) and bias :

w[i](new) = w[i](old)+x[i]*y
bias(new) = bias(old)+y

Get mehmet ali Aytekin’s stories in your inbox

Join Medium for free to get updates from this writer.

basic java implementation of AND Logic in Hebb Net with bipolar representation is below:

public class Neuron {

private int weight;

public void updateWeight(int input, int targetValue) {
weight += input * targetValue;
}

public int getWeight() {
return this.weight;
}

public int fire(int inputValue) {
return inputValue * weight;
}

}


public class HebbNet {

private Neuron[] _neurons;
private int inputSize;
private double bias;

public HebbNet(int countOfInputNeurons) {
this.inputSize = countOfInputNeurons;
_neurons = new Neuron[countOfInputNeurons];
for (int i = 0; i < countOfInputNeurons; i++) {
_neurons[i] = new Neuron();
}
}

public void train(int[] inputValues, int targetValue) {
for (int i = 0; i < inputSize; i++) {
_neurons[i].updateWeight(inputValues[i], targetValue);
}
updateBias(targetValue);
// printNewWeights();
}

@SuppressWarnings("unused")
private void printNewWeights() {
for (int i = 0; i < inputSize; i++) {
System.out.print(_neurons[i].getWeight() + " ");
}
System.out.println();
}

private void updateBias(int targetValue) {
setBias(getBias() + targetValue);

}

public int giveAnswer(int[] inputValues) {
int sum = 0;
for (int i = 0; i < inputSize; i++) {
sum += _neurons[i].fire(inputValues[i]);
}
return sum + getBias() > 0 ? 1 : -1;
}

public double getBias() {
return bias;
}

public void setBias(double bias) {
this.bias = bias;
}
}


public class LearningLogicWithHebb {

final static int _TRUE = 1;
final static int _FALSE = -1;

public static void main(String[] args) {

int[] input1 = { _TRUE, _TRUE };
int[] input2 = { _TRUE, _FALSE };
int[] input3 = { _FALSE, _TRUE };
int[] input4 = { _FALSE, _FALSE };

HebbNet hebbNet = new HebbNet(2);

hebbNet.train(input1, _TRUE);
hebbNet.train(input2, _FALSE);
hebbNet.train(input3, _FALSE);
hebbNet.train(input4, _FALSE);
System.err.println("----------------");
System.err.println(hebbNet.giveAnswer(input1));
System.err.println(hebbNet.giveAnswer(input2));
System.err.println(hebbNet.giveAnswer(input3));
System.err.println(hebbNet.giveAnswer(input4));
}
}

Resource : Fundamentals of Neural Networks by Laurene V. Fausett

--

--

mehmet ali Aytekin
mehmet ali Aytekin

Written by mehmet ali Aytekin

“what has history to do with me? mine is the first and only world!” ideas on game design | philosophy | programming | fiction | ai | art