public void Test() { double testDataAmount = 0; double correctEstimate = 0; foreach (Image image in MnistReader.ReadTestData()) { testDataAmount++; _inputLayer.SetNeurons(image.Data); SetNewDesiredValues(image.Label); FeedForward(); CalculateError(); _confusionMatrix[(int)image.Label, _outputLayer.GetIndexOfHighestNeuron()]++; for (int i = 0; i < _outputLayer.DesiredValues.Length; i++) { if (_outputLayer.DesiredValues[i] == 1.0) { if (i == _outputLayer.GetIndexOfHighestNeuron()) { correctEstimate++; } } } } double accuracy = (correctEstimate / testDataAmount) * 100; Console.WriteLine("Accuracy: " + accuracy + " %.\n"); PrintConfusionMatrix(); }
public void MNISTPropagateTest() { MnistReader.RootPath = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, @"..\..\..\MNIST")); var images = MnistReader.ReadTestData().ToList(); Assert.IsTrue(images.Count() > 0); var image = images.ElementAt(0); NNArray input = image.Values; NNArray output = Utils.OneHot(10, image.Label); var nbInput = input.Length; IActivation activation; activation = new Softmax(); var network = new Network( new NormalizedLayer(nbInput, 1), new DenseLayerNoBias(nbInput, 10, activation, new CrossEntropyOneHot())); // network.AddLayer(new DenseLayerNoBias(nbInput, 28, activation, new SquaredDistance())); network.Initialize(); DateTime start; DateTime end; int epoc = 0, maxEpoc = 10000; double error = double.MaxValue; start = DateTime.Now; while (++epoc < maxEpoc && error > 0.01) { error = network.Train(input, output, 0.01); } end = DateTime.Now; var duration = (end - start).TotalMilliseconds / 1000; Console.WriteLine($"Duration for activation {activation.Name}: {duration} \t epoc: {epoc}\terror: {error}"); Assert.AreEqual(image.Label, network.OutputLayer.Output.ArgMax()); Assert.IsTrue(epoc < maxEpoc); foreach (var img in images.Where(i => i.Label == image.Label)) { network.Evaluate(img.Values); Console.WriteLine($"{network.OutputLayer.Output.ArgMax()}"); } }
public static void ReadDataFromFile() { Console.WriteLine("Reading training data ... "); foreach (var data in MnistReader.ReadTrainingData()) { TrainData.Add(data); } Console.WriteLine("Reading test data ... "); foreach (var data in MnistReader.ReadTestData()) { TestData.Add(data); } }
private static void TestNetwork(FeedForwardNeuralNet net) { Console.WriteLine("Started Testing"); int testedImages = 0; int rightGuesses = 0; foreach (var image in MnistReader.ReadTestData()) { double[] input = Encode(image.Data); double[] output = net.Run(input); int result = output.ToList().IndexOf(output.Max()); if (result == image.Label) { rightGuesses++; } testedImages++; } Console.WriteLine($"{testedImages} has been tested. {rightGuesses} were correctly classified."); }