Пример #1
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();
        }
Пример #2
0
        //обучение с наилучгим форматом
        public Task LearningFormat(PatternsType patterns, KeysType keys, AnswersType answers)
        {
            int setCount = patterns.Count;

            System.Windows.Forms.RichTextBox console = FormConsole.console;
            console.AppendText("Learning Start\r\n");
            Information.form1.Refresh();
            if (IsBiasNeuron)
            {
                for (int i = 0; i < patterns.Count; i++)
                {
                    if (patterns[i].Count != this.inputLayer.SizeNeurons)
                    {
                        patterns[i].Add(1);
                    }
                }
            }
            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  = patterns[setIter];
                    string       label  = keys[setIter];
                    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 % 100 == 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");
                        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(Task.CompletedTask);
                }
                if (isStopLearning)
                {
                    console.AppendText("exit isStopLearning\r\n");
                    Information.form1.Refresh();
                    return(Task.CompletedTask);
                }
            }
            console.AppendText("exit MAX_EP\r\n");
            Information.form1.Refresh();
            return(Task.CompletedTask);
        }
Пример #3
0
        //обучение МОР, количество образцов равно количеству ответов для них
        //(плохо что ответы повторяются, лучше использовать метки)
        public void Learning(int setCount, List <List <float> > patterns, List <List <float> > answers)
        {
            System.Windows.Forms.RichTextBox console = FormConsole.console;
            if (!(setCount == patterns.Count && setCount == answers.Count))
            {
                console.AppendText("learning sizes error\r\n");
                return;
            }
            if (IsBiasNeuron)
            {
                for (int i = 0; i < patterns.Count; i++)
                {
                    patterns[i].Add(1);
                }
            }
            console.AppendText("Learning Start\r\n");
            Information.form1.Refresh();
            float epsEpErr = 0.0001f;

            float[] lastErrors = new float[3];
            for (int i = 0; i < lastErrors.Length; i++)
            {
                lastErrors[i] = i;
            }
            for (int ep = 0; ep < MAX_EP; ep++)
            {
                bool  isStopLearning = true;
                float sumEpErr       = 0;
                for (int setIter = 0; setIter < setCount; setIter++)
                {
                    List <float> input  = patterns[setIter];
                    List <float> output = Run(input, IsBiasNeuron);
                    float        err    = ErrorSet.MSE(answers[setIter], output);
                    sumEpErr += err;
                    if (err > 0.03)
                    {
                        isStopLearning = false;
                    }

                    if (ep % DIST_PRINT == 0)
                    {
                        console.AppendText("ep = " + ep + " setIter = " + setIter
                                           + " err = " + Math.Round(err, 4) + "\r\n");
                        console.SelectionStart = console.TextLength;
                        console.ScrollToCaret();
                        Information.form1.Refresh();
                    }
                    ChangeWeights(answers[setIter], output);
                }
                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();
        }