//Methods private BuildingInstr DefaultRegressionController(BuildingState buildingState) { BuildingInstr instructions = new BuildingInstr { CurrentIsBetter = IsBetter(buildingState.BinaryOutput, buildingState.CurrNetwork, buildingState.BestNetwork ), StopProcess = (BinaryOutput && buildingState.BestNetwork.TrainingBinErrorStat.TotalErrStat.Sum == 0 && buildingState.BestNetwork.TestingBinErrorStat.TotalErrStat.Sum == 0 && buildingState.CurrNetwork.CombinedPrecisionError > buildingState.BestNetwork.CombinedPrecisionError ) }; return(instructions); }
/// <summary> /// Builds trained network /// </summary> /// <returns>Trained network</returns> public TrainedNetwork Build() { TrainedNetwork bestNetwork = null; int lastImprovementEpoch = 0; double lastImprovementCombinedPrecisionError = 0d; double lastImprovementCombinedBinaryError = 0d; //Create network and trainer NonRecurrentNetUtils.CreateNetworkAndTrainer(_networkSettings, _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 TrainedNetwork currNetwork = new TrainedNetwork { NetworkName = _networkName, BinBorder = _binBorder, Network = net, TrainerInfoMessage = trainer.InfoMessage, TrainingErrorStat = net.ComputeBatchErrorStat(_trainingBundle.InputVectorCollection, _trainingBundle.OutputVectorCollection, out List <double[]> trainingComputedOutputsCollection) }; if (BinaryOutput) { currNetwork.TrainingBinErrorStat = new BinErrStat(_binBorder, 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 (BinaryOutput) { currNetwork.TestingBinErrorStat = new BinErrStat(_binBorder, testingComputedOutputsCollection, _testingBundle.OutputVectorCollection); currNetwork.CombinedBinaryError = Math.Max(currNetwork.CombinedBinaryError, currNetwork.TestingBinErrorStat.TotalErrStat.Sum); } //Expected precision accuracy currNetwork.ExpectedPrecisionAccuracy = Math.Min((1d - (currNetwork.TrainingErrorStat.ArithAvg / currNetwork.Network.OutputRange.Span)), (1d - (currNetwork.TestingErrorStat.ArithAvg / currNetwork.Network.OutputRange.Span))); //Expected binary accuracy if (BinaryOutput) { currNetwork.ExpectedBinaryAccuracy = Math.Min((1d - currNetwork.TrainingBinErrorStat.TotalErrStat.ArithAvg), (1d - currNetwork.TestingBinErrorStat.TotalErrStat.ArithAvg)); } else { currNetwork.ExpectedBinaryAccuracy = double.NaN; } //Restart lastImprovementEpoch when new trainer's attempt started if (trainer.AttemptEpoch == 1) { lastImprovementEpoch = trainer.AttemptEpoch; lastImprovementCombinedPrecisionError = currNetwork.CombinedPrecisionError; lastImprovementCombinedBinaryError = currNetwork.CombinedBinaryError; } //First initialization of the best network bestNetwork = bestNetwork ?? currNetwork.DeepClone(); //RegrState instance BuildingState regrState = new BuildingState(_networkName, _binBorder, _foldNum, _numOfFolds, _foldNetworkNum, _numOfFoldNetworks, trainer.Attempt, trainer.MaxAttempt, trainer.AttemptEpoch, trainer.MaxAttemptEpoch, currNetwork, bestNetwork, lastImprovementEpoch); //Call controller BuildingInstr instructions = _controller(regrState); //Better? if (instructions.CurrentIsBetter) { //Adopt current regression unit as a best one bestNetwork = currNetwork.DeepClone(); regrState.BestNetwork = bestNetwork; lastImprovementEpoch = trainer.AttemptEpoch; lastImprovementCombinedPrecisionError = currNetwork.CombinedPrecisionError; lastImprovementCombinedBinaryError = currNetwork.CombinedBinaryError; } if (currNetwork.CombinedBinaryError < lastImprovementCombinedBinaryError || currNetwork.CombinedPrecisionError < lastImprovementCombinedPrecisionError) { lastImprovementEpoch = trainer.AttemptEpoch; lastImprovementCombinedPrecisionError = currNetwork.CombinedPrecisionError; lastImprovementCombinedBinaryError = currNetwork.CombinedBinaryError; } //Raise notification event RegressionEpochDone(regrState, instructions.CurrentIsBetter); //Process instructions if (instructions.StopProcess) { break; } else if (instructions.StopCurrentAttempt) { if (!trainer.NextAttempt()) { break; } } }//while (iteration) //Create statistics of the best network weights bestNetwork.OutputWeightsStat = bestNetwork.Network.ComputeWeightsStat(); return(bestNetwork); }