private static void MNISTTrainNetwork(FeedforwardNeuralNetwork fnn, TrainingExample[] trainingExamples, TrainingExample[] testExamples)
        {
            Console.WriteLine("\nBeginning neural network training procedure.\n");
            Console.WriteLine("Enter desired number of epochs.");
            string epochsStr = Console.ReadLine();
            int    epochs    = Convert.ToInt32(epochsStr);

            do
            {
                Console.WriteLine("Please wait for training cycle to complete...");
                var timer = Stopwatch.StartNew();
                fnn.TrainEpochs(trainingExamples, epochs);
                Console.WriteLine("Training cycle completed in " + timer.Elapsed + ".");
                Console.WriteLine("Test network? Enter Y for yes or anything else for no.");
                string inputStr = Console.ReadLine();
                char   input    = Convert.ToChar(inputStr);
                if (input == 'Y')
                {
                    MNISTTestNetwork(fnn, testExamples);
                }
                Console.WriteLine("Continue training? Enter 0 for no or a valid number of epochs for yes.");
                epochsStr = Console.ReadLine();
                epochs    = Convert.ToInt32(epochsStr);
            } while (epochs > 0);
        }
        public static void DecimalBinaryTestNetwork(FeedforwardNeuralNetwork fnn)
        {
            Matrix inputs = new Matrix(2, 1);

            Matrix outputs = fnn.Evaluate(inputs);   //test for input = 0
            if (outputs[1, 1] > 0.9 && outputs[2, 1] < 0.1 && outputs[3, 1] < 0.1 && outputs[4, 1] < 0.1) {
                Console.WriteLine("0 success.");
            } else {
                Console.WriteLine("0 fail." + outputs[1, 1] + " " + outputs[2, 1]);
            }
            inputs[1, 1] = 0;
            inputs[2, 1] = 1;
            outputs = fnn.Evaluate(inputs);  //test for input = 1
            if (outputs[1, 1] < 0.1 && outputs[2, 1] > 0.9 && outputs[3, 1] < 0.1 && outputs[4, 1] < 0.1) {
                Console.WriteLine("1 success.");
            } else {
                Console.WriteLine("1 fail." + outputs[1, 1] + " " + outputs[2, 1]);
            }
            inputs[1, 1] = 1;
            inputs[2, 1] = 0;
            outputs = fnn.Evaluate(inputs);  //test for input = 2
            if (outputs[1, 1] < 0.1 && outputs[2, 1] < 0.1 && outputs[3, 1] > 0.9 && outputs[4, 1] < 0.1) {
                Console.WriteLine("2 success.");
            } else {
                Console.WriteLine("2 fail." + outputs[1, 1] + " " + outputs[2, 1]);
            }
            inputs[1, 1] = 1;
            inputs[2, 1] = 1;
            outputs = fnn.Evaluate(inputs);  //test for input = 3
            if (outputs[1, 1] < 0.1 && outputs[2, 1] < 0.1 && outputs[3, 1] < 0.1 && outputs[4, 1] > 0.9) {
                Console.WriteLine("3 success.");
            } else {
                Console.WriteLine("3 fail." + outputs[1, 1] + " " + outputs[2, 1]);
            }
        }
        public static void DecimalBinaryExample()
        {
            int[] layers = new int[] { 2, 5, 4 };
            FeedforwardNeuralNetwork fnn = new FeedforwardNeuralNetwork(layers, 1.0F, 0.1F);

            DecimalBinaryTestNetwork(fnn);
            Matrix[]          expectedOutputs = { new Matrix(new float[4, 1] {
                    { 1 },
                    { 0 },
                    { 0 },
                    { 0 }
                }),
                                                  new Matrix(new float[4,          1] {
                    { 0 },
                    { 1 },
                    { 0 },
                    { 0 }
                }),
                                                  new Matrix(new float[4,          1] {
                    { 0 },
                    { 0 },
                    { 1 },
                    { 0 }
                }),
                                                  new Matrix(new float[4,          1] {
                    { 0 },
                    { 0 },
                    { 0 },
                    { 1 }
                }) };
            Matrix[]          inputs = { new Matrix(new float[2, 1] {
                    { 0 },
                    { 0 }
                }),
                                         new Matrix(new float[2,          1] {
                    { 0 },
                    { 1 }
                }),
                                         new Matrix(new float[2,          1] {
                    { 1 },
                    { 0 }
                }),
                                         new Matrix(new float[2,          1] {
                    { 1 },
                    { 1 }
                }) };
            TrainingExample[] examples = new TrainingExample[4];
            for (int i = 0; i < 4; ++i)
            {
                examples[i] = new TrainingExample(inputs[i], expectedOutputs[i]);
            }
            fnn.TrainEpochs(examples, 1000);
            DecimalBinaryTestNetwork(fnn);
        }
        public static void DecimalBinaryTestNetwork(FeedforwardNeuralNetwork fnn)
        {
            Matrix inputs = new Matrix(2, 1);

            Matrix outputs = fnn.Evaluate(inputs);   //test for input = 0

            if (outputs[1, 1] > 0.9 && outputs[2, 1] < 0.1 && outputs[3, 1] < 0.1 && outputs[4, 1] < 0.1)
            {
                Console.WriteLine("0 success.");
            }
            else
            {
                Console.WriteLine("0 fail." + outputs[1, 1] + " " + outputs[2, 1]);
            }
            inputs[1, 1] = 0;
            inputs[2, 1] = 1;
            outputs      = fnn.Evaluate(inputs); //test for input = 1
            if (outputs[1, 1] < 0.1 && outputs[2, 1] > 0.9 && outputs[3, 1] < 0.1 && outputs[4, 1] < 0.1)
            {
                Console.WriteLine("1 success.");
            }
            else
            {
                Console.WriteLine("1 fail." + outputs[1, 1] + " " + outputs[2, 1]);
            }
            inputs[1, 1] = 1;
            inputs[2, 1] = 0;
            outputs      = fnn.Evaluate(inputs); //test for input = 2
            if (outputs[1, 1] < 0.1 && outputs[2, 1] < 0.1 && outputs[3, 1] > 0.9 && outputs[4, 1] < 0.1)
            {
                Console.WriteLine("2 success.");
            }
            else
            {
                Console.WriteLine("2 fail." + outputs[1, 1] + " " + outputs[2, 1]);
            }
            inputs[1, 1] = 1;
            inputs[2, 1] = 1;
            outputs      = fnn.Evaluate(inputs); //test for input = 3
            if (outputs[1, 1] < 0.1 && outputs[2, 1] < 0.1 && outputs[3, 1] < 0.1 && outputs[4, 1] > 0.9)
            {
                Console.WriteLine("3 success.");
            }
            else
            {
                Console.WriteLine("3 fail." + outputs[1, 1] + " " + outputs[2, 1]);
            }
        }
        public static void MNISTTestNetwork(FeedforwardNeuralNetwork fnn, TrainingExample[] testExamples)
        {
            try {
                Console.WriteLine("\nBeginning neural network test procedure.\n");
                int numSuccesses = 0;
                int numImages    = testExamples.Length;
                int expectedNum  = 0;
                for (int r = 0; r < numImages; ++r)
                {
                    for (int i = 1; i <= 10; ++i)
                    {
                        if (testExamples[r].expectedOutput[i, 1] == 1)
                        {
                            expectedNum = i % 10;
                        }
                    }
                    Matrix actualOutput       = fnn.Evaluate(testExamples[r].input);
                    float  largestOutputValue = 0;
                    int    index = 0;
                    for (int j = 1; j <= 10; ++j)
                    {
                        if (actualOutput[j, 1] > largestOutputValue)
                        {
                            largestOutputValue = actualOutput[j, 1];
                            index = j;
                        }
                    }
                    index %= 10;
                    if (index == expectedNum)
                    {
                        ++numSuccesses;
                    }

                    //Console.WriteLine("Test value: " + expectedNum);
                    //Console.WriteLine("Network detected: " + (index));


                    if ((r + 1) % 1000 == 0)
                    {
                        Console.WriteLine((r + 1) / 100 + "00 test images processed.  Current success percentage: " + (float)numSuccesses / (r + 1) * 100 + "%");
                    }
                }
                Console.WriteLine("\nTest regimen completed.  Network was correct for " + (float)numSuccesses / numImages * 100 + "% of " + numImages + " images.\n");
            } catch (Exception ex) {
                Console.WriteLine(ex.ToString());
            }
        }
        public static void MNISTExample()
        {
            try {
                TrainingExample[]        testExamples     = GetTestExamples();
                TrainingExample[]        trainingExamples = GetTrainingExamples();
                int[]                    layers           = new int[] { 784, 400, 10 };
                FeedforwardNeuralNetwork fnn = new FeedforwardNeuralNetwork(layers, 0.5F, 0.1F);
                Console.WriteLine("Neural network constructed.");

                MNISTTestNetwork(fnn, testExamples);

                MNISTTrainNetwork(fnn, trainingExamples, testExamples);

                MNISTTestNetwork(fnn, testExamples);
            } catch (Exception ex) {
                Console.WriteLine(ex.ToString());
            }
        }
 public static void DecimalBinaryExample()
 {
     int[] layers = new int[] { 2, 5, 4 };
     FeedforwardNeuralNetwork fnn = new FeedforwardNeuralNetwork(layers, 1.0F, 0.1F);
     DecimalBinaryTestNetwork(fnn);
     Matrix[] expectedOutputs = { new Matrix(new float[4,1]{ {1},
                                                             {0},
                                                             {0},
                                                             {0}}),
                                  new Matrix(new float[4,1]{ {0},
                                                             {1},
                                                             {0},
                                                             {0}}),
                                  new Matrix(new float[4,1]{ {0},
                                                             {0},
                                                             {1},
                                                             {0}}),
                                  new Matrix(new float[4,1]{ {0},
                                                             {0},
                                                             {0},
                                                             {1}})};
     Matrix[] inputs =          { new Matrix(new float[2,1]{ {0},
                                                             {0}}),
                                  new Matrix(new float[2,1]{ {0},
                                                             {1}}),
                                  new Matrix(new float[2,1]{ {1},
                                                             {0}}),
                                  new Matrix(new float[2,1]{ {1},
                                                             {1}})};
     TrainingExample[] examples = new TrainingExample[4];
     for (int i = 0; i < 4; ++i) {
         examples[i] = new TrainingExample(inputs[i], expectedOutputs[i]);
     }
     fnn.TrainEpochs(examples, 1000);
     DecimalBinaryTestNetwork(fnn);
 }
        public static void LogicGatesExample()
        {
            //First layer is input layer
            //Initialize a 2-layer ANN
            //Two inputs, one output
            int[] layers = new int[] { 2, 3, 1 };
            FeedforwardNeuralNetwork fnn = new FeedforwardNeuralNetwork(layers, 1.0F, 0.1F);
            //Train this many cycles
            int numTrainingEpochs = 10000;
            TrainingExample ex1 = new TrainingExample(new Matrix(new float[,]{ { 0 },
                                                                               { 0 } }),
                                                      new Matrix(new float[,]{ { 0 } }));

            TrainingExample ex2 = new TrainingExample(new Matrix(new float[,]{ { 0 },
                                                                               { 1 } }),
                                                      new Matrix(new float[,]{ { 1 } }));

            TrainingExample ex3 = new TrainingExample(new Matrix(new float[,]{ { 1 },
                                                                               { 0 } }),
                                                      new Matrix(new float[,]{ { 1 } }));

            TrainingExample ex4 = new TrainingExample(new Matrix(new float[,]{ { 1 },
                                                                               { 1 } }),
                                                      new Matrix(new float[,]{ { 0 } }));

            TrainingExample[] trainingExamples = { ex1, ex2, ex3, ex4 };
            for (int i = 0; i < numTrainingEpochs; ++i) {
                //Sets input matrices and output matrices and trains the network accordingly for all combinations
                Random rand = new Random();
                trainingExamples = trainingExamples.OrderBy(x => rand.Next()).ToArray();
                for (int j = 0; j < 4; ++j) {
                    fnn.TrainIteration(trainingExamples[j].input, trainingExamples[j].expectedOutput, 1 - (1.0F * 0.1F / 4));
                }
            }
            //After training, evaluates the network with respect to all possible inputs
            //Prints their outputs to the console to see the result of training
            float[,] inputArray = new float[,] { { 0 },
                                                 { 0 } };
            Matrix input = new Matrix(inputArray);
            Matrix output = fnn.Evaluate(input);
            Console.WriteLine(output[1, 1]);
            inputArray = new float[,] { { 0 },
                                        { 1 } };
            input = new Matrix(inputArray);
            output = fnn.Evaluate(input);
            Console.WriteLine(output[1, 1]);
            inputArray = new float[,] { { 1 },
                                        { 0 } };
            input = new Matrix(inputArray);
            output = fnn.Evaluate(input);
            Console.WriteLine(output[1, 1]);
            inputArray = new float[,] { { 1 },
                                        { 1 } };
            input = new Matrix(inputArray);
            output = fnn.Evaluate(input);
            Console.WriteLine(output[1, 1]);
        }
 private static void MNISTTrainNetwork(FeedforwardNeuralNetwork fnn, TrainingExample[] trainingExamples, TrainingExample[] testExamples)
 {
     Console.WriteLine("\nBeginning neural network training procedure.\n");
     Console.WriteLine("Enter desired number of epochs.");
     string epochsStr = Console.ReadLine();
     int epochs = Convert.ToInt32(epochsStr);
     do {
         Console.WriteLine("Please wait for training cycle to complete...");
         var timer = Stopwatch.StartNew();
         fnn.TrainEpochs(trainingExamples, epochs);
         Console.WriteLine("Training cycle completed in " + timer.Elapsed + ".");
         Console.WriteLine("Test network? Enter Y for yes or anything else for no.");
         string inputStr = Console.ReadLine();
         char input = Convert.ToChar(inputStr);
         if (input == 'Y') {
             MNISTTestNetwork(fnn, testExamples);
         }
         Console.WriteLine("Continue training? Enter 0 for no or a valid number of epochs for yes.");
         epochsStr = Console.ReadLine();
         epochs = Convert.ToInt32(epochsStr);
     } while (epochs > 0);
 }
