Chris Chris - 3 months ago 81
Swift Question

XOR Neural Network - unexpected results

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

Part of me thinks it's the actual training system trying to learn multiple things at once.

Results Screenshot

I have linked my playground in case anyone can spot anything wrong:

Daniels code:


There are a couple of errors in your code. The first (and most important) is a subtlety in the way you're creating your networks.

Right now you're 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 and also all the hidden with the same Neuron, so there are only 4 Neurons: 2 for input (the regular repeated 2 times and a bias neuron) and 2 for hidden (the regular repeated 4 times and 1 for bias).

You can solve it by simply using a for loop:

public class Network
    var inputs:[Neuron] = []
    var hidden:[Neuron] = []
    var output:Neuron!

    public init()
        for _ in 1...2 {

        for _ in 1...4 {

        //print("inputs length: \(inputs.count)")

        inputs.append(Neuron(bias: true))
        hidden.append(Neuron(bias: true))

        output = Neuron()



The other (minor) thing is when you calculate the output of a Neuron you're adding the bias instead of replacing it (bias = from.output*c.weight), I don't know if that was on purpose but the result seems to be unaffected.

Trained network