Xor with one neuron

Experiment: XOR with one decision neuron

Experiment date: July 31, 2026
Distribution: Sensory 0.1.0 for Windows
Network: XorOneNeuron

Objective

The objective was to create a Cognia example that uses one trained decision neuron, can be trained with cognia_train.exe, served by cognia_serve.exe, and called from a PHP CLI client over REST to evaluate XOR inputs.

Mathematical limitation

A conventional neuron over two raw inputs computes a function similar to:

y = tanh(wA*A + wB*B + bias)

Its decision boundary is linear. XOR is not linearly separable: (0,0) and (1,1) belong to class 0, while (0,1) and (1,0) belong to class 1. No single line separates these sets.

The experiment therefore retains one trained decision neuron but changes the input representation. PHP converts the two bits into a one-hot truth-table state:

A B Cognia input [00,01,10,11] Target
0 0 [1,0,0,0] 0
0 1 [0,1,0,0] 1
1 0 [0,0,1,0] 1
1 1 [0,0,0,1] 0

The nonlinear decomposition happens before the neuron. The neuron learns four independent truth-table weights. This is feature engineering, not evidence that one neuron can solve XOR directly over [A,B].

Files and topology

  • xor-one-neuron.cognia — four sensors and one decision neuron;
  • xor.dataset — the complete four-row truth table;
  • client.php — CLI client and (A,B) to one-hot conversion;
  • README-cz.md / README-en.md — usage instructions;
  • networks/xor-one-neuron.bin — trained weights.

The runtime topology has five neuron instances: four input sensors and one trained output neuron, connected by four synapses.

Training

.\bin\cognia_train.exe `
  .\Cognia\examples\xor-one-neuron\xor-one-neuron.cognia `
  .\Cognia\examples\xor-one-neuron\xor.dataset `
  2000 XorOneNeuron `
  --save .\networks\xor-one-neuron.bin

The model has one numeric output, so --softmax is not used. Mean squared error was approximately 0.5616 before training and 0.0754 after 2,000 iterations.

Input Target Activation after training
[1,0,0,0] 0 0.325
[0,1,0,0] 1 0.751
[0,0,1,0] 1 0.751
[0,0,0,1] 0 0.269

The client uses a decision threshold of 0.5.

REST verification

The server was bound to the loopback interface:

.\bin\cognia_serve.exe `
  .\Cognia\examples\xor-one-neuron\xor-one-neuron.cognia `
  .\networks\xor-one-neuron.bin `
  --network XorOneNeuron --host 127.0.0.1 --port 8080

The PHP client calls POST /evaluate, for example:

{"input":[0,1,0,0],"iters":12}

End-to-end results from the saved network, real REST server, and PHP client:

A B REST activation Result
0 0 0.268506 0 — OK
0 1 0.685969 1 — OK
1 0 0.686511 1 — OK
1 1 0.220954 0 — OK

Values during a separate server run differ slightly from the trainer report, but all retain an adequate margin around the 0.5 threshold. The client reads raw output[0]; it does not use outputs[0].active, because the signal actuator derives its level from absolute activation and is unsuitable for general scalar classification.

Unsuccessful variants and findings

Direct gated readout edges

The first design used four gated A_i × B_j edges directly into the decision neuron. The topology was valid, but the single-channel delta trainer did not reliably distinguish the gated features. After 2,000 iterations, all outputs collapsed near 0.69.

This suggests that the single-channel supervised update does not account for the gate in exactly the same way as the forward pass, or updates with pre rather than pre × gate. The runtime source should be inspected to confirm this.

Fixed conjunction hidden layer

The second design materialized four conjunctions as helper neurons and trained only their edges into the output. It was also unstable: longer training increased the error, and one positive case did not learn consistently.

This supports the known limitation that the runtime has no general deep credit assignment. Fixed state or product modules can form a substrate, but the trainer needs an unambiguous definition of readout features and trainable edges.

Conclusion

The final variant is a small, reproducible demonstration of training, serialization, and REST inference with one decision neuron. It solves XOR by explicitly encoding the truth table in the client. Native XOR over raw [A,B] requires reliably trainable product features, a nonlinear hidden layer, or deep learning support in Cognia.

One neuron doing Xor in Sensory
One neuron doing Xor in Sensory Zdroj obrázku: Sensory runtime
ZIP archive

Xor-one-neuron

Xor-one-neuron.zip
Download ZIP