Example 2 - Exclusive OR

Top  Previous  Next

Data file:  xor.csv


The exclusive OR example is a very simple model that uses only four rows of data with two inputs and one output. Furthermore, all inputs are binary – either zero or one representing false and true respectively. We choose to explain this example not only because it is well known in historical neural network literature, but because it shows how true-false models should be built.


You will note that the data for the XOR problem shows that the output is true if one but only one of the inputs is true. Otherwise the output is false. True-false models are best built using the operations in the categories Boolean and Relational, because those operations are true-false operations.


Using logical operations, one of the formulas that represent Exclusive OR is:


(Input1 or Input2) and (not(Input1 and Input2))


Running Chaos Hunter with only the first three operations in the Boolean category easily finds this result using the goal of maximizing R-squared and no scaling.


But there are many other formulas, usually longer, that yield the same answers. You can find them by including all four of the operations in the Boolean category, as well as all of the ones in the Relational category, and then running with different random numbers (seeds). Here is one below.  See if you agree that it works:


If Input2< >0 then (Input1<Input2) else Input1


To interpret the above remember that (Input1<Input2) is either true (1) or false (0). The symbol < > means “not equal”. Remember also that all inputs are either 0 or 1. Until you can convince yourself that the formula above works, you are not ready to pursue true-false equations!


Here are some more we found (we are sure there are many more):


If (Input1 < > 0) then not(Input2) else Input2


If (Input2 and Input1) < > 0 then ((-91.51521) > 0.5797825) else (Input1 or Input2)


((Input1 < 0.5046189) <= Input2) and not((If not((not(Input1) or (((-22.36354) >= 0.5046189) <= (-61.758)))) < > 0 then Input2 else Input1))


not(not(((Input2 or Input1) or Input1))) > (Input2 > (Input2 <= not(Input1)))


not(((Input2 <= not(Input1)) < (Input1 or Input2))) and (Input1 or Input2)


((Input2 <= not(Input1)) or not(Input2)) and (Input1 or Input2)


not(((not(Input1) < Input2) >= (Input1 or Input2)))


not((If ((not(Input2) and (Input2 < Input1)) >= Input2) <> 0 then not(Input1) else Input1))


So you see there are many correct answers, but we won’t ask you to verify all of these longer ones unless you have no other life. If you decide to try, truth tables should help.


To avoid having the constants in the formulas, just set max constants to zero.


This problem can also be solved with simple arithmetic operators as opposed to true/false logic as this formula shows:

 (Input1 - Input2) / (Input1 - Input2)

The formula above works because when the denominator is zero, the Chaos returns 0 as a quotient.

We also built a neural net to solve the problem, which doesn't give precise answers of 0 and 1, but they are close enough:

 n2(2, n2(n2(0.3687445, Input2, 2, n2(0.513603, Input2, n2(0.3687445, 2, (-2), Input2), Input1)), 2, Input2, 0.1258166), Input1, 0.8031954)

In the formula above, n2 is a neuron with two inputs and one output.