public void AddLayer(NeuralLayer layer) { // Don't give the first layer any dendrites yet. They will // be determined on FeedForward by the number of inputs. // Normally, you will want to have the same number of neurons as // inputs, but this adds flexibility for trying out different approaches. if (Layers.Count == 0) { Layers.Add(layer); return; } var currentLastLayer = Layers.Last(); foreach (var newLayerNeuron in layer.Neurons) { foreach (var lastLayerNeuron in currentLastLayer.Neurons) { foreach (var lastLayerOutput in lastLayerNeuron.OutputDendrites) { var newLayerNeuronDendrite = new Dendrite { Signal = lastLayerOutput.Signal }; newLayerNeuron.InputDendrites.Add(newLayerNeuronDendrite); } // Each neuron in the new layer is assigned a dendrite // corresponding to each neuron's output in the layer before. //var newLayerNeuronDendrite = new Dendrite { Signal = lastLayerNeuron.Output }; //newLayerNeuron.InputDendrites.Add(newLayerNeuronDendrite); } newLayerNeuron.ConfigureOutputDendrites(); } Layers.Add(layer); }
private void UpdateFirstLayerInputs(double[] inputs) { // Add dendrites to the first layer according to the number of inputs, // but only if dendrites do not yet exist. An interesting twist on this // might be to allow the inputs to vary so, say, different image sizes // could be passed in. Not sure how well that would work, and it would // require some changes to the existing code (weights, biases handled at // network model level rather than Neuron instantiation). var firstLayer = Layers.First(); if (firstLayer.Neurons.Sum(n => n.InputDendrites.Count()) < 1) { var dendritesPerNeuron = inputs.Count() / firstLayer.Neurons.Count(); foreach (var neuron in Layers.First().Neurons) { for (int i = 0; i < dendritesPerNeuron; i++) { var dendrite = new Dendrite(); neuron.InputDendrites.Add(dendrite); } } } // Assign values to all first layer inputs var dendrites = firstLayer.Neurons.SelectMany(n => n.InputDendrites).ToArray(); for (int i = 0; i < inputs.Count(); i++) { dendrites[i].Signal.Value = inputs[i]; } }