Пример #1
0
        private void RunLearningProcess(bool IsResearch = false)
        {
            try
            {
                AppSetting app = new AppSetting();

                if (inputDataFilename == "")
                {
                    OpenDataFile();
                }
                if (inputDataFilename == "") return;
                blockInterface();
                NBN nbn = new NBN(app.NeuronNumber, inputDataFilename);

                nbn.NBN_Gain = app.Gain;
                nbn.NBN_Activation = app.ActivationFunction;
                nbn.NBN_Topography = app.TopologyType;
                nbn.Threshold = app.Threshold;
                nbn.IsClassification = app.IsClassification;

                nbn.IsResearchMode = IsResearch;
                nbn.MatLabCompareDataFolder = MatLabCompareDataFolder;
                string tmpMLF = MatLabCompareDataFolder;
                MatLabCompareDataFolder = "";//thanks got it
                nbn.OnErrorChange += (s, error) =>
                {
                    SetText(error.Format());
                };

                if (app.ShowConsole)
                {
                    nbn.OnDebug += (s, msg) =>
                    {
                        SetDebug(msg);
                    };
                }
                if (app.DontDrawChart == false)
                {
                    nbn.OnChartUpdate += (s, title, x, y) =>
                    {

                        SetChart(title, x, y);
                    };
                }
                if (app.ShowConsole)
                {
                    console = new DebugConsole();
                    console.Show();
                    console.Hide();
                }
                chart.Series.Clear();
                nbn.settings = new NeuralNetworkSettings(app.MaxIterations, app.MU, app.MUL, app.MUH, app.Scale, app.MaxError);
                var result = nbn.Run(app.LearnTrials);
                if (tmpMLF.Length > 0)
                {
                    string file = tmpMLF + "\\wyniki_z_nbn_csharp.txt";
                    if (File.Exists(file)) File.Delete(file);
                    using (var w = new System.IO.StreamWriter(file, true))
                    {
                        w.WriteLine("Dane: " + Path.GetFileNameWithoutExtension(result.Filename));
                        w.WriteLine("Liczba neuronów: " + result.Info.nn.ToString());
                        w.WriteLine(string.Format("Średnie RMSE uczenia: {0}", result.AverageLearnRMSE));
                        w.WriteLine(string.Format("Średnie RMSE testowania: {0}", result.AverageTestRMSE));
                        w.WriteLine(string.Format("Średnie czas uczenia: {0}", result.AverageLearnTime));
                        w.WriteLine(string.Format("Średnie czas testowania: {0}", result.AverageTestTime));
                        w.WriteLine(string.Format("Odchylenie standardowe uczenia: {0}", double.IsNaN(result.StandardDeviation) ? 0 : result.StandardDeviation));
                        w.WriteLine(string.Format("Odchylenie standardowe testowania: {0}", double.IsNaN(result.StandardDeviationTest) ? 0 : result.StandardDeviationTest));
                        w.WriteLine(string.Format("Wskaźnik sukcesu: {0}", result.SuccessRate));
                    }
                }

                std.Text = string.Format("Odchylenie standardowe uczenia: {0} oraz testowania: {1}", double.IsNaN(result.StandardDeviation) ? 0 : result.StandardDeviation, double.IsNaN(result.StandardDeviationTest) ? 0 : result.StandardDeviationTest);
                String runResult = String.Format(Resource.Inst.Get("r29"),
                    result.AverageLearnRMSE, result.AverageTestRMSE, result.Settings.MaxError);

                runResult += " " + String.Format(Resource.Inst.Get("r167"),
                    (result.AverageLearnRMSE <= result.Settings.MaxError ? Resource.Inst.Get("r169") : Resource.Inst.Get("r170")));

                runResult += ", " + String.Format(Resource.Inst.Get("r168"),
                    (result.AverageTestRMSE <= result.Settings.MaxError ? Resource.Inst.Get("r169") : Resource.Inst.Get("r170")));

                try
                {
                    times.Text = String.Format(String.Format(Resource.Inst.Get("r172"), result.SuccessRate)) + ", " + String.Format(Resource.Inst.Get("r171"),
                            result.AverageLearnTime.TrimEnd(new char[] { '0', '.' }),
                            result.AverageTestTime.TrimEnd(new char[] { '0', '.' }));
                }
                catch (Exception ex)
                {
                    ex.ToLog();
                }
                status.Text = runResult;

                if (app.AutoSaveLearningResults)
                {
                    String file = Common.File.GetXmlFileNameForHistory();
                    DateTime d = DateTime.Now;
                    History h = new History();
                    h.Name = String.Format("{0}_{1}_{2}_{3}_{4}_{5}_{6}", Path.GetFileNameWithoutExtension(result.Filename),
                        d.Year, d.Month, d.Day, d.Hour, d.Minute, d.Second);
                    h.Data = marekbar.Xml.Serialize<LearnResult>(result);
                    h.Insert();
                }

                if (app.ShowConsole) { console.Show(); }

                toolSSE.Items.Clear();
                for (int i = 0; i < result.SSE.Count; i++)
                {
                    toolSSE.Items.Add(Resource.Inst.Get("r182") + (i + 1).ToString());
                    int position = 1;
                    foreach (var sse in result.SSE[i])
                    {
                        toolSSE.Items.Add(String.Format(Resource.Inst.Get("r183"), position, Math.Round(sse,4).ToString().Replace(",",".")));
                        position++;
                    }
                }

                toolRMSE.Items.Clear();
                for (int i = 0; i < result.RMSE.Count; i++)
                {
                    toolRMSE.Items.Add(Resource.Inst.Get("r184") + (i + 1).ToString());
                    int position = 1;
                    foreach (var rmse in result.RMSE[i])
                    {
                        toolRMSE.Items.Add(String.Format(Resource.Inst.Get("r185"), position, Math.Round(rmse, 4).ToString().Replace(",", ".")));
                        position++;
                    }
                }

            }
            catch (Exception ex)
            {
                info = ex.ToLog().Message;
            }
            finally
            {
                unblockInterface();
            }
        }
