示例#1
0
        public Network(params int[] neuronCounts)
        {
            if (neuronCounts.Length < 2)
            {
                throw new ArgumentException("Not enough counts, need at least two for Input and Output layers");
            }

            // Create Layers
            inputLayer = new Layer(neuronCounts[0]);
            for (var i = 1; i < neuronCounts.Length - 1; i++)
            {
                hiddenLayers.Add(new Layer(neuronCounts[i]));
            }
            outputLayer = new Layer(neuronCounts[neuronCounts.Length - 1]);

            // Get All Neurons
            foreach (var neuron in inputLayer.neurons)
            {
                neurons.Add(neuron);
            }
            foreach (var layer in hiddenLayers)
            {
                foreach (var neuron in layer.neurons)
                {
                    neurons.Add(neuron);
                }
            }
            foreach (var neuron in outputLayer.neurons)
            {
                neurons.Add(neuron);
            }

            // Connect Layer Together
            if (hiddenLayers.Count > 0)
            {
                inputLayer.SetNext(hiddenLayers[0]);
                for (var i = 1; i < hiddenLayers.Count; i++)
                {
                    hiddenLayers[i - 1].SetNext(hiddenLayers[i]);
                }
                hiddenLayers[hiddenLayers.Count - 1].SetNext(outputLayer);
            }
            else
            {
                inputLayer.SetNext(outputLayer);
            }

            // Fully Connect Layers
            for (var layer = inputLayer.next; layer != null; layer = layer.next)
            {
                FullyConnect(layer.previous, layer);
            }

            // Randomize Weights and Biases
            foreach (var connection in connections)
            {
                connection.weight = random.NextDouble();
            }

            for (var layer = inputLayer; layer != null; layer = layer.next)
            {
                foreach (var neuron in layer.neurons)
                {
                    neuron.bias = random.NextDouble();
                }
            }
        }