public void test() { // initialize input and output values var input = new double[4][] { new double[] { 0, 0 }, new double[] { 0, 1 }, new double[] { 1, 0 }, new double[] { 1, 1 } }; var output = new double[4][] { new double[] { 0 }, new double[] { 1 }, new double[] { 1 }, new double[] { 1 } }; // create neural network var network = new ActivationNetwork( new SigmoidFunction(2), 2, // two inputs in the network //2, // two neurons in the first layer 1); // one neuron in the second layer // create teacher var teacher = new BackPropagationLearning(network); // loop while (true) { // run epoch of learning procedure var error = teacher.RunEpoch(input, output); // check error value to see if we need to stop // ... if (error < 0.001) { break; } } Console.WriteLine(network.Compute(new double[] { 0, 0 })[0] + "," + network.Compute(new double[] { 0, 1 })[0] + "," + network.Compute(new double[] { 1, 0 })[0] + "," + network.Compute(new double[] { 1, 1 })[0]); }
public virtual void Prepare() { PrepareData(); PrepareCharts(); network = new ActivationNetwork(new Tanh(0.2), Sizes[0], Sizes.Skip(1).ToArray()); network.ForEachWeight(z => rnd.NextDouble() * 2 - 1); teacher = new BackPropagationLearning(network); teacher.LearningRate = 1; Form = new Form() { Text = GetType().Name, Size = new Size(800, 600), FormBorderStyle = FormBorderStyle.FixedDialog, Controls = { AreaChart, HistoryChart } }; }
public void Test() { ActivationNetwork network = new ActivationNetwork( new SigmoidFunction(), 2, // two inputs in the network 2, // two neurons in the first layer 1); // one neuron in the second layer BackPropagationLearning teacher = new BackPropagationLearning(network); double lastError = double.MaxValue; int counter = 0; while (true) { counter++; var error = teacher.RunEpoch(input, output); if (lastError - error < 0.0000001 && error < 0.001) break; lastError = error; } //var bla = network.Compute(input[0])[0]; //var round = Math.Round(network.Compute(input[0])[0], 2); //var result = output[0][0]; //Assert.IsTrue(Math.Abs(round - result) < double.Epsilon); Assert.IsTrue(Math.Abs(network.Compute(input[0])[0] - output[0][0]) < 0.03); Assert.IsTrue(Math.Abs(network.Compute(input[1])[0] - output[1][0]) < 0.03); Assert.IsTrue(Math.Abs(network.Compute(input[2])[0] - output[2][0]) < 0.03); Assert.IsTrue(Math.Abs(network.Compute(input[3])[0] - output[3][0]) < 0.03); Console.WriteLine($"Loop counter = {counter}."); }
public void Learn() { var network = new ActivationNetwork(new BipolarSigmoidFunction(), Constants.StoneCount, 1); var teacher = new BackPropagationLearning(network);//new PerceptronLearning(network); var data = LoadData("4-6-2012-04-24.know"); double error = 1.0; int index = 0; while (error > 0.001 && index < 100000) { error = teacher.RunEpoch(data.Item1, data.Item2); index++; } network.Save("4-6-2012-04-24.bp.net"); var text = "□○○○●○○□○●●□□●□□"; var i = ToDouble(text);//-2 var o = network.Compute(i); var eval = o[0] * 2 * Constants.StoneCount - Constants.StoneCount; Console.WriteLine("{0} {1}", text, eval); }
protected virtual void CreateNetwork() { network = new ActivationNetwork(new Tanh(1), 1, 5, 1); network.ForEachWeight(z => rnd.NextDouble() * 2 - 1); teacher = new BackPropagationLearning(network); teacher.LearningRate = 1; }
protected override void CreateNetwork() { network = new ActivationNetwork(new Tanh(0.1), 1, 5, 1); network.ForEachWeight(z => rnd.NextDouble() * 2 - 1); teacher = new BackPropagationLearning(network); teacher.LearningRate = 1; teacher.Momentum = 0.3; }
public EstimationResult Estimate(IEnumerable<IDateValue> dateValues) { var data = dateValues.ToArray(); var samplesCount = data.Length - LayerWidth; var factor = 1.7 / data.Length; var yMin = data.Min(x => x.Value); var input = new double[samplesCount][]; var output = new double[samplesCount][]; for (var i = 0; i < samplesCount; i++) { input[i] = new double[LayerWidth]; output[i] = new double[1]; for (var j = 0; j < LayerWidth; j++) input[i][j] = (data[i + j].Value - yMin) * factor - 0.85; output[i][0] = (data[i + LayerWidth].Value - yMin) * factor - 0.85; } var network = new ActivationNetwork( new BipolarSigmoidFunction(SigmoidAlphaValue), LayerWidth, LayerWidth * 2, 1); var teacher = new BackPropagationLearning(network) { LearningRate = LearningRate, Momentum = Momentum }; var solutionSize = data.Length - LayerWidth; var solution = new double[solutionSize, 2]; var networkInput = new double[LayerWidth]; for (var j = 0; j < solutionSize; j++) solution[j, 0] = j + LayerWidth; TimesLoop.Do(Iterations, () => { teacher.RunEpoch(input, output); for (int i = 0, n = data.Length - LayerWidth; i < n; i++) { for (var j = 0; j < LayerWidth; j++) networkInput[j] = (data[i + j].Value - yMin) * factor - 0.85; solution[i, 1] = (network.Compute(networkInput)[0] + 0.85) / factor + yMin; } }); return EstimationResult.Create(solution[0, 1], this); }
public NeuralNetworkBot2() : base( "Neural Network Bot II", "Runs a neural network neural network to directly calculate outputs for each move", true) { _network = new ActivationNetwork(new SigmoidFunction(), 4*4, 4*4, 2); //_network.Randomize(); Load(); _teacher = new BackPropagationLearning(_network); _teacher.LearningRate = 0.05; _teacher.Momentum = 0.05; }
static void Main(string[] args) { // initialize input and output values double[][] input = new double[4][] { new double[] {0, 0}, new double[] {0, 1}, new double[] {1, 0}, new double[] {1, 1} }; double[][] output = new double[4][] { new double[] {0}, new double[] {1}, new double[] {1}, new double[] {0} }; // create neural network ActivationNetwork network = new ActivationNetwork( new SigmoidFunction(1), 2, // two inputs in the network 2, // two neurons in the first layer 1); // one neuron in the second layer // create teacher BackPropagationLearning teacher = new BackPropagationLearning(network); // loop for (int i = 0; i < 10000; i++) { // run epoch of learning procedure double error = teacher.RunEpoch(input, output); // check error value to see if we need to stop // ... Console.Out.WriteLine("#" + i + "\t" + error); } double[] ret1 = network.Compute(new double[] { 0, 0 }); double[] ret2 = network.Compute(new double[] { 1, 0 }); double[] ret3 = network.Compute(new double[] { 0, 1 }); double[] ret4 = network.Compute(new double[] { 1, 1 }); Console.Out.WriteLine(); Console.Out.WriteLine("Eval(0, 0) = " + ret1[0]); Console.Out.WriteLine("Eval(1, 0) = " + ret2[0]); Console.Out.WriteLine("Eval(0, 1) = " + ret3[0]); Console.Out.WriteLine("Eval(1, 1) = " + ret4[0]); Console.ReadLine(); }
public NeuralNetworkOperations(int characterSize) { neuralNet = new ActivationNetwork(new BipolarSigmoidFunction(2.0f), characterSize, 400, characterCount); neuralNet.Randomize(); teacher = new AForge.Neuro.Learning.BackPropagationLearning(neuralNet); teacher.LearningRate = 0.5f; teacher.Momentum = 0.1f; prepareDataForTeacher(); //var letters = treningLetterListInput.Zip(treningLetterListOutput, (i,o) => new { treningLetterListInput = i, treningLetterListOutput = o }); double err = 400.0f; int count = 0; while(err >= 30.0f) { err = teacher.RunEpoch(treningLetterListInput.ToArray(), treningLetterListOutput.ToArray()); count++; } }
public string Learn(string knowFile) { var network = new ActivationNetwork(new BipolarSigmoidFunction(), Constants.StoneCount, 1); var teacher = new BackPropagationLearning(network); //new PerceptronLearning(network); var data = LoadData(knowFile); double error = int.MaxValue; int index = 0; while (error > 1.0 && index++ < 5000) { error = teacher.RunEpoch(data.Item1, data.Item2); } var networkFile = knowFile + ".net"; network.Save(networkFile); Console.WriteLine("Learn: {0}, Gen: {1}", knowFile, networkFile); return networkFile; }
public void Test() { var network = new ActivationNetwork(new SigmoidFunction(), inputCount, firstLayerNeurons, secondLayerNeurons, thirdLayerNeurons, lastLayerNeurons); var teacher = new BackPropagationLearning(network); var lastError = double.MaxValue; var counter = 0; while (true) { counter++; var error = teacher.RunEpoch(input, output); if ((lastError - error < 0.00001 && error < 0.01) || counter > 1200000) break; lastError = error; } var result1 = network.Compute(new double[] {1, 0, 1, 0, 1, 0, 1, 0}); Console.WriteLine($"2 + 2, 2 * 2 = {result1[0]}, {result1[1]}"); var result2 = network.Compute(new double[] {0, 1, 0, 1, 1, 0, 0, 1}); Console.WriteLine($"1 + 1, 2 * 1 = {result2[0]}, {result2[1]}"); var result3 = network.Compute(new double[] {1, 0, 1, 0, 0, 1, 0, 0}); Console.WriteLine($"2 + 2, 1 * 0 = {result3[0]}, {result3[1]}"); var result4 = network.Compute(new double[] {0, 1, 0, 0, 0, 1, 1, 0}); Console.WriteLine($"1 + 0, 1 * 2 = {result4[0]}, {result4[1]}"); }
static void Main(string[] args) { Console.WriteLine("This is a demo application that combines Linear Discriminant Analysis (LDA) and Multilayer Perceptron(MLP)."); double[,] inputs = { { 4, 1 }, { 2, 4 }, { 2, 3 }, { 3, 6 }, { 4, 4 }, { 9, 10 }, { 6, 8 }, { 9, 5 }, { 8, 7 }, { 10, 8 } }; int[] output = { 1, 1, 2, 1, 1, 2, 2, 2, 1, 2 }; Console.WriteLine("\r\nProcessing sample data, pease wait..."); //1.1 var lda = new LinearDiscriminantAnalysis(inputs, output); //1.2 Compute the analysis lda.Compute(); //1.3 double[,] projection = lda.Transform(inputs); //both LDA and MLP have a little bit different inputs //e.x double[,] to double[][], etc. //e.x. LDA needs int classes and MLP needs classes to be in the range [0..1] #region convertions int vector_count = projection.GetLength(0); int dimensions = projection.GetLength(1); //==================================================================== // conver for NN double[][] input2 = new double[vector_count][]; double[][] output2 = new double[vector_count][]; for (int i = 0; i < input2.Length; i++) { input2[i] = new double[projection.GetLength(1)]; for (int j = 0; j < projection.GetLength(1); j++) { input2[i][j] = projection[i, j]; } output2[i] = new double[1]; //we turn classes from ints to doubles in the range [0..1], because we use sigmoid for the NN output2[i][0] = Convert.ToDouble(output[i]) / 10; } #endregion //2.1 create neural network ActivationNetwork network = new ActivationNetwork( new SigmoidFunction(2), dimensions, // inputs neurons in the network dimensions, // neurons in the first layer 1); // one neuron in the second layer //2.2 create teacher BackPropagationLearning teacher = new BackPropagationLearning(network); //2.3 loop int p = 0; while (true) { // run epoch of learning procedure double error = teacher.RunEpoch(input2, output2); p++; if (p > 1000000) break; // instead of iterations we can check error values to see if we need to stop } //==================================================================== //3. Classify double[,] sample = { { 10, 8 } }; double[,] projectedSample = lda.Transform(sample); double[] projectedSample2 = new double[2]; projectedSample2[0] = projectedSample[0, 0]; projectedSample2[1] = projectedSample[0, 1]; double[] classs = network.Compute(projectedSample2); Console.WriteLine("========================"); //we convert back to int classes by first rounding and then multipling by 10 (because we devided to 10 before) //if you do not get the expected result //- rounding might be a problem //- try more training Console.WriteLine(Math.Round(classs[0], 1, MidpointRounding.AwayFromZero)*10); Console.ReadLine(); }
private static NeuralNetworkEvaluator evaluateSingleLayerActivationNetworkWithSigmoidFunctionBackPropagationLearning( double[][] input, double[][] output, double[][] crossValidationInput, char[] crossValidationDataLabels, double[][] evaluationInput, char[] evaluationDataLabels, double learningRate, string networkName) { //Create the neural Network BipolarSigmoidFunction sigmoidFunction = new BipolarSigmoidFunction(2.0f); ActivationNetwork neuralNet = new ActivationNetwork(sigmoidFunction, input[0].Length, ClassifierHelpers.NUM_CHAR_CLASSES); //Randomise the networks initial weights neuralNet.Randomize(); //Create teacher that the network will use to learn the data (Back Propogation Learning technique used here) BackPropagationLearning teacher = new BackPropagationLearning(neuralNet); teacher.LearningRate = LEARNING_RATE; //Train the Network //trainNetwork(neuralNet, teacher, input, output, crossValidationInput, crossValidationDataLabels); //Train multiple networks, pick the one that performs best on the Cross-Validation data neuralNet = trainNetworksCompeteOnCrossValidation(neuralNet, teacher, input, output, crossValidationInput, crossValidationDataLabels); //Evaluate the network returned on the cross-validation data so it can be compared to the current best NeuralNetworkEvaluator crossValEvaluator = new NeuralNetworkEvaluator(neuralNet); crossValEvaluator.Evaluate(crossValidationInput, crossValidationDataLabels); //See if this network is better than the current best network of it's type //Try and load a previous network of this type string previousNetworkPath = Program.NEURAL_NETWORKS_PATH + networkName + Program.NEURAL_NETWORK_FILE_EXTENSION; string networkCMPath = Program.NEURAL_NETWORKS_PATH + networkName + ".csv"; bool newBest = false; ActivationNetwork bestNetwork = neuralNet; if(File.Exists(previousNetworkPath)) { //Load the previous network & evaluate it ActivationNetwork previous = ActivationNetwork.Load(previousNetworkPath) as ActivationNetwork; NeuralNetworkEvaluator prevCrossValEval = new NeuralNetworkEvaluator(previous); prevCrossValEval.Evaluate(crossValidationInput, crossValidationDataLabels); //If this network is better than the previous best, write it out as the new best if(prevCrossValEval.ConfusionMatrix.NumMisclassifications > crossValEvaluator.ConfusionMatrix.NumMisclassifications) { DefaultLog.Info("New best cross-validation score for network \"{0}\". Previous was {1}/{2}, new best is {3}/{2}", networkName, prevCrossValEval.ConfusionMatrix.NumMisclassifications, prevCrossValEval.ConfusionMatrix.TotalClassifications, crossValEvaluator.ConfusionMatrix.NumMisclassifications); //Delete the old files File.Delete(previousNetworkPath); File.Delete(networkCMPath); newBest = true; } else //The previous network is still the best { DefaultLog.Info("Existing \"{0}\" network performed better than new one. New network scored {1}/{2}, existing scored {3}/{2}", networkName, crossValEvaluator.ConfusionMatrix.NumMisclassifications, crossValEvaluator.ConfusionMatrix.TotalClassifications, prevCrossValEval.ConfusionMatrix.NumMisclassifications); bestNetwork = previous; } } else //Otherwise there isn't a previous best { DefaultLog.Info("No previous best record for network \"{0}\" . . .", networkName); newBest = true; } //Evaluate the best system on the evaluation data NeuralNetworkEvaluator evaluator = new NeuralNetworkEvaluator(bestNetwork); evaluator.Evaluate(evaluationInput, evaluationDataLabels); //If there is a new best to write out if(newBest) { DefaultLog.Info("Writing out net best network of type\"{0}\"", networkName); neuralNet.Save(previousNetworkPath); //Write out the Confusion Matrix for the evaluation data, not cross-validation evaluator.ConfusionMatrix.WriteToCsv(networkCMPath); DefaultLog.Info("Finished writing out network \"{0}\"", networkName); } return evaluator; }
/* * Naucz sieć neuronową. * Uczy perceptron przy pomocy algorytmu wstecznej propagacji. * Sieć oczy się doputy, dopuki błąd uczenia w danej epoce nie będzie mniejszy niż * dany margines błędu (this.errorRate). Zwraca ilość epok nauczania. * double [][] input - tablica wektorów wejściowych * double [][] output - tablica oczekiwanych wektorów wyjściowych */ public int Learn(double [][] input, double [][] output) { if (input.Length == output.Length) { double samples = (double)input.Length; this.network = new ActivationNetwork(new BipolarSigmoidFunction(this.sigmoidAlphaValue), input[0].Length, this.neuronsInFirstLayer, 1); BackPropagationLearning teacher = new BackPropagationLearning(network); teacher.LearningRate = this.learningRate; teacher.Momentum = this.momentum; int epoch = 0; while (teacher.RunEpoch(input, output) < this.errorRate) { epoch++; } return epoch; } else { throw new Exception("Size of input and output arrays differs!"); } }
static void Learn() { var network = new ActivationNetwork( new SigmoidFunction(), baseMaker.InputSize, arguments.NeuronsCount, baseMaker.OutputSize ); network.Randomize(); foreach (var l in network.Layers) foreach (var n in l.Neurons) for (int i = 0; i < n.Weights.Length; i++) n.Weights[i] = rnd.NextDouble() * 2 - 1; var teacher = new BackPropagationLearning(network); teacher.LearningRate = 1; teacher.Momentum = 0; while (true) { var watch = new Stopwatch(); watch.Start(); while (watch.ElapsedMilliseconds < 500) { teacher.RunEpoch(baseMaker.Inputs, baseMaker.Answers); } watch.Stop(); var count = 0; percentage = new double[baseMaker.OutputSize, baseMaker.OutputSize]; for (int i = 0; i < baseMaker.OutputSize; i++) for (int j = 0; j < baseMaker.OutputSize * 5; j++) { var task = baseMaker.GenerateRandom(i); var output = network.Compute(task); var max = output.Max(); var maxIndex = Enumerable.Range(0, output.Length).Where(z => output[z] == max).First(); percentage[i, maxIndex]++; if (i != maxIndex) totalErrors++; count++; } var maxPercentage = percentage.Cast<double>().Max(); for (int i = 0; i < baseMaker.OutputSize; i++) for (int j = 0; j < baseMaker.OutputSize; j++) percentage[i, j] /= maxPercentage; totalErrors /= count; form.BeginInvoke(new Action(Update)); } }
public static void ExactTrainingData() { TrainData[] tdatas; // AForge.Neuro.Learning.BackPropagationLearning beural=new AForge.Neuro.Learning.BackPropagationLearning(new AForge.Neuro.ActivationNetwork( tdatas = traindatas.ToArray(); double[][] output = new double[tdatas.Length-1][]; double[][] input = new double[tdatas.Length-1][]; for (int i = 1; i < tdatas.Length; i++) { int spddiff = 0, voldiff = 0, occdiff = 0, u_spddifft = 0, u_voldifft = 0, u_occdifft = 0, d_spddifft = 0, d_voldifft = 0, d_occdifft = 0,level=0; tdatas[i].getTrainData(ref voldiff, ref spddiff, ref occdiff); u_spddifft = tdatas[i].vd1.AvgSpd - tdatas[i - 1].vd1.AvgSpd; u_voldifft = tdatas[i].vd1.Volume - tdatas[i-1].vd1.Volume; u_occdifft = tdatas[i].vd1.Occupancy - tdatas[i - 1].vd1.Occupancy; d_spddifft = tdatas[i].vd2.AvgSpd - tdatas[i - 1].vd2.AvgSpd; d_voldifft = tdatas[i].vd2.Volume - tdatas[i - 1].vd2.Volume; d_occdifft = tdatas[i].vd2.Occupancy - tdatas[i - 1].vd2.Occupancy; level = tdatas[i-1].Level; output[i-1] = new double[1]; output[i-1][0] = level; input[i-1] = new double[9]; input[i-1][0] = spddiff; input[i-1][1] = voldiff; input[i-1][2] = occdiff; input[i-1][3] = u_spddifft; input[i-1][4] = u_voldifft; input[i-1][5] = u_occdifft; input[i-1][6] = d_spddifft; input[i-1][7] = d_voldifft; input[i-1][8] = d_occdifft; Console.WriteLine(spddiff + "," + voldiff + "," + occdiff + "," + u_spddifft + "," + u_voldifft + "," + u_occdifft + "," + d_spddifft + "," + d_voldifft + "," + d_occdifft+","+level); } ActivationNetwork network = new ActivationNetwork( new BipolarSigmoidFunction(1.5),9, 25, 1 ); // create teacher BackPropagationLearning teacher = new BackPropagationLearning(network); teacher.Momentum = 0.1; teacher.LearningRate = 0.01; // loop double err=100; int cnt = 0; while (err / tdatas.Length >0.00079) { // run epoch of learning procedure err = teacher.RunEpoch( input, output ); Console.WriteLine(err / tdatas.Length); cnt++; } for (int i = 0; i < tdatas.Length-1; i++) { if (System.Convert.ToInt32(output[i][0]) != System.Convert.ToInt32(network.Compute(input[i])[0])) Console.WriteLine("fail"); // Console.WriteLine("chreck:"+Math.Abs((output[i][0] - network.Compute(input[i])[0]))>0,5?:"fail",""); } }
public void Train(List<Session> train, List<Session> cv, out List<double> trainErrors, out List<double> cvErrors, IActivationFunction function) { trainErrors = new List<double>(); cvErrors = new List<double>(); var count = train.Count; // prepare learning data Console.WriteLine("prepare learning data"); double[][] input = new double[count][]; double[][] output = new double[count][]; // preparing the data for (int i = 0; i < count; i++) { input[i] = CreateInput(train[i]); output[i] = CreateOutput(train[i]); } Console.WriteLine("feature scaling"); mean = new double[inputSize]; dev = new double[inputSize]; for (int i = 0; i < inputSize; i++) { var query = input.Select(p => p[i]); mean[i] = query.Average(); dev[i] = query.Deviation(mean[i]); } for (int i = 0; i < count; i++) for (int j = 0; j < inputSize; j++) { input[i][j] = (input[i][j] - mean[j]) / dev[j]; } Console.WriteLine("prepare cv data"); // prepare cv data double[][] cvIn = new double[cv.Count][]; double[][] cvOut = new double[cv.Count][]; // preparing the data for (int i = 0; i < cv.Count; i++) { cvIn[i] = CreateInput(cv[i]); cvOut[i] = CreateOutput(cv[i]); } Console.WriteLine("cv feature scaling"); for (int i = 0; i < cv.Count; i++) cvIn[i] = ScaleInput(cvIn[i]); Console.WriteLine("create network"); // create perceptron _network = new ActivationNetwork(function, inputSize, inputSize, classesCount); _network.Randomize(); // create teacher //PerceptronLearning teacher = new PerceptronLearning(_network); BackPropagationLearning teacher = new BackPropagationLearning(_network); // set learning rate teacher.LearningRate = 0.01; // loop int iter = 0; double error = 999; double delta = 999; Console.WriteLine("Train Network"); //while (iter < 1000) while (delta > 1 && iter < 5000) //while (iter < 2000) { // run epoch of learning procedure double trainError = teacher.RunEpoch(input, output); double trainError2 = ComputeCVError(_network, input, output); double cvError = ComputeCVError(_network, cvIn, cvOut); delta = Math.Abs(error - trainError); error = trainError; trainErrors.Add(trainError2); cvErrors.Add(cvError); iter++; if (iter % 100 == 0) Console.WriteLine(iter); } Console.WriteLine(iter); }
// Worker thread void SearchSolution( ) { // number of learning samples int samples = data.Length - predictionSize - windowSize; // data transformation factor double factor = 1.7 / chart.RangeY.Length; double yMin = chart.RangeY.Min; // prepare learning data double[][] input = new double[samples][]; double[][] output = new double[samples][]; for ( int i = 0; i < samples; i++ ) { input[i] = new double[windowSize]; output[i] = new double[1]; // set input for ( int j = 0; j < windowSize; j++ ) { input[i][j] = ( data[i + j] - yMin ) * factor - 0.85; } // set output output[i][0] = ( data[i + windowSize] - yMin ) * factor - 0.85; } // create multi-layer neural network ActivationNetwork network = new ActivationNetwork( new BipolarSigmoidFunction( sigmoidAlphaValue ), windowSize, windowSize * 2, 1 ); // create teacher BackPropagationLearning teacher = new BackPropagationLearning( network ); // set learning rate and momentum teacher.LearningRate = learningRate; teacher.Momentum = momentum; // iterations int iteration = 1; // solution array int solutionSize = data.Length - windowSize; double[,] solution = new double[solutionSize, 2]; double[] networkInput = new double[windowSize]; // calculate X values to be used with solution function for ( int j = 0; j < solutionSize; j++ ) { solution[j, 0] = j + windowSize; } // loop while ( !needToStop ) { // run epoch of learning procedure double error = teacher.RunEpoch( input, output ) / samples; // calculate solution and learning and prediction errors double learningError = 0.0; double predictionError = 0.0; // go through all the data for ( int i = 0, n = data.Length - windowSize; i < n; i++ ) { // put values from current window as network's input for ( int j = 0; j < windowSize; j++ ) { networkInput[j] = ( data[i + j] - yMin ) * factor - 0.85; } // evalue the function solution[i, 1] = ( network.Compute( networkInput)[0] + 0.85 ) / factor + yMin; // calculate prediction error if ( i >= n - predictionSize ) { predictionError += Math.Abs( solution[i, 1] - data[windowSize + i] ); } else { learningError += Math.Abs( solution[i, 1] - data[windowSize + i] ); } } // update solution on the chart chart.UpdateDataSeries( "solution", solution ); // set current iteration's info SetText( currentIterationBox, iteration.ToString( ) ); SetText( currentLearningErrorBox, learningError.ToString( "F3" ) ); SetText( currentPredictionErrorBox, predictionError.ToString( "F3" ) ); // increase current iteration iteration++; // check if we need to stop if ( ( iterations != 0 ) && ( iteration > iterations ) ) break; } // show new solution for ( int j = windowSize, k = 0, n = data.Length; j < n; j++, k++ ) { AddSubItem( dataList, j, solution[k, 1].ToString( ) ); } // enable settings controls EnableControls( true ); }
private void Button_Click_1(object sender, RoutedEventArgs e) { var learningTask = new Task(delegate { var learningData = GenerateInputOutput().Take(10); BackPropagationLearning trainer = new BackPropagationLearning(net); double prErr = 10000000; // Ошибка сети double error = 100; // Сначала скорость обучения должна быть высока trainer.LearningRate = 100; int iteration = 0; // Обучаем сеть пока ошибка сети станет небольшой while (error > 0.001) { iteration++; // Получаем ошибку сети var che = false; Dispatcher.Invoke(DispatcherPriority.Normal, new Action( delegate { if (ShowResultsCheckbox.IsChecked != null) che = ShowResultsCheckbox.IsChecked.Value; })); if (!che) { error = trainer.RunEpoch(learningData.Select(i => i.Item1).ToArray(), learningData.Select(i => i.Item2).ToArray()); Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate { label.Content = error + "\nIteration: " + iteration; })); } else { Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate { label.Content = "See?"; })); foreach (var data in learningData) { Dispatcher.Invoke(DispatcherPriority.Normal, new Action( delegate { if (ShowResultsCheckbox.IsChecked != null) che = ShowResultsCheckbox.IsChecked.Value; })); if (!che) break; // Если ошибка сети изменилась на небольшое значения, в сравнении ошибкой предыдущей эпохи Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate { _answerLable.Content = String.Join(", ", data.Item2.Select(d => d.ToString("N2"))); _learnedLable.Content = String.Join(", ", net.Compute(data.Item1).Select(d => d.ToString("N2"))); im.Source = BitmapSourceConvert.ToBitmapSource(data.Item3); im.UpdateLayout(); })); Thread.Sleep(1000); } } if (Math.Abs(error - prErr) < 0.000000001) { // Уменьшаем коэффициент скорости обучения на 2 trainer.LearningRate /= 2; if (trainer.LearningRate < 0.001) trainer.LearningRate = 0.001; } prErr = error; learningData = GenerateInputOutput().Take(10); } }); learningTask.Start(); }
private void initANN() { System.Windows.MessageBox.Show("inp: " + inputCount+" op: " + numberOfOutputs, "detection error", MessageBoxButton.OK, MessageBoxImage.Error); ActivationNetwork network = new ActivationNetwork((IActivationFunction)new SigmoidFunction(sigmoidAlphaValue), this.inputCount, globalVars.hiddenCount, numberOfOutputs); ActivationNetwork network1 = new ActivationNetwork((IActivationFunction)new BipolarSigmoidFunction(sigmoidAlphaValue), this.inputCount, globalVars.hiddenCount, numberOfOutputs); BackPropagationLearning teacher ; if (globalVars.typeOfLearning == 1) { teacher = new BackPropagationLearning(network1); } else if (globalVars.typeOfLearning == 2) { teacher = new BackPropagationLearning(network); } else { System.Windows.MessageBox.Show("else", "detection error", MessageBoxButton.OK, MessageBoxImage.Error); return; //teacher = new BackPropagationLearning(network); } // teacher = new BackPropagationLearning(network1); // set learning rate and momentum teacher.LearningRate = this.learningRate; teacher.Momentum = this.momentum; // iterations iteration = 1; // statistic files StreamWriter errorsFile = null; try { String desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // check if we need to save statistics to files // open files errorsFile = File.CreateText(desktopPath+@"\errors.csv"); ArrayList errorsList = new ArrayList(); // loop while (!needToStopLearning) { // run epoch of learning procedure double error = teacher.RunEpoch(input, output); // errorsList.Add(error); //took me a f*****g day for this. http://www.albahari.com/threading/part2.aspx Action action = () => { globalVars.error.Content = error; globalVars.annIter.Content = (iteration ) ;}; System.Windows.Application.Current.Dispatcher.Invoke(action); //l.l1.TextChanged += new System.EventHandler(l.textBox1_TextChanged); // learningWindow.l1.Refresh(); // learningWindow.l1.Invalidate(); // save current error if (errorsFile != null) { // errorsFile.WriteLine(error); } iteration++; // check if we need to stop if (error <= learningErrorLimit) break; } /* double[,] errors = new double[errorsList.Count, 2]; for (int i = 0, n = errorsList.Count; i < n; i++) { errors[i, 0] = i; errors[i, 1] = (double)errorsList[i]; } */ //errorChart.RangeX = new DoubleRange(0, errorsList.Count - 1); //errorChart.UpdateDataSeries("error", errors); } catch { Console.WriteLine("oopsie! bad filename"); } finally { // close files if (errorsFile != null) errorsFile.Close(); } Action action1 = () => { Learner.savedNet = network; globalVars.saveANNbutn.IsEnabled = true; }; System.Windows.Application.Current.Dispatcher.Invoke(action1); }
// Worker thread void SearchSolution() { // initialize input and output values double[][] input = null; double[][] output = null; if (sigmoidType == 0) { // unipolar data input = new double[4][] { new double[] {0, 0}, new double[] {0, 1}, new double[] {1, 0}, new double[] {1, 1} }; output = new double[4][] { new double[] {0}, new double[] {1}, new double[] {1}, new double[] {0} }; } else { // biipolar data input = new double[4][] { new double[] {-1, -1}, new double[] {-1, 1}, new double[] { 1, -1}, new double[] { 1, 1} }; output = new double[4][] { new double[] {-1}, new double[] { 1}, new double[] { 1}, new double[] {-1} }; } // create perceptron ActivationNetwork network = new ActivationNetwork( (sigmoidType == 0) ? (IActivationFunction)new SigmoidFunction(sigmoidAlphaValue) : (IActivationFunction)new BipolarSigmoidFunction(sigmoidAlphaValue), 2, 2, 1); // create teacher BackPropagationLearning teacher = new BackPropagationLearning(network); // set learning rate and momentum teacher.LearningRate = learningRate; teacher.Momentum = momentum; // iterations int iteration = 1; // statistic files StreamWriter errorsFile = null; try { // check if we need to save statistics to files if (saveStatisticsToFiles) { // open files errorsFile = File.CreateText("errors.csv"); } // erros list List<double> errorsList = new List<double>(); // loop while (!needToStop) { // run epoch of learning procedure double error = teacher.RunEpoch(input, output); errorsList.Add(error); // save current error if (errorsFile != null) { errorsFile.WriteLine(error); } // show current iteration & error UpdateTextbox(currentIterationBox, iteration.ToString()); //currentIterationBox.Text = iteration.ToString(); UpdateTextbox(currentErrorBox, error.ToString()); //currentErrorBox.Text = error.ToString(); iteration++; // check if we need to stop if (error <= learningErrorLimit) break; } // show error's dynamics double[,] errors = new double[errorsList.Count, 2]; for (int i = 0, n = errorsList.Count; i < n; i++) { errors[i, 0] = i; errors[i, 1] = errorsList[i]; } errorChart.RangeX = new DoubleRange(0, errorsList.Count - 1); errorChart.UpdateDataSeries("error", errors); } catch (IOException) { MessageBox.Show("Failed writing file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { // close files if (errorsFile != null) errorsFile.Close(); } // enable settings controls EnableControls(true); }
private void SearchSolution() { MemoryStream ms = new MemoryStream(); mNetwork = new ActivationNetwork( new BipolarSigmoidFunction(sigmoidAlphaValue), mInput[0].Length, mHiddenNeurons, mOutput[0].Length); // create teacher BackPropagationLearning teacher = new BackPropagationLearning(mNetwork); // set learning rate and momentum teacher.LearningRate = mLearningRate; teacher.Momentum = mMomentum; bool needToStop = false; int iteration = 0; while (!needToStop) { double error = teacher.RunEpoch(mInput, mOutput) / mInput.Length; mErrors[iteration] = error; double test_error = 0.0; for (int i = 0; i < mTestInput.Length; i++) { double[] test_result = mNetwork.Compute(mTestInput[i]); test_error += ( mTestOutput[i][0] - test_result[0])*( mTestOutput[i][0] - test_result[0]); } mTestErrors[iteration] = Math.Sqrt(test_error); if (min_test_error > mTestErrors[iteration]) { min_test_error = mTestErrors[iteration]; // mTestBestNetwork = mNetwork; ms = new MemoryStream(); mNetwork.Save(this.Id + ".txt"); } iteration++; if (iteration >= mIterations) needToStop = true; } mTestBestNetwork = (ActivationNetwork)ActivationNetwork.Load(this.Id + ".txt"); }
// teach the network public void learn() { network = new ActivationNetwork(new SigmoidFunction(sigmoidAV), 4, 5, 4); teacher = new BackPropagationLearning(network); teacher.LearningRate = learningRate; teacher.Momentum = momentum; error = 0; Random randomG = new Random(); for (int i = 0; i < iteration; i++) { if (randomMomentum) { double random = (randomG.NextDouble() * 0.3) + 0.5; teacher.Momentum = random; } error = teacher.RunEpoch(input, output) / sampleCount; if(outputErrorComponent != null) { outputErrorComponent.Text = "Neural network error is : " + error; } } Console.WriteLine("error is " + error); }
private void Worker() { ExtendedIOHelpers.ShowAlert("Generate new samples before training? y/n ", () => GenerateSamples()); if (!LoadSamples()) { return; } Console.WriteLine("Loaded {0} samples", _inputList.Count); var network = new ActivationNetwork(new SigmoidFunction(1), InputCount, OutputCount); var teacher = new BackPropagationLearning(network) { LearningRate = 0.01 }; Console.WriteLine("Start training the network."); int iteration = 0; const int iterations = 5000; double error = 1.0; var st = new Stopwatch(); st.Start(); while (iteration < iterations && error > 0.00005) { error = teacher.RunEpoch(_inputList.ToArray(), _outputList.ToArray()) / _inputList.Count; iteration++; } var time = st.ElapsedMilliseconds / 1000.0; st.Stop(); Console.WriteLine("Network successfully trained! Error = {0:0.######}, Iteration = {1}", error, iteration); Console.WriteLine("Time = {0:0.000} s\n", time); // Normalize weights and convert to string format. var weights = network.Layers .Select(layer => layer.Neurons .Select(neuron => neuron.Weights .Select(x => string.Format("{0,6}", Convert.ToInt32(x * 1000.0))))); ExtendedIOHelpers.ShowAlert("Do you want to save network to file? y/n ", () => { SaveWeights(weights); network.Save(NetworkFile); }); Console.ReadLine(); }
void FeedForwardTeach() { int samples = trainingFFData.GetLength(0); double[][] input = new double[samples][]; double[][] output = new double[samples][]; WriteLine("Teaching:"); for (int i = 0; i < samples; i++) { input[i] = new double[inputValues]; output[i] = new double[outputValues]; // set input for (int q = 0; q < inputValues; ++q) { input[i][q] = trainingFFData[i, q]; } // set output for (int q = inputValues; q < inputValues + outputValues; ++q) { output[i][q-inputValues] = trainingFFData[i, q]; } Write("["); for (int q = 0; q < inputValues; ++q) { Write(String.Concat(" ", input[i][q].ToString())); } Write(" ] -> ["); for (int q = 0; q < outputValues; ++q) { Write(string.Concat(" ", output[i][q].ToString())); } WriteLine(" ];"); } //IActivationFunction int[] newHidden = new int[hiddenLayer.Length+1]; Array.Copy(hiddenLayer, 0, newHidden, 0, hiddenLayer.Length); newHidden[newHidden.Length-1] = outputValues; // newHidden[0] = inputValues; activationNetwork = new ActivationNetwork(new BipolarSigmoidFunction(), inputValues, newHidden); ISupervisedLearning teacher; if (bias) { teacher = new BackPropagationLearning(activationNetwork); BackPropagationLearning teacherbp = teacher as BackPropagationLearning; teacherbp.LearningRate = learningRate; teacherbp.Momentum = momentum; } else { teacher = new BackPropagationLearningWithoutBias(activationNetwork); BackPropagationLearningWithoutBias teacherbp = teacher as BackPropagationLearningWithoutBias; teacherbp.LearningRate = learningRate; teacherbp.Momentum = momentum; } SetIterationsCount(0); int iteration = 1; while (!needToStop) { double error = teacher.RunEpoch(input, output); SetIterationsCount(iteration); if (++iteration > numberOfCycles) break; } BlockInterface(false); teachingDone = true; needToStop = false; UpdateLabels(); }
// Worker thread void SearchSolution() { // number of learning samples int samples = data.GetLength(0); // data transformation factor double yFactor = 1.7 / chart.RangeY.Length; double yMin = chart.RangeY.Min; double xFactor = 2.0 / chart.RangeX.Length; double xMin = chart.RangeX.Min; // prepare learning data double[][] input = new double[samples][]; double[][] output = new double[samples][]; for (int i = 0; i < samples; i++) { input[i] = new double[1]; output[i] = new double[1]; // set input input[i][0] = (data[i, 0] - xMin) * xFactor - 1.0; // set output output[i][0] = (data[i, 1] - yMin) * yFactor - 0.85; } // create multi-layer neural network ActivationNetwork network = new ActivationNetwork( new BipolarSigmoidFunction(sigmoidAlphaValue), 1, neuronsInFirstLayer, 1); // create teacher BackPropagationLearning teacher = new BackPropagationLearning(network); // set learning rate and momentum teacher.LearningRate = learningRate; teacher.Momentum = momentum; // iterations int iteration = 1; // solution array double[,] solution = new double[50, 2]; double[] networkInput = new double[1]; // calculate X values to be used with solution function for (int j = 0; j < 50; j++) { solution[j, 0] = chart.RangeX.Min + (double)j * chart.RangeX.Length / 49; } // loop while (!needToStop) { // run epoch of learning procedure double error = teacher.RunEpoch(input, output) / samples; // calculate solution for (int j = 0; j < 50; j++) { networkInput[0] = (solution[j, 0] - xMin) * xFactor - 1.0; solution[j, 1] = (network.Compute(networkInput)[0] + 0.85) / yFactor + yMin; } chart.UpdateDataSeries("solution", solution); // calculate error double learningError = 0.0; for (int j = 0, k = data.GetLength(0); j < k; j++) { networkInput[0] = input[j][0]; learningError += Math.Abs(data[j, 1] - ((network.Compute(networkInput)[0] + 0.85) / yFactor + yMin)); } // set current iteration's info UpdateTextbox(currentIterationBox, iteration.ToString()); //currentIterationBox.Text = iteration.ToString(); UpdateTextbox(currentErrorBox, learningError.ToString("F3")); //currentErrorBox.Text = learningError.ToString("F3"); // increase current iteration iteration++; // check if we need to stop if ((iterations != 0) && (iteration > iterations)) break; } // enable settings controls EnableControls(true); }
private void PutNetworkAndTrainer(Type eventType) { int count = Enum.GetValues(eventType).Length; ActivationNetwork network = new ActivationNetwork(new SigmoidFunction(), count, new int[] { count, 2 }); NetworkMap.Add(eventType, network); BackPropagationLearning learning = new BackPropagationLearning(network); TrainerMap.Add(eventType, learning); }
/// <summary> /// Mencari solusi model neural network /// </summary> private void searchSolution() { // Normalize Data switch (this.selectedActivationFunction) { case ActivationFunctionEnumeration.SemiLinearFunction: this.activationFunction = new SemiLinearFunction(); this.normalizeData(0.1, 0.9); break; case ActivationFunctionEnumeration.SigmoidFunction: this.activationFunction = new SigmoidFunction(); this.normalizeData(0.1, 0.9); break; case ActivationFunctionEnumeration.BipolarSigmoidFunction: this.activationFunction = new BipolarSigmoidFunction(); this.normalizeData(-0.9, 0.9); break; case ActivationFunctionEnumeration.HyperbolicTangentFunction: this.activationFunction = new HyperbolicTangentFunction(); this.normalizeData(-0.9, 0.9); break; default: this.activationFunction = new BipolarSigmoidFunction(); this.normalizeData(-0.9, 0.9); break; } //create network this.network = new ActivationNetwork (this.activationFunction, this.inputLayerNeurons, this.hiddenLayerNeurons, this.outputLayerNeurons); this.network.Randomize(); this.learning = new BackPropagationLearning(this.network); this.learning.LearningRate = this.learningRate; this.learning.Momentum = this.momentum; //variable for looping //needToStop = false; double mse = 0.0, error = 0.0, mae=0.0; int iteration = 1; double msle = 0.0, mspe = 0.0, generalizationLoss = 0.0, pq = 0.0; double[] trainingErrors = new double[this.strip]; for (int i = 0; i < this.strip; i++) trainingErrors[i] = double.MaxValue / strip; double lastMSE = double.MaxValue; int n = this.data.Length - this.network.InputsCount; int validationSet = (int)Math.Round(this.validationSetRatio * n); int trainingSet = n - validationSet; double[][] networkTrainingInput = new double[trainingSet][]; double[][] networkTrainingOutput = new double[trainingSet][]; for (int i = 0; i < trainingSet; i++) { networkTrainingInput[i] = new double[this.network.InputsCount]; networkTrainingOutput[i] = new double[1]; } for (int i = 0; i < trainingSet; i++) { for (int j = 0; j < this.network.InputsCount; j++) { networkTrainingInput[i][j] = this.networkInput[i][j]; } networkTrainingOutput[i][0] = this.networkOutput[i][0]; } double[] solutionValidation = new double[validationSet]; double[] inputForValidation = new double[this.network.InputsCount]; double[] inputForValidationNetwork = new double[this.network.InputsCount]; this.bestValidationError = double.MaxValue; this.bestNetworkWeight = new double[this.network.LayersCount][][]; this.bestNetworkBias = new double[this.network.LayersCount][]; this.bestSolution = new double[n]; for (int i = 0; i < this.network.LayersCount; i++) { this.bestNetworkWeight[i] = new double[this.network[i].NeuronsCount][]; this.bestNetworkBias[i] = new double[this.network[i].NeuronsCount]; for (int j = 0; j < this.network[i].NeuronsCount; j++) { this.bestNetworkWeight[i][j] = new double[this.network[i][j].InputsCount]; } } //best network criterion double bestNetworkError = double.MaxValue, bestNetworkMSE = double.MaxValue, bestNetworkMAE = double.MaxValue; //training while (!needToStop) { double sse = 0.0; double sae = 0.0; double ssle = 0.0; double sspe = 0.0; if (this.useAdvanceEarlyStopping) error = this.learning.RunEpoch(networkTrainingInput, networkTrainingOutput); else error = this.learning.RunEpoch(this.networkInput, this.networkOutput); this.solutionData = new double[n]; this.solutionToShow = new double[n, 2]; if (this.useAdvanceEarlyStopping) { //validation //for (int j = 0; j < this.network.InputsCount; j++) //{ // inputForValidation[this.network.InputsCount - 1 - j] = this.data[this.data.Length - validationSet - 1 - j]; //} } else { seriesChart.UpdateDataSeries("Validation", null); } this.validationSolutionToShow = new double[validationSet, 2]; for (int i = 0; i < n; i++) { this.solutionData[i] = (this.network.Compute(this.networkInput[i])[0] - this.minNormalizedData) / this.factor + this.minData; this.solutionToShow[i, 0] = i + this.network.InputsCount; this.solutionToShow[i,1] = this.solutionData[i]; sse += Math.Pow(this.solutionData[i] - this.data[i + this.network.InputsCount], 2); sae += Math.Abs(this.solutionData[i] - this.data[i + this.network.InputsCount]); //calculate advance early stopping if (this.useAdvanceEarlyStopping) { if (i < n - validationSet) { ssle += Math.Pow(this.solutionData[i] - this.data[i + this.network.InputsCount], 2); } else { if (i == n - validationSet) { for (int j = 0; j < this.network.InputsCount; j++) { inputForValidation[this.network.InputsCount - 1 - j] = this.data[this.data.Length - (n - i) - 1 - j]; } } for (int j = 0; j < this.network.InputsCount; j++) { inputForValidationNetwork[j] = (inputForValidation[j] - this.minData) * this.factor + this.minNormalizedData; } solutionValidation[i - n + validationSet] = (this.network.Compute(inputForValidationNetwork)[0] - this.minNormalizedData) / this.factor + this.minData; this.validationSolutionToShow[i - n + validationSet, 0] = i + this.network.InputsCount; this.validationSolutionToShow[i - n + validationSet, 1] = solutionValidation[i - n + validationSet]; sspe += Math.Pow(this.data[i + this.network.InputsCount] - solutionValidation[i - n + validationSet], 2); for (int j = 0; j < this.network.InputsCount - 1; j++) { inputForValidation[j] = inputForValidation[j + 1]; } inputForValidation[this.network.InputsCount - 1] = solutionValidation[i - n + validationSet]; } } } mse = sse / this.solutionData.Length; mae = sae / this.solutionData.Length; //Display it this.iterationBox.Text = iteration.ToString(); this.maeBox.Text = mae.ToString("F5"); this.mseBox.Text = mse.ToString("F5"); this.errorBox.Text = error.ToString("F5"); if (this.previewGrapicRadio.Checked) { seriesChart.UpdateDataSeries("Predicted", this.solutionToShow); } else if (this.previewTextRadio.Checked) { if (!this.useAdvanceEarlyStopping) { txtProgress.AppendText("Iteration: " + iteration.ToString() + "\tError: " + error.ToString("F5") + "\tMAE: " + mae.ToString("F5") + "\tMSE: " + mse.ToString("F5") + "\n"); txtProgress.ScrollToCaret(); } } if (this.useAdvanceEarlyStopping) { //calculate advance early stopping 2 mspe = sspe / validationSet; msle = ssle / (this.solutionData.Length - validationSet); //save best weight if (this.bestValidationError > mspe) { this.bestValidationError = mspe; this.bestSolution = this.solutionData; for (int i = 0; i < this.network.LayersCount; i++) for (int j = 0; j < this.network[i].NeuronsCount; j++) for (int k = 0; k < this.network[i][j].InputsCount; k++) this.bestNetworkWeight[i][j][k] = this.network[i][j][k]; for (int i = 0; i < this.network.LayersCount; i++) for (int j = 0; j < this.network[i].NeuronsCount; j++) this.bestNetworkBias[i][j] = this.network[i][j].Threshold; bestNetworkError = error; bestNetworkMAE = mae; bestNetworkMSE = mse; } //calculate generalization loss &pq generalizationLoss = 100 * (mspe / this.bestValidationError - 1); trainingErrors[(iteration - 1) % this.strip] = msle; double minStripTrainingError = double.MaxValue, sumStripTrainingError = 0.0; for (int i = 0; i < this.strip; i++) { sumStripTrainingError += trainingErrors[i]; if (trainingErrors[i] < minStripTrainingError) minStripTrainingError = trainingErrors[i]; } double trainingProgress = 1000 * ((sumStripTrainingError / (this.strip * minStripTrainingError)) - 1); pq = generalizationLoss / trainingProgress; //display advance early stopping this.learningErrorBox.Text = msle.ToString("F5"); this.validationErrorBox.Text = mspe.ToString("F5"); this.generalizationLossBox.Text = generalizationLoss.ToString("F5"); this.pqBox.Text = pq.ToString("F5"); seriesChart.UpdateDataSeries("Validation", this.validationSolutionToShow); if (this.previewTextRadio.Checked) { txtProgress.AppendText("Iteration: " + iteration.ToString() + "\tError: " + error.ToString("F5") + "\tMAE: " + mae.ToString("F5") + "\tMSE: " + mse.ToString("F5") + "\tLearning Error: " + msle.ToString("F5") + "\tValidation Error: " + mspe.ToString("F5") + "\tGeneralization Loss: " + generalizationLoss.ToString("F5") + "\tPQ: " + pq.ToString("F5") + "\n"); txtProgress.ScrollToCaret(); } //stopping switch (this.advanceStoppingMethod) { case AdvanceStoppingMethodEnumeration.GeneralizationLoss: if (generalizationLoss > this.generalizationLossTreshold) needToStop = true; break; case AdvanceStoppingMethodEnumeration.ProgressQuotient: if (pq > this.pqTreshold) needToStop = true; break; } } if (this.withCheckingCycle && iteration % this.checkingCycle == 0) { switch (this.checkingMethod) { case CheckingMethodEnumeration.byMSEValue: if (mse <= this.byMSEValueStopping) needToStop = true; break; case CheckingMethodEnumeration.byMSEChange: if (lastMSE - mse <= this.byMSEChangeStopping) needToStop = true; break; } lastMSE = mse; } if (iteration >= this.maxIteration) { needToStop = true; } iteration++; } //restore weight if (this.useAdvanceEarlyStopping) { this.solutionData = this.bestSolution; for (int i = 0; i < this.network.LayersCount; i++) for (int j = 0; j < this.network[i].NeuronsCount; j++) for (int k = 0; k < this.network[i][j].InputsCount; k++) this.network[i][j][k] = this.bestNetworkWeight[i][j][k]; for (int i = 0; i < this.network.LayersCount; i++) for (int j = 0; j < this.network[i].NeuronsCount; j++) this.network[i][j].Threshold = this.bestNetworkBias[i][j]; //best network criterion this.error = bestNetworkError; this.mse = bestNetworkMSE; this.mae = bestNetworkMAE; } else { this.error = error; this.mse = mse; this.mae = mae; } this.enableControls(true); }
void Proc2(Model model) { int learnIterations = 100; double desiredError = 0; var activationFunc1 = new ThresholdFunction(); var activationFunc2 = new SigmoidFunction(1); IActivationFunction activationFunc = activationFunc2; int inputsCount = model.InputsCount; int[] neuronsCount = new int[3]; neuronsCount[0] = model.InputsCount; neuronsCount[1] = model.InputsCount * 2; neuronsCount[2] = model.desiredOutput[0].Length; ActivationNetwork net = new ActivationNetwork(activationFunc, inputsCount, neuronsCount); BackPropagationLearning lerning = new BackPropagationLearning(net); ErrorCalculator errorCalculator = new ErrorCalculator(); Process(learnIterations, desiredError, net, lerning, model.input, model.desiredOutput, errorCalculator); Console.ReadLine(); }