Exemplo n.º 1
0
        /// <summary>
        /// Cycles through each of the <paramref name="trainingDatasetEntries"/> and applies the inputs to the <paramref name="network"/>. Afterwards, the <paramref name="network"/> outputs are
        /// tested against the expected values from the <paramref name="trainingDatasetEntries"/> to determine if each of the outputs is within the allowed
        /// TRAINING_FAULT_TOLERANCE level.
        /// </summary>
        /// <param name="network">The network that is being tested against.</param>
        /// <param name="trainingDatasetEntries">The training entries that test whether the network is trained or not.</param>
        /// <returns>Returns true/false indicating the the <paramref name="network"/> is trained according to the <paramref name="trainingDatasetEntries"/>.</returns>
        private bool GetIsNetworkTrained(IDFFNeuralNetwork network, IEnumerable <INetworkTrainingIteration> trainingDatasetEntries)
        {
            foreach (var entry in trainingDatasetEntries)
            {
                var networkOutputs = network.ApplyInputs(entry.Inputs);

                using (var networkOutputsEnumerator = networkOutputs.GetEnumerator())
                    using (var trainingOutputsEnumerator = entry.Outputs.GetEnumerator())
                    {
                        while (networkOutputsEnumerator.MoveNext() && trainingOutputsEnumerator.MoveNext())
                        {
                            var networkOutput  = networkOutputsEnumerator.Current;
                            var trainingOutput = trainingOutputsEnumerator.Current;

                            // If the network output-activation level is {TRAINING_ERROR_ALLOWANCE} away from the expected activation level
                            // for each output of each dataset entry, the network is considered trained.
                            if (Math.Abs(trainingOutput.ExpectedActivationLevel - networkOutput.ActivationLevel) > TRAINING_ERROR_ALLOWANCE)
                            {
                                return(false);
                            }
                        }
                    }
            }

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Trains the <paramref name="network"/> using <paramref name="trainingDatasetEntries"/> and return the average training cost of the final iteration.
        /// </summary>
        /// <param name="network">The neural network that is to be trained.</param>
        /// <param name="trainingDatasetEntries">The training iterations to train the network with.</param>
        /// <returns>Returns the average training cost of the final iteration.</returns>
        private IDFFNeuralNetwork TrainNetwork(IDFFNeuralNetwork network, IList <INetworkTrainingIteration> trainingDatasetEntries)
        {
            var priorTrainingCost = 999.0;
            var trainingIteration = 0;

            while (true)
            {
                trainingDatasetEntries.Shuffle();

                // TODO: Implement Clone() method for NeuralNetwork and store the BEST network.
                var trainingCost = network.Train(trainingDatasetEntries).Average(i => i.TrainingCost);

                Console.WriteLine($"{trainingIteration} : {trainingCost}");

                // Check the training cost every 500 iterations to ensure it's continuing to improve
                if (trainingIteration % 500 == 0)
                {
                    if ((priorTrainingCost - trainingCost) < .01)
                    {
                        break;
                    }
                    else
                    {
                        priorTrainingCost = trainingCost;
                    }
                }


                trainingIteration++;
            }

            return(network);
        }
Exemplo n.º 3
0
        public void Initialize()
        {
            _network     = new DFFNeuralNetwork(_inputLayerNeuronCount, _hiddenLayersCount, _hiddenLayerNeuronCount, _outputLayerNeuronCount);
            _inputLayer  = _network.Layers.OfType <IInputLayer>().First();
            _outputLayer = _network.Layers.OfType <IOutputLayer>().First();
            _hiddenLayer = _network.Layers.OfType <IHiddenLayer>().First();

            _trainingIterations = new List <INetworkTrainingIteration>();
        }
Exemplo n.º 4
0
        public void DFFNeuralNetwork_WithLayerNeuronCounts_InitializesMultipleHiddenLayers()
        {
            // Act
            _network = new DFFNeuralNetwork(3, 5, 5, 2);
            var hiddenLayers = _network.Layers.OfType <IHiddenLayer>();

            // Assert
            Assert.IsTrue(hiddenLayers.Count() == 5);
            Assert.IsTrue(hiddenLayers.All(l => l.Neurons.Count() == 5));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Fetches and applies the necessary network inputs to the network based on the identified network configuration.
        /// </summary>
        /// <param name="network">The neural network that the inputs will be applied to.</param>
        /// <param name="networkConfig">The NetworkConfiguration to pull inputs from.</param>
        /// <param name="companyId">The id of the company that the inputs are chosen for.</param>
        /// <param name="testDate">The date of the calculated inputs that will be applied to the <paramref name="network"/>.</param>
        /// <returns>Returns the network outputs after the config inputs have been applied.</returns>
        private IEnumerable <INetworkOutput> ApplyConfigInputsToNetwork(IDFFNeuralNetwork network, N networkConfig, Q quote)
        {
            var networkInputLayer = network.Layers?.OfType <IInputLayer>().First();
            var inputNeurons      = networkInputLayer.Neurons?.OfType <IInputNeuron>().ToList();
            var networkInputs     = _datasetService.GetNetworkInputs(quote.Id);

            MapInputsToInputNeurons(inputNeurons, networkInputs);

            // Apply inputs to network and returns network outputs.
            var resultingNetworkOutputs = network.ApplyInputs(networkInputs).ToList();

            var networkOutputLayer = network.Layers?.OfType <IOutputLayer>().First();
            var outputNeurons      = networkOutputLayer.Neurons?.OfType <IOutputNeuron>();

            return(MapOutputNeuronsToOutputs(outputNeurons, resultingNetworkOutputs));
        }
Exemplo n.º 6
0
        public void DFFNeuralNetwork_WithLayerNeuronCountsAndNoHiddenLayers_InitializesConnectionsBetweenInputAndOutputLayers()
        {
            // Arrange
            _network = new DFFNeuralNetwork(3, 0, 0, 2);

            var inputLayer  = _network.Layers.OfType <IInputLayer>().First();
            var outputLayer = _network.Layers.OfType <IOutputLayer>().First();

            // Assert
            Assert.IsTrue(inputLayer.Neurons.All(n => n.Connections.Count() == 2));
            Assert.IsTrue(inputLayer.Neurons.All(n => n.Connections.OfType <IOutgoingConnection>().Count() == 2));
            Assert.IsTrue(inputLayer.Neurons.All(n => n.Connections.OfType <IOutgoingConnection>().All(c => c.ToNeuron is IOutputNeuron)));

            Assert.IsTrue(outputLayer.Neurons.All(n => n.Connections.Count() == 3));
            Assert.IsTrue(outputLayer.Neurons.All(n => n.Connections.OfType <IIncomingConnection>().Count() == 3));
            Assert.IsTrue(outputLayer.Neurons.All(n => n.Connections.OfType <IIncomingConnection>().All(c => c.FromNeuron is IInputNeuron)));
        }
Exemplo n.º 7
0
        public void ApplyInputs_WithValidNetwork_AssignsActivationLevelsToInputNeurons()
        {
            // Arrange
            _network = new DFFNeuralNetwork(1, 1, 2, 2);

            var inputs = new List <INetworkInput>()
            {
                new NetworkInput()
                {
                    ActivationLevel = .75
                }
            };

            var inputNeuron = _network.Layers.OfType <IInputLayer>().First().Neurons.First();

            // Act
            _network.ApplyInputs(inputs);

            // Assert
            Assert.IsTrue(inputNeuron.ActivationLevel == .75);
        }