Esempio n. 10
0
 public static void SineExample()
 {
     int[] layers = { 1, 3, 1 };
     FeedforwardNeuralNetwork fnn = new FeedforwardNeuralNetwork(layers, 1.0F, 0.1F);
 }
Esempio n. 11
0
        public static void MNISTTestNetwork(FeedforwardNeuralNetwork fnn, TrainingExample[] testExamples)
        {
            try {
                Console.WriteLine("\nBeginning neural network test procedure.\n");
                int numSuccesses = 0;
                int numImages = testExamples.Length;
                int expectedNum = 0;
                for (int r = 0; r < numImages; ++r) {
                    for (int i = 1; i <= 10; ++i) {
                        if (testExamples[r].expectedOutput[i, 1] == 1) {
                            expectedNum = i % 10;
                        }
                    }
                    Matrix actualOutput = fnn.Evaluate(testExamples[r].input);
                    float largestOutputValue = 0;
                    int index = 0;
                    for (int j = 1; j <= 10; ++j) {
                        if (actualOutput[j, 1] > largestOutputValue) {
                            largestOutputValue = actualOutput[j, 1];
                            index = j;
                        }
                    }
                    index %= 10;
                    if (index == expectedNum) {
                        ++numSuccesses;
                    }

                    //Console.WriteLine("Test value: " + expectedNum);
                    //Console.WriteLine("Network detected: " + (index));

                    if ((r + 1) % 1000 == 0) {
                        Console.WriteLine((r + 1) / 100 + "00 test images processed.  Current success percentage: " + (float)numSuccesses/(r+1) * 100 + "%");
                    }
                }
                Console.WriteLine("\nTest regimen completed.  Network was correct for " + (float)numSuccesses / numImages * 100 + "% of " + numImages + " images.\n");

            } catch (Exception ex) {
                Console.WriteLine(ex.ToString());
            }
        }
