public GeneticNeuronGenerator(
            INeuronActivator neuronActivator,
            int populationSize,
            int[] topology,
            params IGenerationUpdater[] generationUpdaters)
        {
            _neuralNetworkTopology = topology;
            _genotypeGenesAmount   = NeuralNetwork.CalculateWeightCount(topology);

            _currentPopulation = new GeneticNeuron[populationSize];
            EvolutionManager   = new EvolutionManager(_genotypeGenesAmount, populationSize, generationUpdaters);

            _neuronActivator = neuronActivator;
        }
Exemple #2
0
        public static NeuralNetwork CreateNeuralNetworkFromFile(string path, INeuronActivator neuronActivator)
        {
            XDocument doc = XDocument.Load(path);

            if (doc.Root == null)
            {
                throw new Exception("Document is empty");
            }

            XElement root            = doc.Root;
            var      biasList        = new List <string>(8);
            var      weightList      = new List <string>(8);
            var      inputCountList  = new List <string>(8);
            var      outputCountList = new List <string>(8);

            foreach (XElement layerElement in root.Elements(LayerId))
            {
                biasList.Add(layerElement.Attribute(BiasId)?.Value);
                inputCountList.Add(layerElement.Attribute(InputCountId)?.Value);
                outputCountList.Add(layerElement.Attribute(OutputCountId)?.Value);
                foreach (XElement element in layerElement.Elements())
                {
                    weightList.Add(element.Value);
                }
            }

            int[] topology = new int[inputCountList.Count + 1];
            for (int i = 0; i < inputCountList.Count; i++)
            {
                topology[i] = int.Parse(inputCountList[i]);
            }

            topology[topology.Length - 1] = int.Parse(outputCountList[outputCountList.Count - 1]);

            float[] weights = weightList.Select(float.Parse).ToArray();
            var     network = new NeuralNetwork(neuronActivator, topology);

            network.FillLayers(weights);

            for (int i = 0; i < biasList.Count; i++)
            {
                string biasString = biasList[i];
                if (!string.IsNullOrEmpty(biasString) && float.TryParse(biasString, out float bias))
                {
                    network.Layers[i].Bias = bias;
                }
            }

            return(network);
        }
Exemple #3
0
        public NeuralLayer(int inputCount, int outputCount, INeuronActivator neuronActivator = null)
        {
            _neuronActivator = neuronActivator;

            InputCount  = inputCount;
            OutputCount = outputCount;

            Weights = new float[InputCount + 1][];
            for (int i = 0; i < Weights.Length; i++)
            {
                Weights[i] = new float[OutputCount];
            }

            _biasedInputs = new float[InputCount + 1];
        }
        public NeuralNetwork(INeuronActivator neuronActivator, params int[] topology)
        {
#if DEBUG
            if (topology.Length <= 1)
            {
                throw new Exception("Network should have at least 1 layer");
            }
#endif

            TotalWeightCount = CalculateWeightCount(topology);
            Layers           = new NeuralLayer[topology.Length - 1];
            _layersOutput    = new float[Layers.Length][];
            for (int i = 0; i < Layers.Length; i++)
            {
                int outputCount = topology[i + 1];
                Layers[i]        = new NeuralLayer(topology[i], outputCount, neuronActivator);
                _layersOutput[i] = new float[outputCount];
            }
        }