/// <summary> /// Learing weights /// </summary> /// <param name="document">Document - pdf document</param> /// <param name="result">LearnResult - learn result</param> private static void DefineWeightsSection(Document document, LearnResult result) { Section section = document.AddSection(); document.LastSection.AddParagraph("Wagi otrzymane w procesie uczenia", "Heading2"); Paragraph paragraph = section.AddParagraph("Wagi otrzymane w procesie uczenia dla: " + Path.GetFileNameWithoutExtension(result.Filename) + "\r\n"); paragraph.AddBookmark("Wagi otrzymane w procesie uczenia"); paragraph.Format.SpaceAfter = "3cm"; paragraph.Format.Font.Size = 16; paragraph.Format.Font.Color = Colors.Black; paragraph.Format.SpaceBefore = "1cm"; paragraph.Format.SpaceAfter = "1cm"; int i = 1; foreach (var item in result.ResultWeights) { paragraph = section.AddParagraph("\r\n" + "Wagi otrzymane z uczenia dla próby nr: " + i.ToString() + "\r\n"); paragraph.Format.SpaceAfter = "3cm"; paragraph.Format.Font.Size = 16; paragraph.Format.Font.Color = Colors.Black; paragraph.Format.SpaceBefore = "1cm"; paragraph.Format.SpaceAfter = "1cm"; Table table = new Table(); table.Borders.Width = 0.75; Column column = table.AddColumn(Unit.FromCentimeter(1.5)); column.Format.Alignment = ParagraphAlignment.Center; Column column2 = table.AddColumn(Unit.FromCentimeter(5.5)); column2.Format.Alignment = ParagraphAlignment.Center; int w = 1; Row _row = table.AddRow(); Cell _cell1 = _row.Cells[0]; _cell1.AddParagraph("Numer wagi"); Cell _cell2 = _row.Cells[1]; _cell2.AddParagraph("Waga"); foreach (var weight in item) { Row row = table.AddRow(); Cell cell1 = row.Cells[0]; cell1.AddParagraph(w.ToString()); Cell cell2 = row.Cells[1]; cell2.AddParagraph(weight.ToString()); w++; } paragraph.Section.Add(table); //document.LastSection.Add(table); i++; } }
/// <summary> /// Main page /// </summary> /// <param name="document">Document - pdf document</param> /// <param name="result">LearnResult - learning result</param> private void DefineInfo(Document document, LearnResult result) { Section section = document.AddSection(); document.LastSection.AddParagraph("Informacje na temat sieci neuronowej", "Heading2"); Paragraph paragraph = section.AddParagraph("Informacje na temat sieci neuronowej" + "\r\n"); paragraph.AddBookmark("Informacje na temat sieci neuronowej"); paragraph.Format.SpaceAfter = "3cm"; paragraph.Format.Font.Size = 16; paragraph.Format.Font.Color = Colors.Black; paragraph.Format.SpaceBefore = "1cm"; paragraph.Format.SpaceAfter = "1cm"; Table table = new Table(); table.Borders.Width = 0.75; Column column = table.AddColumn(Unit.FromCentimeter(10.0)); column.Format.Alignment = ParagraphAlignment.Left; Column column2 = table.AddColumn(Unit.FromCentimeter(5.5)); column2.Format.Alignment = ParagraphAlignment.Center; Row _row = table.AddRow(); Cell _cell1 = _row.Cells[0]; _cell1.AddParagraph("Nazwa parametru"); Cell _cell2 = _row.Cells[1]; _cell2.AddParagraph("Wartość parametru"); foreach (var item in result.GetInformation()) { Row row = table.AddRow(); Cell cell1 = row.Cells[0]; cell1.AddParagraph(item[0]); Cell cell2 = row.Cells[1]; cell2.AddParagraph(item[1]); } document.LastSection.Add(table); }
/// <summary> /// Load from xml string /// </summary> /// <param name="xmlText">String - xml</param> /// <returns>LearnResult</returns> public static LearnResult LoadFromXMLString(string xmlText) { try { LearnResult lr = Xml.Deserialize <LearnResult>(xmlText); if (Xml.Error != XmlSerializeError.NONE) { return(null); } else { return(lr); } } catch (Exception) { return(null); } }
/// <summary> /// Run NBN training and its test - this is the first version /// </summary> /// <param name="trials">int - number of learning trials</param> /// <returns>LearnResult</returns>R private LearnResult RunFirstVersion(int trials) { backupSettings = new NeuralNetworkSettings(); backupSettings.MaxError = settings.MaxError; backupSettings.MaxIterations = settings.MaxIterations; backupSettings.MU = settings.MU; backupSettings.MUH = settings.MUH; backupSettings.MUL = settings.MUL; backupSettings.Scale = settings.Scale; Trials = trials; LearnResult result = new LearnResult(); result.Filename = Filename; if (MatLabCompareDataFolder.Length > 0) { result.Filename = string.Format("{0}\\{1}.dat", MatLabCompareDataFolder, Path.GetFileNameWithoutExtension(Directory.GetFiles(MatLabCompareDataFolder, "*.dat")[0])); } if (!loadInputData(Filename)) { updateErrorNBN("Dane nie zostały wczytane."); return(result); } if (OnDebug != null) { debug("Data loaded from file: " + Filename); } var tmp = new System.Collections.Generic.List <int>(); tmp.Add(inputLearn.Cols); //setting number of hidden neurons for (int i = 0; i < Handle; i++) { tmp.Add(1); } tmp.Add(1); var vh = new VectorHorizontal(tmp.Count); for (int i = 0; i < vh.Length; i++) { vh[i] = tmp[i]; } switch (NBN_Topography) { case 0: { topo = Topography.Generate(TopographyType.BMLP, vh); } break; case 1: { topo = Topography.Generate(TopographyType.MLP, vh); } break; } result.Topo = topo.Data[0]; if (OnDebug != null) { debug(topo.ToString()); } if (topo == null) { updateErrorNBN("Topologia sieci nie została utworzona."); return(result); } info = this.checkInputs(ref inputLearn, ref outputLearn, ref topo, out indexes);//here are set indexes result.TopoIndex = indexes.Data[0]; result.Info = info; if (OnDebug != null) { debug(indexes.ToString()); debug(info.ToString()); } Activation act = new Activation(info.nn); act.FillWithNumber(NBN_Activation); act.setValue(info.nn - 1, 0); result.ActivationFunction = act.Data[0]; Gain gain = new Gain(info.nn); gain.FillWithNumber(NBN_Gain); result.GainValue = gain.Data[0]; result.Settings = this.settings; for (trial = 0; trial < trials; trial++) { Weights initialWeights = new Weights(info.nw); if (MatLabCompareDataFolder.Length > 0) { initialWeights = MatrixMB.Load(string.Format("{0}\\poczatkowe_wagi_proba_{1}.txt", MatLabCompareDataFolder, trial + 1)).ToWeights(); } else { initialWeights = Weights.Generate(info.nw); } if (IsResearchMode) { string initialWeightsFile = String.Format("{0}\\{1}{2}_initial_weights.dat", _reasearch_folder, trial, Path.GetFileNameWithoutExtension(result.Filename)); initialWeights.Store(initialWeightsFile); } initialWeights.Name = "Initial"; if (OnDebug != null) { debug(String.Format("\r\nTrial {0} from {1}\r\n", trial + 1, trials)); debug(initialWeights.ToString()); } settings = null; settings = NeuralNetworkSettings.Default(); settings.MaxError = backupSettings.MaxError; settings.MaxIterations = backupSettings.MaxIterations; settings.MU = backupSettings.MU; settings.MUH = backupSettings.MUH; settings.MUL = backupSettings.MUL; settings.Scale = backupSettings.Scale; I = MatrixMB.Eye(info.nw); tic();//learn time measure start var tr = Train(ref this.settings, ref this.info, ref this.inputLearn, ref this.outputLearn, ref this.topo, initialWeights, ref act, ref gain, ref indexes); String LearnExecutionTime = toc(); //learn time measure stop LearnTimeList = time.ElapsedTicks; //learn time measure save result.Add(tr.weights.Data[0], SSE.ToDoubleArray(), RMSE.ToDoubleArray()); result.LearnRMSE = (double)RMSE[RMSE.Count]; LearnRmseList = LastRMSE; if (OnDebug != null) { debug(tr.weights.ToString()); debug("\r\nLearn execution time: " + LearnExecutionTime + "(hours:minutes:seconds:miliseconds)\r\n"); debug("\r\nLearn SSE: " + tr.sse.ToString() + "\r\n"); debug("\r\nLearn RMSE: " + result.LearnRMSE.ToString() + "\r\n"); } updateError(result.LearnRMSE); NetworkInfo infoTest = info.Copy(); infoTest.np = inputTest.Rows; tic(); error.CalculateError(ref infoTest, ref inputTest, ref outputTest, ref topo, tr.weights, ref act, ref gain, ref indexes); var TestExecutionTime = toc(); TestTimeList = time.ElapsedTicks; result.TestRMSE = Math.Sqrt(error.Error / infoTest.np); TestRmseList = result.TestRMSE; result.TestingRmseList.Add(result.TestRMSE); if (OnDebug != null) { debug("\r\nTest execution time: " + TestExecutionTime + "(hours:minutes:seconds:miliseconds)\r\n"); debug("\r\nTest SSE: " + error.Error.ToString() + "\r\n"); debug("\r\nTest RMSE: " + result.TestRMSE.ToString() + "\r\n"); } //if (result.LearnRMSE < Threshold) IsTrainOK++; if (result.SSE[trial][result.SSE[trial].Length - 1] < Threshold) { IsTrainOK++; } } result.LearnRMSE = AverageLearnRMSE; result.TestRMSE = AverageTestRMSE; result.setStatisticsData(LearnRMSE, TestRMSE, LearnTime, TestTime, Trials); result.SuccessRate = (double)IsTrainOK / Trials; if (IsResearchMode)//save research { try { string filename = extractionFolder + "\\research result.pdf"; PDFGenerate data = new PDFGenerate(); data.Filename = filename; data.Result = result; data.ChartFilename = GeneratePlot(result.RMSE, Path.GetFileNameWithoutExtension(result.Filename)); HistoryPDF pdf = new HistoryPDF(data.Result, data.ChartFilename, true); pdf.Save(data.Filename); } catch { } } return(result); }
/// <summary> /// Load learning result into interface /// </summary> /// <param name="lr">LearnResult</param> private void loadIntoInterface(LearnResult lr) { try { status.Text = ""; int trial = 1; weightsList.Items.Clear(); chart.Series.Clear(); foreach (var weights in lr.ResultWeights) { String name = LearnByError.Internazional.Resource.Inst.Get("r53") + trial.ToString(); weightsList.Items.Add(name); for (int i = 0; i < weights.Length; i++) { weightsList.Items.Add(weights[i].ToString()); } weightsList.Items.Add(""); if (lr.Info.np == 0) throw new Exception(LearnByError.Internazional.Resource.Inst.Get("r54")); int jj = 1; foreach (var rmse in lr.RMSE) { for (int j = 0; j < rmse.Length; j++) { SetChart(LearnByError.Internazional.Resource.Inst.Get("r53") + jj.ToString(), j + 1, rmse[j]); } jj++; } trial++; } chart.Update(); status.Text = LearnByError.Internazional.Resource.Inst.Get("r55"); learnRMSE.Text = string.Format(LearnByError.Internazional.Resource.Inst.Get("r230"), result.AverageLearnRMSE); testRMSE.Text = string.Format(LearnByError.Internazional.Resource.Inst.Get("r231"), result.AverageTestRMSE); times.Text = string.Format(LearnByError.Internazional.Resource.Inst.Get("r232"), result.AverageLearnTime, result.AverageTestTime); neuronNumber.Text = string.Format(LearnByError.Internazional.Resource.Inst.Get("r233"), result.Filename, result.Info != null ? result.Info.nn : 0); } catch (Exception ex) { ex.ToLog(); status.Text = ex.Message; } finally { this.Text = String.Format(LearnByError.Internazional.Resource.Inst.Get("r229"), history.Items.Count); } }
/// <summary> /// Selection action - selecting learning result /// </summary> /// <param name="sender">who</param> /// <param name="e">arguments</param> private void list_SelectedIndexChanged(object sender, EventArgs e) { try { History h = new History(); h.Id = items[history.SelectedIndex]; h.Read(); result = marekbar.Xml.Deserialize<LearnResult>(h.Data); loadIntoInterface(result); } catch (Exception ex) { ex.ToLog(); } }
/// <summary> /// Run NBN training and its test - this is the first version /// </summary> /// <param name="trials">int - number of learning trials</param> /// <returns>LearnResult</returns>R private LearnResult RunFirstVersion(int trials) { backupSettings = new NeuralNetworkSettings(); backupSettings.MaxError = settings.MaxError; backupSettings.MaxIterations = settings.MaxIterations; backupSettings.MU = settings.MU; backupSettings.MUH = settings.MUH; backupSettings.MUL = settings.MUL; backupSettings.Scale = settings.Scale; Trials = trials; LearnResult result = new LearnResult(); result.Filename = Filename; if (MatLabCompareDataFolder.Length > 0) { result.Filename = string.Format("{0}\\{1}.dat", MatLabCompareDataFolder, Path.GetFileNameWithoutExtension(Directory.GetFiles(MatLabCompareDataFolder, "*.dat")[0])); } if (!loadInputData(Filename)) { updateErrorNBN("Dane nie zostały wczytane."); return result; } if (OnDebug != null) debug("Data loaded from file: " + Filename); var tmp = new System.Collections.Generic.List<int>(); tmp.Add(inputLearn.Cols); //setting number of hidden neurons for (int i = 0; i < Handle; i++) { tmp.Add(1); } tmp.Add(1); var vh = new VectorHorizontal(tmp.Count); for (int i = 0; i < vh.Length; i++) { vh[i] = tmp[i]; } switch (NBN_Topography) { case 0: { topo = Topography.Generate(TopographyType.BMLP, vh); } break; case 1: { topo = Topography.Generate(TopographyType.MLP, vh); } break; } result.Topo = topo.Data[0]; if (OnDebug != null) debug(topo.ToString()); if (topo == null) { updateErrorNBN("Topologia sieci nie została utworzona."); return result; } info = this.checkInputs(ref inputLearn, ref outputLearn, ref topo, out indexes);//here are set indexes result.TopoIndex = indexes.Data[0]; result.Info = info; if (OnDebug != null) { debug(indexes.ToString()); debug(info.ToString()); } Activation act = new Activation(info.nn); act.FillWithNumber(NBN_Activation); act.setValue(info.nn - 1, 0); result.ActivationFunction = act.Data[0]; Gain gain = new Gain(info.nn); gain.FillWithNumber(NBN_Gain); result.GainValue = gain.Data[0]; result.Settings = this.settings; for (trial = 0; trial < trials; trial++) { Weights initialWeights = new Weights(info.nw); if (MatLabCompareDataFolder.Length > 0) { initialWeights = MatrixMB.Load(string.Format("{0}\\poczatkowe_wagi_proba_{1}.txt", MatLabCompareDataFolder, trial + 1)).ToWeights(); } else { initialWeights = Weights.Generate(info.nw); } if (IsResearchMode) { string initialWeightsFile = String.Format("{0}\\{1}{2}_initial_weights.dat", _reasearch_folder, trial, Path.GetFileNameWithoutExtension(result.Filename)); initialWeights.Store(initialWeightsFile); } initialWeights.Name = "Initial"; if (OnDebug != null) { debug(String.Format("\r\nTrial {0} from {1}\r\n", trial + 1, trials)); debug(initialWeights.ToString()); } settings = null; settings = NeuralNetworkSettings.Default(); settings.MaxError = backupSettings.MaxError; settings.MaxIterations = backupSettings.MaxIterations; settings.MU = backupSettings.MU; settings.MUH = backupSettings.MUH; settings.MUL = backupSettings.MUL; settings.Scale = backupSettings.Scale; I = MatrixMB.Eye(info.nw); tic();//learn time measure start var tr = Train(ref this.settings, ref this.info, ref this.inputLearn, ref this.outputLearn, ref this.topo, initialWeights, ref act, ref gain, ref indexes); String LearnExecutionTime = toc();//learn time measure stop LearnTimeList = time.ElapsedTicks;//learn time measure save result.Add(tr.weights.Data[0], SSE.ToDoubleArray(), RMSE.ToDoubleArray()); result.LearnRMSE = (double)RMSE[RMSE.Count]; LearnRmseList = LastRMSE; if (OnDebug != null) { debug(tr.weights.ToString()); debug("\r\nLearn execution time: " + LearnExecutionTime + "(hours:minutes:seconds:miliseconds)\r\n"); debug("\r\nLearn SSE: " + tr.sse.ToString() + "\r\n"); debug("\r\nLearn RMSE: " + result.LearnRMSE.ToString() + "\r\n"); } updateError(result.LearnRMSE); NetworkInfo infoTest = info.Copy(); infoTest.np = inputTest.Rows; tic(); error.CalculateError(ref infoTest, ref inputTest, ref outputTest, ref topo, tr.weights, ref act, ref gain, ref indexes); var TestExecutionTime = toc(); TestTimeList = time.ElapsedTicks; result.TestRMSE = Math.Sqrt(error.Error / infoTest.np); TestRmseList = result.TestRMSE; result.TestingRmseList.Add(result.TestRMSE); if (OnDebug != null) { debug("\r\nTest execution time: " + TestExecutionTime + "(hours:minutes:seconds:miliseconds)\r\n"); debug("\r\nTest SSE: " + error.Error.ToString() + "\r\n"); debug("\r\nTest RMSE: " + result.TestRMSE.ToString() + "\r\n"); } //if (result.LearnRMSE < Threshold) IsTrainOK++; if (result.SSE[trial][result.SSE[trial].Length - 1] < Threshold) IsTrainOK++; } result.LearnRMSE = AverageLearnRMSE; result.TestRMSE = AverageTestRMSE; result.setStatisticsData(LearnRMSE, TestRMSE, LearnTime, TestTime, Trials); result.SuccessRate = (double)IsTrainOK / Trials; if (IsResearchMode)//save research { try { string filename = extractionFolder + "\\research result.pdf"; PDFGenerate data = new PDFGenerate(); data.Filename = filename; data.Result = result; data.ChartFilename = GeneratePlot(result.RMSE, Path.GetFileNameWithoutExtension(result.Filename)); HistoryPDF pdf = new HistoryPDF(data.Result, data.ChartFilename, true); pdf.Save(data.Filename); } catch { } } return result; }