Example #1
0
        //Methods
        /// <summary>
        /// The default implementation of the build process controler.
        /// </summary>
        protected BuildInstr DefaultNetworkBuildController(BuildProgress buildProgress)
        {
            BuildInstr instructions = new BuildInstr
            {
                CurrentIsBetter = IsBetter(buildProgress.CurrNetwork,
                                           buildProgress.BestNetwork
                                           ),
                StopProcess = (buildProgress.CurrNetwork.HasBinErrorStats &&
                               buildProgress.BestNetwork.TrainingBinErrorStat.TotalErrStat.Sum == 0 &&
                               buildProgress.BestNetwork.TestingBinErrorStat.TotalErrStat.Sum == 0 &&
                               buildProgress.CurrNetwork.CombinedPrecisionError > buildProgress.BestNetwork.CombinedPrecisionError
                               )
            };

            return(instructions);
        }
Example #2
0
        /// <summary>Build the SourceStruct using the SoucreObject sequence from a SourceFile</summary>
        /// <param name="lastSrcStruct">Ref: loaded with the last SourceStruct created previously or null if none. Gets set to the last created this call</param>
        /// <param name="srcFile">The SourceFile to use for building the SourceStruct from</param>
        /// <returns>The 1st SourceStruct created by this call ...</returns>
        internal SourceStruct Build(ref SourceStruct lastSrcStruct, SourceFile srcFile)
        {
            BuildInstr   instr = BuildInstr.Continue;  // continue until a struct is identified
            SourceObject start = srcFile.Sequence;     // the start object in a sequence of Struct objects to process
            SourceObject next = start;                 // the next object to process
            SourceStruct first = null;                 // the 1st struct built upon this call
            SourceStruct previous, build = null;

            NextStateFunc.BeginParse();
            while (next != null)
            {
                ParseResponse response = NextStateFunc.GoToNextState(next);
                switch (response)
                {
                case ParseResponse.Accept:
                    if (first == null)
                    {
                        first = build;
                    }
                    break;

                case ParseResponse.Resume:
                    break;

                case ParseResponse.Next:
                case ParseResponse.Call:
                    next = next.Sequence;
                    break;

                default:
                    throw new InvalidEnumValueException(typeof(ParseResponse), response);
                }
            }

            // TODO: add check for source file completion, i.e. nothing left on the stack

            return(first);
        }
