Esempio n. 1
0
        //Тест количество верных ответов для mnist TestDigits
        public void MnistTest(ReadMnist readMnist, List <List <float> > answers)
        {
            FormConsole.PrintlnAndScroll("\r\nstart Mnist Test");
            int testCount = readMnist.TestDigits.Count;
            int goodCount = 0;

            for (int i = 0; i < testCount; i++)
            {
                List <float> input = readMnist.TestDigits[i].floatPixels;
                if (IsBiasNeuron)
                {
                    input.Add(1);
                }
                int          label  = readMnist.TestDigits[i].label;
                List <float> output = Run(input, IsBiasNeuron);
                if (IsCorrectAnswer(output, answers[label]))
                {
                    goodCount++;
                }
                if (i % 100 == 0)
                {
                    FormConsole.PrintlnAndScroll("i = " + i + " goodCount = " + goodCount);
                }
            }
            FormConsole.PrintlnAndScroll("goodCount = " + goodCount
                                         + "\r\nfinish Mnist Test");
        }
Esempio n. 2
0
        //обучение для мниста такое же как и раньше только у здесь есть метки
        public void LearningMnist(ReadMnist readMnist, List <List <float> > answers)
        {
            int setCount = readMnist.TrainDigits.Count;

            System.Windows.Forms.RichTextBox console = FormConsole.console;
            console.AppendText("Learning Start\r\n");
            Information.form1.Refresh();
            float epsEpErr = 0.0000001f;

            float[] lastErrors = new float[3];
            for (int i = 0; i < lastErrors.Length; i++)
            {
                lastErrors[i] = i;
            }
            for (int ep = 0; ep < MAX_EP; ep++)
            {
                DateTime startEpTime    = DateTime.Now;
                bool     isStopLearning = true;
                float    sumEpErr       = 0;
                for (int setIter = 0; setIter < setCount; setIter++)
                {
                    List <float> input = readMnist.TrainDigits[setIter].floatPixels;
                    if (IsBiasNeuron)
                    {
                        input.Add(1);
                    }

                    /*string inStr = "";
                     * foreach (var el in input)
                     *  inStr += el + " ";
                     * File.WriteAllLines("input"+setIter+"-"+ep, new string[] { inStr });*/
                    int          label  = readMnist.TrainDigits[setIter].label;
                    List <float> output = Run(input, IsBiasNeuron);
                    float        err    = ErrorSet.MSE(answers[label], output);
                    sumEpErr += err;
                    if (err > 0.03)
                    {
                        isStopLearning = false;
                    }
                    string outStr  = "";
                    string answStr = "";
                    foreach (var el in output)
                    {
                        outStr += Math.Round(el, 4) + " ";
                    }
                    foreach (var el in answers[label])
                    {
                        answStr += Math.Round(el, 4) + " ";
                    }
                    ChangeWeights(answers[label], output);
                    if (ep % DIST_PRINT == 0 && setIter % 1000 == 0)
                    {
                        console.AppendText("ep = " + ep + " setIter = " + setIter
                                           + " err = " + Math.Round(err, 4) + "\r\n");
                        console.AppendText("out: " + outStr + "\r\n");
                        console.AppendText("answ: " + answStr + "\r\n");
                        console.AppendText("label = " + label + "\r\n");
                        console.AppendText(readMnist.TrainDigits[setIter].ToString());
                        TimeSpan epTime = DateTime.Now - startEpTime;
                        console.AppendText("timeEp = " + epTime.ToString("h'h 'm'm 's's'") + "\r\n");
                        console.SelectionStart = console.TextLength;
                        console.ScrollToCaret();
                        //Information.form1.Refresh();
                    }
                }
                lastErrors[0] = lastErrors[1];
                lastErrors[1] = lastErrors[2];
                lastErrors[2] = sumEpErr / setCount;
                if (Math.Abs(lastErrors[0] - lastErrors[1]) < epsEpErr ||
                    Math.Abs(lastErrors[1] - lastErrors[2]) < epsEpErr)
                {
                    console.AppendText("exit epsEpErr\r\n");
                    console.AppendText(lastErrors[0] + "\r\n");
                    console.AppendText(lastErrors[1] + "\r\n");
                    console.AppendText(lastErrors[2] + "\r\n");
                    Information.form1.Refresh();
                    return;
                }
                if (isStopLearning)
                {
                    console.AppendText("exit isStopLearning\r\n");
                    Information.form1.Refresh();
                    return;
                }
            }
            console.AppendText("exit MAX_EP\r\n");
            Information.form1.Refresh();
        }