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()) }
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.

source share