// ignore how inaccurate the accuracy results could be if this is ran in parallel :^) private void TrainNetwork(Layer outputLayer, double[] inputs, List <double> accuracyResults, int threadCount, int currentThread) { var rand = new Random(); var output = outputLayer.CloneWithSameWeightValueReferences(); var momentum = Momentum.GenerateMomentum(output, 0.9); var learningRate = 0.25; for (var i = 0; i < 10000; i++) { if (i % 100 == 0) { var currentResults = new double[inputs.Length]; SetResults(inputs, output, currentResults); accuracyResults.Add(AccuracyStatistics.CalculateKolmogorovStatistic( currentResults, inputs.Select(Calculation).ToArray())); } var trial = (rand.NextDouble() / 4) + ((double)currentThread / (double)threadCount); output.Backpropagate(new[] { trial }, new double[] { Calculation(trial) }, learningRate, momentum); ModifyLearningRate(ref learningRate); } }
public bool Fit(TestReport solution) { try { TimeConstraints.Check(solution.Extractor); TimeConstraints.Check(solution.Matcher); AccuracyStatistics performance = new AccuracyStatistics(solution.Matcher.ScoreTables, Measure); string acceptance; if (BestSolution != null) { if (performance.AverageError > BestPerformance.AverageError) { throw new FailedMutationException("Worse accuracy: {0:P2} -> {1:P2}", BestPerformance.AverageError, performance.AverageError); } else if (performance.AverageError == BestPerformance.AverageError) { if (performance.Separation > BestPerformance.Separation) { throw new FailedMutationException("Same accuracy, worse separation: {0:F4} -> {1:F4}", BestPerformance.Separation, performance.Separation); } else if (performance.Separation == BestPerformance.Separation) { if (solution.Matcher.Time.NonMatching > BestSolution.Matcher.Time.NonMatching) { throw new FailedMutationException("Same accuracy, same separation, worse speed: {0:F0} fp/s -> {1:F0} fp/s", 1 / BestSolution.Matcher.Time.NonMatching, 1 / solution.Matcher.Time.NonMatching); } else if (solution.Matcher.Time.NonMatching > BestSolution.Matcher.Time.NonMatching) { throw new FailedMutationException("Same accuracy, same separation, same performance"); } else { acceptance = String.Format("Better speed: {0:F0} fp/s -> {1:F0} fp/s", 1 / BestSolution.Matcher.Time.NonMatching, 1 / solution.Matcher.Time.NonMatching); } } else { acceptance = String.Format("Better separation: {0:F4} -> {1:F4}", BestPerformance.Separation, performance.Separation); } } else { acceptance = String.Format("Better accuracy: {0:P2} -> {1:P2}", BestPerformance.AverageError, performance.AverageError); } } else { acceptance = "Initial"; } bool improved = BestSolution != null && (performance.AverageError <BestPerformance.AverageError || performance.Separation> BestPerformance.Separation); BestSolution = solution; BestPerformance = performance; if (OnAccepted != null) { OnAccepted(acceptance); } return(improved); } catch (FailedMutationException e) { if (OnRejected != null) { OnRejected(e.Message); } return(false); } }