Beispiel #1
0
        private void MainForm_Load(object sender, EventArgs e)
        {
            loading = true;
            Brain   = new Brain(28 * 28, 10, 3, 16, -0.5F, 0.5F, false, Neuron.ReLU);
            Brain   = new Brain(new FileStream(@"network.brainStream", FileMode.Open), Neuron.ReLU);//new Brain(File.ReadAllText(@"network.brain"),28 * 28, 10, 3, 16);
            Thread loadThread = new Thread(new ThreadStart(loadData));

            loadThread.Start();

            trainingActive = true;

            void loadData()
            {
                if (trainingActive)
                {
                    trainingAmount = 60000;
                    Images         = ParseDatabase.ParseImages(@"train-images.idx3-ubyte", trainingAmount);
                    Labels         = ParseDatabase.ParseLabels(@"train-labels.idx1-ubyte", trainingAmount);
                }
                else
                {
                    trainingAmount = 10000;
                    Images         = ParseDatabase.ParseImages(@"t10k-images.idx3-ubyte", trainingAmount);
                    Labels         = ParseDatabase.ParseLabels(@"t10k-labels.idx1-ubyte", trainingAmount);
                }

                loading = false;
            }
        }
 private void MainForm_Paint(object sender, PaintEventArgs e)
 {
     if (!loading)
     {
         e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
         Bitmap    bmp = ParseDatabase.ByteToBitmap(Images[count], ImageSize, ImageSize);
         Rectangle rec = new Rectangle(10, 10, 200, 200);
         e.Graphics.DrawImage(bmp, rec);
         bmp.Dispose();
     }
 }
        private void MainForm_Load(object sender, EventArgs e)
        {
            loading = true;
            Brain   = new Brain(28 * 28, 10, 2, 16);
            Thread loadThread = new Thread(new ThreadStart(loadData));

            loadThread.Start();

            void loadData()
            {
                Images = ParseDatabase.ParseImages(@"train-images-idx3-ubyte");
                Labels = ParseDatabase.ParseLabels(@"train-labels-idx1-ubyte");

                loading = false;
            }
        }
        private void train_button_Click(object sender, EventArgs e)
        {
            count++;
            if (trainThread == null)
            {
                trainThread = new Thread(new ThreadStart(training));
                trainThread.Start();
            }
            float[] output = Brain.Think(ParseDatabase.ByteToFloat(Images[count]));
            outputNum = Training.OutputNumber(output);


            if (Labels[count] == outputNum)
            {
                good++;
            }
            float accuracy = good / (count + 1);

            Console.Write(" accuracy: " + accuracy + "\r");
            outputNum_label.Text = outputNum + " accuracy: " + accuracy;
            show_output(output);

            this.Refresh();
        }
        private void training()
        {
            float tweakAmount = 0.0001F;

            count = 0;
            bool Active      = true;
            bool adjustTweak = true;

            while (Active)
            {
                float[] output = Brain.Think(ParseDatabase.ByteToFloat(Images[count]));
                outputNum = Training.OutputNumber(output);
                float cost = Training.CalculateCost(output, Labels[count]);
                Training.Backpropagate(Brain, tweakAmount, outputNum, Labels[count]);
                int expected = Labels[count];

                // Normalerweise trennt man die Prüfdaten von den Trainingsdaten,
                // um zu gucken ob das Netz nicht nur auswendig lernt, sondern auch generalisiert
                if (count % 1000 == 0)
                {
                    good = 0;                    //reset statistic every 1000 steps
                }
                if (expected == outputNum)
                {
                    good++;
                }
                double accuracy = good / (count % 1000 + 1.0);
                int    percent  = (int)(100 * accuracy);

                if (count % 100 == 90)
                {
                    Console.Write("accuracy: " + percent + "% cost: " + cost + " \r");
                    if (percent > 50 && adjustTweak)
                    {
                        tweakAmount = tweakAmount / 10;
                        adjustTweak = false;// only once
                    }
                }
                //this.Invoke(new MethodInvoker(delegate {
                //    this.Refresh();
                //    outputNum_label.Text =outputNum + " accuracy: " + percent+ "%";
                //    //show_output(output);
                //}));

                count++;

                if (count >= 59999)
                {
                    count = 0;
                    string path = @"save.brain";
                    File.Create(path).Close();
                    File.WriteAllText(path, Brain.BrainStructure);

                    DialogResult res = MessageBox.Show("Finished", "Training", MessageBoxButtons.OKCancel);
                    if (res.HasFlag(DialogResult.Cancel))
                    {
                        Active = false;
                    }
                }
            }
        }
Beispiel #6
0
        private void training()
        {
            float tweakAmount = 0.0005F;

            count = 0;
            bool Active = true;

            while (Active)
            {
                float[] output = Brain.Think(ParseDatabase.ByteToFloat(Images[count]), Neuron.ReLU);
                outputNum = Training.OutputNumber(output);
                float cost = Training.CalculateCost(output, Labels[count]);
                Training.Backpropagate(Brain, tweakAmount, outputNum, Labels[count]);
                int expected = Labels[count];

                // Normalerweise trennt man die Prüfdaten von den Trainingsdaten,
                // um zu gucken ob das Netz nicht nur auswendig lernt, sondern auch generalisiert
                if (count % 1000 == 0)
                {
                    good = 0;                    //reset statistic every 1000 steps
                }
                if (expected == outputNum)
                {
                    totalSuccess++;
                    good++;
                }

                double accuracy = good / (count % 1000 + 1.0);
                int    percent  = (int)(100 * accuracy);

                int totalAccuracy = Convert.ToInt32(totalSuccess * 100 / (count + 1));

                Console.Write("accuracy: " + percent + "% cost: " + cost + "total:" + totalAccuracy + "% \r");

                if (display)
                {
                    this.Invoke(new MethodInvoker(delegate {
                        this.Refresh();
                        outputNum_label.Text = outputNum + " accuracy: " + percent + "%";
                        show_output(output);
                    }));
                }

                count++;

                if (count >= trainingAmount - 1)
                {
                    count        = 0;
                    totalSuccess = 0;
                    string path = @"save.brain";
                    File.Create(path).Close();
                    File.WriteAllText(path, Brain.BrainStructureString);

                    DialogResult res = MessageBox.Show("Finished", "Training", MessageBoxButtons.OKCancel);
                    if (res.HasFlag(DialogResult.Cancel))
                    {
                        Active = false;
                    }
                }
            }
        }