public void DataTransferProcess(double min, double max, int count, double a, double b, double c, string function, double significanceLevel) { var range = _rangeBuilder.Min(min) .Max(max) .A(a) .B(b) .C(c) .Count(count) .Function(function) .Build(); _sender.MessageIsSent += _receiver.Receive; _sender.SendMessage(range, function, significanceLevel); var errorCalculator = new ErrorCalculator(); var structuredRecorder = (StructureRecorder)Recorder; var source = structuredRecorder.Source.Select(it => it.Y).ToList(); var @fixed = structuredRecorder.ReceivedMessage.ToList(); var e = errorCalculator.E(source, @fixed); var ea = errorCalculator.ECoefficient(a, structuredRecorder.A); var eb = errorCalculator.ECoefficient(b, structuredRecorder.B); var ec = errorCalculator.ECoefficient(c, structuredRecorder.C); Recorder.RecordErrors(e, ea, eb, ec); }
private void CalculateErrors(Dictionary <String, double> expected, Dictionary <String, double> actual) { foreach (string key in errorList.Keys) { ErrorCalculator ec = (ErrorCalculator)errorList[key]; double val = ec.CalculateError(expected, actual); Accumulate(key, val); } }
public IForecastingModel TrainNewModel(double[][] iInput, double[][] iOutput) { int inputSize = iInput[0].Length, samplesNum = iOutput.Length; if (samplesNum != iInput.Length) { throw new ArgumentException(); } for (int i = 0; i < samplesNum; ++i) { if (iInput[i].Length != inputSize || iOutput[i].Length != 1) //iInput isn't a square matrix or iOutput isn't a vector { throw new ArgumentException(); } } int[] neuronsCount = (int[])ModelParametersDict[NeuronsInLayersKey]; string activationFunction = (string)ModelParametersDict[ActivationFunctionKey]; long maxIterNum = (long)ModelParametersDict[MaxIterationsNumberKey]; double stopError = (double)ModelParametersDict[StopErrorKey]; ActivationNetwork netToTrain = new ActivationNetwork(ActivationFunctionsDict[activationFunction], inputSize, neuronsCount); DataNormalizer normalizer = new DataNormalizer(iInput.Concat(iOutput).ToArray()); IForecastingModel aModel = new ANNforecastingModel(netToTrain, normalizer); ISupervisedLearning teacher = new ResilientBackpropagationLearning(netToTrain); double[][] trainInputSet, trainOutputSet; TrainingSubsetGenerator.GenerateRandomly(iInput, iOutput, out trainInputSet, out trainOutputSet, iMultiplier: TrainSubsetMultiplier); trainInputSet = normalizer.Normalize(trainInputSet); trainOutputSet = normalizer.Normalize(trainOutputSet); long epochsCount = 0; double nextError = ErrorCalculator.CalculateMSE(aModel, iInput, iOutput), prevError; do { prevError = nextError; teacher.RunEpoch(trainInputSet, trainOutputSet); nextError = ErrorCalculator.CalculateMSE(aModel, iInput, iOutput); }while (epochsCount++ <= maxIterNum && Math.Abs(prevError - nextError) >= stopError); return(aModel); }
public Network Train(Network net) { Console.WriteLine("=======Training Started========"); var iterationError = new double[net.InputLayer.Trainset.Length]; double errorRate; do { for (var i = 0; i < net.InputLayer.Trainset.Length; ++i) { net.HiddenLayer.SetData(net.InputLayer.Trainset[i].Item1.ToDoubles()); net.HiddenLayer.Recognize(null, net.OutputLayer); net.OutputLayer.Recognize(net, null); var errors = new double[net.InputLayer.Trainset[i].Item2.OpCount]; for (var x = 0; x < errors.Length; ++x) { errors[x] = net.InputLayer.Trainset[i].Item2[x] - net.FactResult[x]; } iterationError[i] = ErrorCalculator.CalcIterationError(errors); var tempGsums = net.OutputLayer.BackwardPass(errors); net.HiddenLayer.BackwardPass(tempGsums); } errorRate = ErrorCalculator.CalcRoundError(iterationError); Console.WriteLine($"Round error: {errorRate}"); } while (errorRate > _allowedErrorRate); net.HiddenLayer.InitWeights(MemoryModes.Set); net.OutputLayer.InitWeights(MemoryModes.Set); Console.WriteLine("========Training Ended========"); return(net); }
/// <summary> /// The error calculator computes the error from the input values /// and the output values. By default this is the sum of squares. /// </summary> /// <param name="name">The name of the error calculator</param> /// <param name="calculator">The calculator to set</param> public void AddCalculator(string name, ErrorCalculator calculator) { errorManager.AddCalculator(name, calculator); }
/// <summary> /// The error calculator computes the error from the input values /// and the output values. By default this is sum of squares. This /// version of the addCalculator uses the default name for the /// calculator. /// </summary> /// <param name="calculator">The calculator to set</param> public void AddCalculator(ErrorCalculator calculator) { errorManager.AddCalculator(calculator.DefaultName, calculator); }
public static double Evaluate(Math::Matrix <double> predictions, Math::Matrix <double> shouldBe, ErrorCalculator ec) { double errorSum = 0; for (int row = 0; row < predictions.RowCount; row++) { double desiredSum = 0; double actualSum = 0; for (int col = 0; col < predictions.ColumnCount; col++) { desiredSum += shouldBe[row, col]; actualSum += predictions[row, col]; } errorSum += ec.CalculateError(actualSum, desiredSum); } return(errorSum / (predictions.RowCount)); }
/// <summary> /// Adds a new calculator with the given name to the error /// manager. /// </summary> /// <param name="calculator">The calculator to add</param> /// <param name="name">The name of the calculator</param> public void AddCalculator(string name, ErrorCalculator calculator) { calculators[name] = calculator; errorReport[name] = 0.0; }
public void TrainNetwork(ref NeuralNetwork networkToTrain, int maxEpochs, double desiredErrorRate = 0) { var learnSet = DataProvider.LearnSet; //shorter var TempTestErrorHistory = Vector <double> .Build.Dense(maxEpochs + 1); Vector <double> TemporaryEpochErrorHistory = Vector <double> .Build.Dense(maxEpochs, 0); //place to temporary store epoch errors for all epochs TempTestErrorHistory[0] = Double.MaxValue; // assume error at beginning is maximal TemporaryEpochErrorHistory[0] = Double.MaxValue; // assume error at beginning is maximal int shuffleAmount = (int)Math.Round(Math.Log(learnSet.Length, 2)); // calculate how much should shuffle learn set depending on its size Vector <double> output; Vector <double> errorVector; int EpochIndex = 1; // epochs are counted starting from 1 while (EpochIndex < maxEpochs) { DataProvider.ShuffleDataSet(learnSet, shuffleAmount); // shuffle some data in learn set CurrentEpochErrorVector = Vector <double> .Build.Dense(learnSet.Length, 0); // init with 0s #region calculate epoch for (int dataIndex = 0; dataIndex < learnSet.Length; dataIndex++) { output = networkToTrain.CalculateOutput(learnSet[dataIndex].X, CalculateMode.OutputsAndDerivatives); errorVector = ErrorCalculator.CalculateErrorVector(output, learnSet[dataIndex].D); CurrentEpochErrorVector[dataIndex] = ErrorCalculator.CalculateErrorSum(output, learnSet[dataIndex].D); #region adapt weights LearningAlgorithm.AdaptWeights(networkToTrain, errorVector, CurrentEpochErrorVector[dataIndex], CurrentEpochErrorVector[dataIndex.Previous()]); #endregion } #endregion #region epoch error TemporaryEpochErrorHistory[EpochIndex] = ErrorCalculator.CalculateEpochError(CurrentEpochErrorVector); if (TemporaryEpochErrorHistory[EpochIndex] <= desiredErrorRate) //learning is done { return; } #endregion #region Adapt Learning Rate LearningAlgorithm.AdaptLearningRate(TemporaryEpochErrorHistory[EpochIndex], TemporaryEpochErrorHistory[EpochIndex.Previous()]); #endregion #region create and store test results var testError = tester.TestNetwork(networkToTrain, DataProvider); TempTestErrorHistory[EpochIndex] = testError; #endregion #region update best network state if (TempTestErrorHistory[EpochIndex] < BestError) { BestNetworkState = networkToTrain.DeepCopy(); BestError = TempTestErrorHistory[EpochIndex]; } #endregion EpochIndex++; } #region save errors for all epochs that actually were calculated TestErrorHistory = Vector <double> .Build.Dense(EpochIndex); EpochErrorHistory = Vector <double> .Build.Dense(EpochIndex); TemporaryEpochErrorHistory.CopySubVectorTo(EpochErrorHistory, 0, 0, EpochIndex); TempTestErrorHistory.CopySubVectorTo(TestErrorHistory, 0, 0, EpochIndex); #endregion //restore best network state ( on set for verifying) networkToTrain = BestNetworkState; }
/// <summary> /// Add an error calculator with the given name, as opposed to the default /// name of the calculator. /// </summary> /// <param name="ec">The error calculator</param> /// <param name="name">The name of the error calculator</param> public void AddErrorCalculator(ErrorCalculator ec, string name) { errorList[name] = ec; errorValues[name] = 0.0; }
/// <summary> /// Adds a new error calculator to the evaluator. /// </summary> /// <param name="ec">The error calculator</param> public void AddErrorCalculator(ErrorCalculator ec) { AddErrorCalculator(ec, ec.DefaultName); }