コード例 #1
0
            public double[] Compute(Options options, double[] inputs)
            {
                var inputLayer = Layers[0];

                if (inputs.Length != inputLayer.Neurons.Count)
                {
                    throw new ArgumentException($"Invalid number of inputs. Expected: {inputLayer.Neurons.Count}");
                }

                for (int i = 0; i < inputs.Length; i++)
                {
                    inputLayer.Neurons[i].Value = inputs[i];
                }

                var prevLayer = inputLayer; // Previous layer is input layer.

                for (var i = 1; i < Layers.Count; i++)
                {
                    for (var j = 0; j < Layers[i].Neurons.Count; j++)
                    {
                        // For each Neuron in each layer.
                        var sum = 0.0;
                        for (var k = 0; k < prevLayer.Neurons.Count; k++)
                        {
                            // Every Neuron in the previous layer is an input to each Neuron in
                            // the next layer.
                            sum += prevLayer.Neurons[k].Value *
                                   Layers[i].Neurons[j].Weights[k];
                        }

                        // Compute the activation of the Neuron.
                        Layers[i].Neurons[j].Value = options.Activation(sum);
                    }
                    prevLayer = Layers[i];
                }

                // All outputs of the Network.
                var lastLayer = Layers.Last();

                return(lastLayer.Neurons.Select(n => n.Value).ToArray());
            }