public static void HandleEpochEnded(BackpropAlgorithm alg, MultiRegressionSample <double[][, ]> test, MultiRegressionSample <double[][, ]> train, string outputPath) { Console.WriteLine("\r------------------------------------------- Epoch #{0} ({1}) ", alg.Epoch, DateTime.Now); Console.WriteLine("L:\t{0}", alg.LossValue); Console.WriteLine("DW:\t{0}", alg.Step2); Console.WriteLine("LR:\t{0}", alg.LearningRate); double?pct = null; if (test == null || !test.Any()) { Console.WriteLine("Test: none"); } else { var terrors = alg.GetErrors(test, 0, true); var tec = terrors.Count(); var tdc = test.Count; var tpct = Math.Round(100.0F * tec / tdc, 2); Console.WriteLine("Test: {0} of {1} ({2}%)", tec, tdc, tpct); pct = tpct; } if (train == null || !train.Any()) { Console.WriteLine("Train: none"); } else { var verrors = alg.GetErrors(train, 0, true); var vec = verrors.Count(); var vdc = train.Count; var vpct = Math.Round(100.0F * vec / vdc, 2); Console.WriteLine("Train: {0} of {1} ({2}%)", vec, vdc, vpct); if (!pct.HasValue) { pct = vpct; } } var ofileName = string.Format("cn_e{0}_p{1}.mld", alg.Epoch, Math.Round(pct.Value, 2)); var ofilePath = Path.Combine(outputPath, ofileName); using (var stream = File.Open(ofilePath, FileMode.Create)) { alg.Net.Serialize(stream); } }
public static void HandleEpochEnded(BackpropAlgorithm alg, ClassifiedSample <double[][, ]> test, string outputPath) { Console.WriteLine("---------------- Epoch #: {0} ({1})", alg.Epoch, DateTime.Now); Console.WriteLine("L:\t{0}", alg.LossValue); Console.WriteLine("DW:\t{0}", alg.Step2); Console.WriteLine("LR:\t{0}", alg.LearningRate); Console.WriteLine("Errors:"); var errors = alg.GetErrors(test); var ec = errors.Count(); var dc = test.Count; var pct = Math.Round(100.0F * ec / dc, 2); Console.WriteLine("{0} of {1} ({2}%)", ec, dc, pct); var ofileName = string.Format("cn_e{0}_p{1}.mld", alg.Epoch, Math.Round(pct, 2)); var ofilePath = Path.Combine(outputPath, ofileName); using (var stream = File.Open(ofilePath, FileMode.Create)) { alg.Net.Serialize(stream); } }
private void onTestButtonClick(object sender, RoutedEventArgs e) { var path = @"F:\Work\science\Machine learning\data\cat-dog\train\kaggle"; var errors1 = 0; var errors1C = 0; var errors1D = 0; var errors2 = 0; var errors2C = 0; var errors2D = 0; var errorsC1 = 0; var errorsC2 = 0; var errorsR = 0; var pct1 = 0; var pct1C = 0; var pct1D = 0; var pct2 = 0; var pct2C = 0; var pct2D = 0; var pctC = 0; var pctC1 = 0; var pctC2 = 0; var pctR = 0; var alp1 = 0.95D; var alp2 = 0.05D; var dir = new DirectoryInfo(path); var total = dir.GetFiles().Length; var sample = new MultiRegressionSample <double[][, ]>(); var cat = new double[] { 1.0D, 0.0D }; var dog = new double[] { 0.0D, 1.0D }; int cnt = 0; foreach (var file in dir.EnumerateFiles().Shuffle(0).Skip(10000).Take(500)) { var fname = Path.GetFileNameWithoutExtension(file.Name); var expected = fname.StartsWith("cat.") ? 0 : 1; var data1 = getNetData(file.FullName); double[][,] data2; using (var image = (Bitmap)System.Drawing.Image.FromFile(file.FullName)) data2 = getNetFData(image); sample.Add(data2, expected == 0 ? cat : dog); var result1 = m_Network.Calculate(data1).Select(d => d[0, 0]).ToArray(); var actual1 = ML.Core.Mathematics.MathUtils.ArgMax(result1); if (expected != actual1) { if (expected == 0) { errors1C++; } else { errors1D++; } errors1++; } var result2 = m_NetworkF.Calculate(data2).Select(d => d[0, 0]).ToArray(); var actual2 = ML.Core.Mathematics.MathUtils.ArgMax(result2); if (expected != actual2) { if (expected == 0) { errors2C++; } else { errors2D++; } errors2++; } var resultR = new double[result1.Length]; resultR[0] = alp1 * result1[0] + (1 - alp1) * result2[0]; resultR[1] = alp2 * result1[1] + (1 - alp2) * result2[1]; var actualR = ML.Core.Mathematics.MathUtils.ArgMax(resultR); if (expected != actualR) { errorsR++; } if ((expected != actual1) && (expected != actual2)) { if (expected == 0) { errorsC1++; } else { errorsC2++; } } cnt++; pct1 = errors1 * 100 / cnt; pct2 = errors2 * 100 / cnt; pctC1 = errorsC1 * 100 / cnt; pctC2 = errorsC2 * 100 / cnt; pctC = (errorsC1 + errorsC2) * 100 / cnt; pctR = errorsR * 100 / cnt; } var alg = new BackpropAlgorithm(m_NetworkF); var err = alg.GetErrors(sample, 0, true); var message = "Errors1: {0}%, Errors2: {1}%, ErrorsC: {2}%, ErrorR: {3}%"; MessageBox.Show(string.Format(message, pct1, pct2, pctC, pctR)); }