public static void Main(string[] args) { Console.WriteLine("--------Neural Network--------"); Stopwatch sw = new Stopwatch(); Network imageNeuralNetwork; Console.Write("Loading Images...."); LoadImageData(out List <Image> trainImages, out List <Image> testImages); Console.WriteLine("Success!\n"); //PrintStatistics(testImages, "Testing Image Set"); //PrintStatistics(trainImages, "Training Image Set"); //PrintImage(1, 3737); Console.Write("Initializing Network..."); imageNeuralNetwork = new Network(784, 89, 10, 0.2, 0.9, 0.005); imageNeuralNetwork.Initialize(ActivationFunctions.Sigmoid, ActivationFunctions.Sigmoid); Console.WriteLine("Success!\n"); Console.WriteLine("*** Learning-Cycle ***"); sw.Start(); //imageNeuralNetwork.Learn(trainImages, testImages); imageNeuralNetwork.Learn(trainImages); sw.Stop(); Console.Write("Testing Network...."); int[,] confusionMatrix = imageNeuralNetwork.Test(testImages); Console.WriteLine("Success!"); ConfusionMatrixDrawer.Draw(confusionMatrix); Console.WriteLine("Total Learning Time taken: {0}\n", sw.Elapsed.ToString()); Console.ReadKey(); }
//optionally provide test-images to print a confusion matrix after each Cycle. Helps to find sweet spot between over and underfitting. public void Learn(List <Image> Images, List <Image> TestImages = null) { Stopwatch watch = new Stopwatch(); int nrOfRuns = 0; double networkError = 0.0; //Train Network until desired error rate is reached do { double networkErrorSum = 0.0; nrOfRuns++; watch.Start(); //For all Images in the training set for (int i = 0; i < Images.Count; i++) { //Set Image InputLayer.NeuronValues = Images[i].Data; for (int j = 0; j < NrOutputNeurons; j++) { if (j == (int)Images[i].Label) { OutputLayer.DesiredNeuronValues[j] = 1.0; } else { OutputLayer.DesiredNeuronValues[j] = 0.0; } } //Calculate values and adjust weights according to error. this.FeedForward(); this.BackPropagate(); networkErrorSum += CalculateError(); networkError = networkErrorSum / (i + 1); } watch.Stop(); //Print error rate and time elapsed for this epoch Console.Write("Error rate after epoch {0}: ", nrOfRuns, networkError); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("{0}", networkError); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("Time elapsed: {1}", nrOfRuns, watch.Elapsed.ToString()); Console.WriteLine(); if (TestImages != null) { ConfusionMatrixDrawer.Draw(Test(TestImages)); } } while (networkError > GoalErrorRate); }