Example #3
0
        /// <summary>
        /// Builds the trained network.
        /// </summary>
        /// <returns>The trained network.</returns>
        public TNRNet Build()
        {
            TNRNet bestNetwork                     = null;
            int    bestNetworkAttempt              = 0;
            int    bestNetworkAttemptEpoch         = 0;
            int    currNetworkLastImprovementEpoch = 0;
            double currNetworkLastImprovementCombinedPrecisionError = 0d;
            double currNetworkLastImprovementCombinedBinaryError    = 0d;

            //Create network and trainer
            NonRecurrentNetUtils.CreateNetworkAndTrainer(_networkCfg,
                                                         _trainingBundle.InputVectorCollection,
                                                         _trainingBundle.OutputVectorCollection,
                                                         _rand,
                                                         out INonRecurrentNetwork net,
                                                         out INonRecurrentNetworkTrainer trainer
                                                         );
            //Iterate training cycles
            while (trainer.Iteration())
            {
                //Compute current error statistics after training iteration
                //Training data part
                TNRNet currNetwork = new TNRNet(_networkName, _networkOutput)
                {
                    Network            = net,
                    TrainerInfoMessage = trainer.InfoMessage,
                    TrainingErrorStat  = net.ComputeBatchErrorStat(_trainingBundle.InputVectorCollection, _trainingBundle.OutputVectorCollection, out List <double[]> trainingComputedOutputsCollection)
                };
                if (TNRNet.IsBinErrorStatsOutputType(_networkOutput))
                {
                    currNetwork.TrainingBinErrorStat = new BinErrStat(BoolBorder, trainingComputedOutputsCollection, _trainingBundle.OutputVectorCollection);
                    currNetwork.CombinedBinaryError  = currNetwork.TrainingBinErrorStat.TotalErrStat.Sum;
                }
                currNetwork.CombinedPrecisionError = currNetwork.TrainingErrorStat.ArithAvg;
                //Testing data part
                currNetwork.TestingErrorStat       = net.ComputeBatchErrorStat(_testingBundle.InputVectorCollection, _testingBundle.OutputVectorCollection, out List <double[]> testingComputedOutputsCollection);
                currNetwork.CombinedPrecisionError = Math.Max(currNetwork.CombinedPrecisionError, currNetwork.TestingErrorStat.ArithAvg);
                if (TNRNet.IsBinErrorStatsOutputType(_networkOutput))
                {
                    currNetwork.TestingBinErrorStat = new BinErrStat(BoolBorder, testingComputedOutputsCollection, _testingBundle.OutputVectorCollection);
                    currNetwork.CombinedBinaryError = Math.Max(currNetwork.CombinedBinaryError, currNetwork.TestingBinErrorStat.TotalErrStat.Sum);
                }
                //Restart lastImprovementEpoch when new trainer's attempt started
                if (trainer.AttemptEpoch == 1)
                {
                    currNetworkLastImprovementEpoch = trainer.AttemptEpoch;
                    currNetworkLastImprovementCombinedPrecisionError = currNetwork.CombinedPrecisionError;
                    if (TNRNet.IsBinErrorStatsOutputType(_networkOutput))
                    {
                        currNetworkLastImprovementCombinedBinaryError = currNetwork.CombinedBinaryError;
                    }
                }
                //First initialization of the best network
                if (bestNetwork == null)
                {
                    bestNetwork        = currNetwork.DeepClone();
                    bestNetworkAttempt = trainer.Attempt;
                }
                if ((TNRNet.IsBinErrorStatsOutputType(_networkOutput) && currNetwork.CombinedBinaryError < currNetworkLastImprovementCombinedBinaryError) ||
                    currNetwork.CombinedPrecisionError < currNetworkLastImprovementCombinedPrecisionError
                    )
                {
                    currNetworkLastImprovementCombinedPrecisionError = currNetwork.CombinedPrecisionError;
                    if (TNRNet.IsBinErrorStatsOutputType(_networkOutput))
                    {
                        currNetworkLastImprovementCombinedBinaryError = currNetwork.CombinedBinaryError;
                    }
                    currNetworkLastImprovementEpoch = trainer.AttemptEpoch;
                }
                //BuildProgress instance
                BuildProgress buildProgress = new BuildProgress(_networkName,
                                                                trainer.Attempt,
                                                                trainer.MaxAttempt,
                                                                trainer.AttemptEpoch,
                                                                trainer.MaxAttemptEpoch,
                                                                currNetwork,
                                                                currNetworkLastImprovementEpoch,
                                                                bestNetwork,
                                                                bestNetworkAttempt,
                                                                bestNetworkAttemptEpoch
                                                                );
                //Call controller
                BuildInstr instructions = _controller(buildProgress);
                //Better?
                if (instructions.CurrentIsBetter)
                {
                    //Adopt current regression unit as a best one
                    bestNetwork             = currNetwork.DeepClone();
                    bestNetworkAttempt      = trainer.Attempt;
                    bestNetworkAttemptEpoch = trainer.AttemptEpoch;
                    //Update build progress
                    buildProgress.BestNetwork                = bestNetwork;
                    buildProgress.BestNetworkAttemptNum      = bestNetworkAttempt;
                    buildProgress.BestNetworkAttemptEpochNum = bestNetworkAttemptEpoch;
                }
                //Raise notification event
                NetworkBuildProgressChanged?.Invoke(buildProgress);
                //Process instructions
                if (instructions.StopProcess)
                {
                    break;
                }
                else if (instructions.StopCurrentAttempt)
                {
                    if (!trainer.NextAttempt())
                    {
                        break;
                    }
                }
            }//while (iteration)
            //Create statistics of the best network weights
            bestNetwork.NetworkWeightsStat = bestNetwork.Network.ComputeWeightsStat();
            return(bestNetwork);
        }