XOR Neural Network - Unexpected Results

I am trying to implement Neural Network Daniel Shiffman XOR in swift, I have all the parts, but after training the results are unexpected.

Part of me thinks this is a real learning system, trying to learn several things at once.

Results Screenshot

I linked my playground if someone might notice something is wrong: https://www.dropbox.com/s/9rv8ku3d62h03ip/Neural.playground.zip?dl=0

Daniels Code:

https://github.com/shiffman/The-Nature-of-Code-Examples/blob/master/chp10_nn/xor/code/src/Network.java

+5
source share
1 answer

There are several errors in your code. The first (and most important) is the subtlety of how you create your networks.

Now you are using

inputs = [Neuron](repeating: Neuron(), count:2+1) hidden = [Neuron](repeating: Neuron(), count:4+1) 

But this creates all the inputs with the same Neuron , as well as everything hidden with the same Neuron , so for input only 4 Neuron s: 2 (regular repeat 2 times and bias neuron) and 2 for hidden (regular repeat 4 times and 1 for offset )

You can solve this simply by using a for loop:

 public class Network { var inputs:[Neuron] = [] var hidden:[Neuron] = [] var output:Neuron! public init() { for _ in 1...2 { inputs.append(Neuron()) } for _ in 1...4 { hidden.append(Neuron()) } //print("inputs length: \(inputs.count)") inputs.append(Neuron(bias: true)) hidden.append(Neuron(bias: true)) output = Neuron() setupInputHidden() setupHiddenOutput() } ... } 

Another (minor) thing when you calculate the Neuron output, add an offset instead of a replacement ( bias = from.output*c.weight ) , I don’t know if this was intentional, but the result does not seem to be affected.

Trained network

+1
source

Source: https://habr.com/ru/post/1259089/


All Articles