Пример #2
0
        private void testForData_Click(object sender, EventArgs e)
        {
            if (worker != null && worker.IsBusy)
            {
                info = Resource.Inst.Get("r189");
                return;
            }
            var file = Common.Dialog.Open(Resource.Inst.Get("r190"), Resource.Inst.Get("r191"), "dat");
            if (file.Length > 0 && File.Exists(file))
            {
                RepeatWindowChoice rwc = new RepeatWindowChoice();
                rwc.Text = Resource.Inst.Get("r192");
                rwc.FormClosing += (owner, args) =>
                {
                    var w = (RepeatWindowChoice)owner;
                    if (w.Repeat)
                    {
                        var repeats = w.NumberOfRepeats;
                        NeuronNumberWindow nnw = new NeuronNumberWindow();
                        nnw.FormClosing += (nnnwOwner, nnwArgs) => {
                            var nw = (NeuronNumberWindow)nnnwOwner;
                            if (nw.IsSelected)
                            {
                                var neurons = nw.NumberOfNeurons;

                                worker = new BackgroundWorker();
                                worker.WorkerReportsProgress = true;
                                worker.WorkerSupportsCancellation = true;
                                worker.DoWork += (a, b) =>
                                {
                                    var data = ((String)b.Argument).Split(";".ToCharArray());
                                    var inputFile = data[0];
                                    var numberRepeats = int.Parse(data[1]);
                                    var maxNeurons = int.Parse(data[2]);
                                    List<LearnResult> results = new List<LearnResult>();

                                    int counter = 0;
                                    for (int nn = 1; nn < maxNeurons; nn++)
                                    {
                                        for (int r = 0; r < numberRepeats; r++)
                                        {
                                            NBN nbn = new NBN(nn, file);
                                            results.Add(nbn.Run(1));
                                            ((BackgroundWorker)a).ReportProgress(++counter);
                                        }
                                    }

                                    List<ReportData> items = new List<ReportData>();

                                    for (int nn = 2; nn <= maxNeurons; nn++)
                                    {
                                        var list = results.Where(q => q.Info != null && q.Info.nn == nn).ToList();

                                        var avgLearnRMSE = list.Average(q => q.AverageLearnRMSE);
                                        var avgTestRMSE = list.Average(q => q.AverageTestRMSE);
                                        ReportData r = new ReportData();
                                        r.LearnData = file;
                                        r.LearnRMSE = avgLearnRMSE.ToString();
                                        r.NeuronNumber = nn.ToString();
                                        r.TestRMSE = avgTestRMSE.ToString();
                                        r.Trials = numberRepeats.ToString();
                                        r.LearnTime = list.GetLearnTime();
                                        r.TestTime = list.GetTestTime();
                                        items.Add(r);
                                    }
                                    b.Result = items;
                                };
                                worker.ProgressChanged += (a, b) =>
                                {
                                    info = b.ProgressPercentage.ToString() + " z " + maxTmp;
                                };
                                worker.RunWorkerCompleted += (a, b) =>
                                {
                                    info = Resource.Inst.Get("r193");
                                    var report = ((List<ReportData>)b.Result);
                                    GridWindow gw = new GridWindow();
                                    gw.SetData(Resource.Inst.Get("r194"), report);
                                    gw.ShowDialog();
                                };

                                var arguments = String.Format("{0};{1};{2}", file, repeats, neurons);
                                maxTmp = (repeats * neurons).ToString();
                                worker.RunWorkerAsync(arguments);

                            }
                            else
                            {
                                info = Resource.Inst.Get("r195");
                            }
                        };
                        nnw.ShowDialog();
                    }
                    else
                    {
                        info = Resource.Inst.Get("r195");
                    }
                };
                rwc.ShowDialog();
            }
            else
            {
                info = Resource.Inst.Get("r196");
            }
        }