Esempio n. 12
0
        public static void MNISTExample()
        {
            try {
                TrainingExample[] testExamples = GetTestExamples();
                TrainingExample[] trainingExamples = GetTrainingExamples();
                int[] layers = new int[] { 784, 400, 10 };
                FeedforwardNeuralNetwork fnn = new FeedforwardNeuralNetwork(layers, 0.5F, 0.1F);
                Console.WriteLine("Neural network constructed.");

                MNISTTestNetwork(fnn, testExamples);

                MNISTTrainNetwork(fnn, trainingExamples, testExamples);

                MNISTTestNetwork(fnn, testExamples);

            } catch (Exception ex) {
                Console.WriteLine(ex.ToString());
            }
        }
Esempio n. 13
0
 public static void SineExample()
 {
     int[] layers = { 1, 3, 1 };
     FeedforwardNeuralNetwork fnn = new FeedforwardNeuralNetwork(layers, 1.0F, 0.1F);
 }
Esempio n. 14
0
        public static void LogicGatesExample()
        {
            //First layer is input layer
            //Initialize a 2-layer ANN
            //Two inputs, one output
            int[] layers = new int[] { 2, 3, 1 };
            FeedforwardNeuralNetwork fnn = new FeedforwardNeuralNetwork(layers, 1.0F, 0.1F);
            //Train this many cycles
            int             numTrainingEpochs = 10000;
            TrainingExample ex1 = new TrainingExample(new Matrix(new float[, ] {
                { 0 },
                { 0 }
            }),
                                                      new Matrix(new float[, ] {
                { 0 }
            }));

            TrainingExample ex2 = new TrainingExample(new Matrix(new float[, ] {
                { 0 },
                { 1 }
            }),
                                                      new Matrix(new float[, ] {
                { 1 }
            }));

            TrainingExample ex3 = new TrainingExample(new Matrix(new float[, ] {
                { 1 },
                { 0 }
            }),
                                                      new Matrix(new float[, ] {
                { 1 }
            }));

            TrainingExample ex4 = new TrainingExample(new Matrix(new float[, ] {
                { 1 },
                { 1 }
            }),
                                                      new Matrix(new float[, ] {
                { 0 }
            }));

            TrainingExample[] trainingExamples = { ex1, ex2, ex3, ex4 };
            for (int i = 0; i < numTrainingEpochs; ++i)
            {
                //Sets input matrices and output matrices and trains the network accordingly for all combinations
                Random rand = new Random();
                trainingExamples = trainingExamples.OrderBy(x => rand.Next()).ToArray();
                for (int j = 0; j < 4; ++j)
                {
                    fnn.TrainIteration(trainingExamples[j].input, trainingExamples[j].expectedOutput, 1 - (1.0F * 0.1F / 4));
                }
            }
            //After training, evaluates the network with respect to all possible inputs
            //Prints their outputs to the console to see the result of training
            float[,] inputArray = new float[, ] {
                { 0 },
                { 0 }
            };
            Matrix input  = new Matrix(inputArray);
            Matrix output = fnn.Evaluate(input);

            Console.WriteLine(output[1, 1]);
            inputArray = new float[, ] {
                { 0 },
                { 1 }
            };
            input  = new Matrix(inputArray);
            output = fnn.Evaluate(input);
            Console.WriteLine(output[1, 1]);
            inputArray = new float[, ] {
                { 1 },
                { 0 }
            };
            input  = new Matrix(inputArray);
            output = fnn.Evaluate(input);
            Console.WriteLine(output[1, 1]);
            inputArray = new float[, ] {
                { 1 },
                { 1 }
            };
            input  = new Matrix(inputArray);
            output = fnn.Evaluate(input);
            Console.WriteLine(output[1, 1]);
        }