XOR is not solvable by using a single perceptron with standard scalar product and unit step function.
This article suggests using 3 perceptron to make a network: http://toritris.weebly.com/perceptron-5-xor-how--why-neurons-work-together.html
I'm trying to run the 3-perceptron network this way but it doesn't produce correct results for XOR:
//pseudocode
class perceptron {
constructor(training_data) {
this.training_data = training_data
}
train() {
iterate multiple times over training data
to train weights
}
unit_step(value) {
if (value<0) return 0
else return 1
}
compute(input) {
weights = this.train()
sum = scalar_product(input,weights)
return unit_step(sum)
}
}
The above perceptron can solve NOT, AND, OR bit operations correctly. This is how I use 3 perceptrons to solve XOR:
AND_perceptron = perceptron([
{Input:[0,0],Output:0},
{Input:[0,1],Output:0},
{Input:[1,0],Output:0},
{Input:[1,1],Output:1}
])
OR_perceptron = perceptron([
{Input:[0,0],Output:0},
{Input:[0,1],Output:1},
{Input:[1,0],Output:1},
{Input:[1,1],Output:1}
])
XOR_perceptron = perceptron([
{Input:[0,0],Output:0},
{Input:[0,1],Output:1},
{Input:[1,0],Output:1},
{Input:[1,1],Output:0}
])
test_x1 = 0
test_x2 = 1
//first layer of perceptrons
and_result = AND_perceptron.compute(test_x1,test_x2)
or_result = OR_perceptron.compute(test_x1,test_x2)
//second layer
final_result = XOR_perceptron.compute(and_result,or_result)
The final_result above is not consistent, sometimes 0, sometimes 1. It seems I run the 2 layers wrongly. How to run these 3 perceptrons in 2 layers the correct way?
If you want to build a neural network with logical connectives (and, or, not), you have to consider the following equivalences regarding xor:
A xor B ≡ (A ∨ B) ∧ ¬(A ∧ B) ≡ (A ∨ B) ∧ (¬A ∨ ¬B) ≡ (A ∧ ¬B) ∨ (¬A ∧ B)
So you would need at least three and- or or-perceptrons and one negation if you want to use your perceptrons if I understand them correctly. In the article they use three perceprons with special weights for the xor. These are not the same as and- and or-perceptrons.