Пример #3
0
        public void Run()
        {
            int counter = 0;

            Parallel.ForEach(Items, file =>
                 {
                     for (int nn = 1; nn < NeuronNumber; nn++)//neuron number
                     {
                         for (int i = 0; i < RepeatForEachFile; i++)//learning tests for file with x neurons in network
                         {
                             try
                             {
                                 NBN nbn = new NBN(nn, file);
                                 var result = nbn.Run(1);

                                 DateTime d = DateTime.Now;
                                 History h = new History();
                                 h.Name = String.Format("{0}_{1}_{2}_{3}_{4}_{5}_{6}_{7}", Path.GetFileNameWithoutExtension(result.Filename),
                                     d.Year, d.Month, d.Day, d.Hour, d.Minute, d.Second, d.Millisecond);
                                 h.Data = marekbar.Xml.Serialize<LearnResult>(result);
                                 h.Insert();

                                 if (PDF)
                                 {
                                     try
                                     {
                                         string filename = Common.Folder.Data + "\\" + h.Name + ".pdf";

                                         PDFGenerate data = new PDFGenerate();
                                         data.Filename = filename;
                                         data.Result = result;
                                         data.ChartFilename = GeneratePlot(result.RMSE[0], Path.GetFileNameWithoutExtension(result.Filename));
                                         HistoryPDF pdf = new HistoryPDF(data.Result, data.ChartFilename, true);
                                         pdf.Save(data.Filename);
                                     }
                                     catch
                                     { //don't care about it
                                     }
                                 }
                             }
                             catch (Exception ex)
                             {
                                 ex.GetError();
                             }
                             finally
                             {
                                 counter++;
                                 if (OnUpdate != null)
                                 {
                                     OnUpdate(counter);
                                 }
                             }
                         }
                     }
                 });
        }