Exemplo n.º 1
0
        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;
                    }
                }
            }
        }
Exemplo n.º 2
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;
                    }
                }
            }
        }