Beispiel #1
0
        static void Main(string[] args)
        {
            try
            {
                ReadMNIST        read     = new ReadMNIST("D:\\Source\\C++\\BackpropagationNeuronNetwork\\BackpropagationNeuronNetwork\\bin\\Debug");
                DataSet          ds       = read.LoadDataMNIST();
                ReadDatabasePlus readplus = new ReadDatabasePlus();
                DataSet          ds1      = readplus.Load("data.xml");
                ds.CoppyDataset(ds1);
                //Backpropagation bnn = new Backpropagation (784, 200, 14);

                //printds(ds);

                Backpropagation bnn = new Backpropagation("RecogCharacterPlus.xml");

                //test(bnn,ds);
                //testTextFile(bnn);

                double learnRate = 0.1; // learning rate - controls the maginitude of the increase in the change in weights.
                double momentum  = 0.1; // momentum - to discourage oscillation.
                Console.WriteLine("Setting learning rate = " + learnRate.ToString("F2") + " and momentum = " + momentum.ToString("F2"));

                int maxEpochs = 500;



                int    epoch = 0;
                double error = double.MaxValue;
                Console.WriteLine("\nBeginning training using back-propagation\n");

                double[] yValues;         // outputs
                while (epoch < maxEpochs) // train
                {
                    error = 0.0;
                    for (int i = 0; i < ds.DataTrain.Count; i++)
                    {
                        yValues = bnn.ComputeOutputs(ds.DataTrain[i].input);
                        error  += Backpropagation.Error(ds.DataTrain[i].output, yValues);

                        bnn.UpdateWeights(ds.DataTrain[i].output, learnRate, momentum);
                    }

                    bnn.Save("RecogCharacterPlus.xml");

                    Console.WriteLine("epoch = " + epoch + " error = " + error);

                    ++epoch;
                } // train loop

                bnn.Save("RecogCharacter.xml");

                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Fatal: " + ex.Message);
                Console.ReadLine();
            }
        }
Beispiel #2
0
 private static void testTextFile(Backpropagation bnn)
 {
     double[] input = new double[784];
     using (TextReader reader = File.OpenText("write.txt"))
     {
         for (int i = 0; i < 784; i++)
         {
             input[i] = double.Parse(reader.ReadLine());
         }
         double[] output = bnn.ComputeOutputs(input);
     }
 }
Beispiel #3
0
        private static void test(Backpropagation bnn, DataSet ds)
        {
            int count = 0;

            double[] yValues;
            for (int i = 0; i < 10000; i++)
            {
                yValues = bnn.ComputeOutputs(ds.DataTest[i].input);
                bool check  = false;
                int  select = 0;
                for (int j = 1; j < 10; j++)
                {
                    if (Math.Abs(1 - yValues[j]) < Math.Abs(1 - yValues[select]))
                    {
                        select = j;
                    }
                }
                if (ds.DataTest[i].output[select] != 1)
                {
                    int tResult = 0;
                    Console.WriteLine("test case " + i + " is no ");
                    for (int k = 0; k < 10; k++)
                    {
                        Console.WriteLine("   " + yValues[k]);
                        if (ds.DataTest[i].output[k] == 1)
                        {
                            tResult = k;
                        }
                    }
                    Console.WriteLine("       target result = " + tResult);
                }
                else
                {
                    count++;
                    Console.WriteLine("test case " + i + "is yes \n");
                    for (int k = 0; k < 28; k++)
                    {
                        for (int j = 0; j < 28; j++)
                        {
                            Console.Write(ds.DataTest[i].input[k * 28 + j] + " ");
                        }
                        Console.WriteLine();
                    }
                    Console.WriteLine();
                }
            }
            Console.WriteLine("count = " + count);
            double result = count / 10000;

            Console.WriteLine("rate success = " + result);
        }