public ProtoNet(int[] topology) { // Validate the input. if (topology == null || topology.Length < 1) return; this.topology = topology; // NOTE: I seed my random for consistency when // testing the network. rand = new Random(424242); // And now I'm cheating with a flattened // list of nodes. This makes training easier. flattened = new List<Neuron>(); // Create the neuron array. inputs = new Neuron[topology[0]]; // Generate the input nodes and recursively construct the graph. for (var i = 0; i < topology[0]; i++) { // The first layer of the topology arg will be // representative of the inputs. inputs[i] = new Neuron(topology[0], rand); flattened.Add(inputs[i]); } // Recursively construct the graph. recurse(inputs, topology, 1); }
private void recurse(Neuron[] parents, int[] topology, int index) { if (parents == null || parents.Length == 0) return; if (topology == null || topology.Length == 0) return; if (index >= topology.Length) return; // Construct the next layer. Neuron[] layer = new Neuron[topology[index]]; for (var i = 0; i < layer.Length; i++) { // Create each node in this layer. layer[i] = new Neuron(parents.Length, rand); flattened.Add(layer[i]); } // "Wire up" the parents. for (var i = 0; i < parents.Length; i++) { parents[i].Children = layer; } // Recurse. recurse(layer, topology, ++index); }