/// <summary> /// Save results of a single learning (a single run) - just 1 network, 1 learning. /// Saves error plot, regression plot, and info about the run. /// </summary> /// <param name="layersVal"></param> /// <param name="learningSettings"></param> /// <param name="report"></param> /// <param name="regressionPlot"></param> /// <param name="errorPlot"></param> /// <param name="network"></param> private void SaveSingleRunData(LearningSettings learningSettings, SingleRunReport report, PlotModel regressionPlot, PlotModel errorPlot, INetwork network) // could be refactored -> use MainWindow fields or create a class { DateTime time = report.Time; List <int> layersVal = RetrieveLayersVal(learningSettings); string prefix = report.Name; if (eid.ReportingOptions.ShouldSavePlots) { string regressionFileName = prefix + "_regression.png"; string regressionSavePath = System.IO.Path.Combine(innerResultsPath, regressionFileName); using (FileStream fileStream = new FileStream(regressionSavePath, FileMode.CreateNew)) { PngExporter.Export(regressionPlot, fileStream, 900, 900, OxyColors.White); } string errorFileName = prefix + "_error.png"; string errorSavePath = System.IO.Path.Combine(innerResultsPath, errorFileName); using (FileStream fileStream = new FileStream(errorSavePath, FileMode.CreateNew)) { PngExporter.Export(errorPlot, fileStream, 900, 900, OxyColors.White); } } if (eid.ReportingOptions.ShouldSaveRunInfos) { string infoFileName = prefix + "_info.txt"; string infoSavePath = System.IO.Path.Combine(innerResultsPath, infoFileName); FileManager.SaveTextFile(infoSavePath, GetResultInfo(learningSettings, report.LearningResult, layersVal, network, time)); } }
public AggregateResult(List <SingleRunReport> sortedReports, LearningSettings settings) { Errors = new DenseVector(sortedReports.Count); MPVer1 = new DenseVector(sortedReports.Count); MPVer2 = new DenseVector(sortedReports.Count); Iterations = new DenseVector(sortedReports.Count); List <TimeSpan> timesTaken = new List <TimeSpan>(sortedReports.Count); for (int i = 0; i < sortedReports.Count; i++) { Errors[i] = sortedReports[i].LearningResult.TestSetError; MPVer1[i] = sortedReports[i].LearningResult.MPVer1; MPVer2[i] = sortedReports[i].LearningResult.MPVer2; Iterations[i] = sortedReports[i].LearningResult.IterationsExecuted; timesTaken.Add(sortedReports[i].LearningResult.TimeTaken); } RunCount = sortedReports.Count; SettingsUsed = settings; AverageError = Errors.Average(); AverageSecondsTaken = timesTaken.Average(x => x.TotalSeconds); Reports = sortedReports.ToList(); // create copies of references BestErrorIndex = Errors.MinimumIndex(); PercentageLearningStuck = (double)sortedReports.Count(x => x.LearningResult.GotStuck) / (double)sortedReports.Count; }
private List <int> RetrieveLayersVal(LearningSettings learningSettings) { List <int> layersVal = new List <int>() { eid.InputCount }; layersVal.AddRange(learningSettings.HiddenNeuronCounts); layersVal.Add(eid.OutputCount); return(layersVal); }
private string GetResultInfo(LearningSettings settings, LearningResult result, List <int> neuronCounts, INetwork network, DateTime now) { StringBuilder sb = new StringBuilder(); sb.AppendLine(settings.ToString()); sb.Append(now.ToLongDateString()); sb.Append(" "); sb.AppendLine(now.ToLongTimeString()); sb.AppendLine(System.IO.Path.GetFileName(eid.DataSetName)); sb.AppendFormat("Layer counts: {0}\r\n", string.Join("-", neuronCounts)); sb.AppendLine(result.ToString()); return(sb.ToString()); }
private List <int> BuildLayersVal(LearningSettings lSettings) { List <int> ret = new List <int>(); ret.Add(eid.InputCount); foreach (int neuronCount in lSettings.HiddenNeuronCounts) { ret.Add(neuronCount); } ret.Add(eid.OutputCount); return(ret); }
private void ProcessSingleResultEntry(LearningSettings settings, SingleRunReport result) { RegressionPlotBuilder regressionBuilder = new RegressionPlotBuilder(); PlotModel regressionPlot = regressionBuilder.Build1DRegressionModel(result.TrainSet, result.TestSet, eid.PlotAgainstInput); //ErrorPlotBuilder errorBuilder = new ErrorPlotBuilder(eid.ErrorScale); ErrorPlotBuilder errorBuilder = new ErrorPlotBuilder(); PlotModel errorPlot = errorBuilder.SetUpModel(result.LearningResult.MSEHistory); SaveSingleRunData(settings, result, regressionPlot, errorPlot, result.Network); if (eid.ReportingOptions.ShouldDisplayPlots) { DisplayResults(regressionPlot, errorPlot, result.LearningResult, result.Name); } }
public async Task TestCancel() { IInputLayer inputLayer = new InputLayer(() => new InputNode(), 2, new Bias()); var innerLayer = new Layer(() => new Neuron(new Logistic(0.888)), 3, new Bias()); var outputLayer = new Layer(new Neuron(new Logistic(0.777))); var network = new Network { InputLayer = inputLayer, OutputLayer = outputLayer }; network.AddInnerLayer(innerLayer); var generator = new EachToEachSynapseGenerator(new Random()); generator.Generate(network, inputLayer, innerLayer); generator.Generate(network, innerLayer, outputLayer); var samples = new List <ILearningSample> { new LearningSample(new double[] { 0, 1 }, new double[] { 1 }), new LearningSample(new double[] { 1, 0 }, new double[] { 1 }), new LearningSample(new double[] { 0, 0 }, new double[] { 0 }), new LearningSample(new double[] { 1, 1 }, new double[] { 0 }) }; var strategy = new BackpropagationStrategy(); var settings = new LearningSettings { EpochRepeats = 20000 }; var learning = new Learning <Network, ILearningSample>(network, strategy, settings); var cts = new CancellationTokenSource(); var task = Task.Run(async() => { await Task.Delay(1000); cts.Cancel(); }); await Assert.ThrowsAsync <OperationCanceledException>(async() => await learning.Learn(samples, cts.Token)); }
public LearningResult Run(ILearningStrategy learningStrategy, LearningSettings lSettings, IStatusReporter statusHolder) { //AssertArguments(trainData, testData, learningRate); // TODO: write assertions if (!network.IsInitialized) { network.Initialize(CreationModes.RandomizeWeights); } LearningResult learningResult = new LearningResult(); learningResult.MSEHistory = learningStrategy.Train(network, trainSet, statusHolder); learningResult.TestSetError = AnswerTestSet(); learningResult.DirectionGuessRate = CalculateDirectionGuessed(DirectionGuess.NetworkToNetwork); learningResult.DirectionGuessVer2 = CalculateDirectionGuessed(DirectionGuess.IdealToNetwork); learningResult.TimeTaken = learningStrategy.TimeTaken; learningResult.GotStuck = learningStrategy.GotStuck; return(learningResult); }
private static void BuildSettingsImplementation(Dictionary <string, List <string> > groupedParameters, Stack <string> titleStack, LearningSettings currentSetting, List <LearningSettings> result) { if (titleStack.Count == 0) { // recursion end result.Add(currentSetting.Clone()); // add a snapshot of the current settings to the result return; } string paramTitle = titleStack.Pop(); foreach (string paramValue in groupedParameters[paramTitle]) { currentSetting.SetParamByTitle(paramTitle, paramValue); BuildSettingsImplementation(groupedParameters, titleStack, currentSetting, result); } titleStack.Push(paramTitle); }
public async Task TestTeachLite() { var inputLayer = new InputLayer(new InputNode(), new Bias()); var innerLayer = new Layer(new Neuron(new Rectifier())); var outputLayer = new Layer(new Neuron(new Rectifier())); var network = new Network { InputLayer = inputLayer, OutputLayer = outputLayer }; network.AddInnerLayer(innerLayer); var generator = new EachToEachSynapseGenerator(new Random()); generator.Generate(network, inputLayer, innerLayer); generator.Generate(network, innerLayer, outputLayer); var samples = new List <ILearningSample> { new LearningSample(new double[] { 0 }, new double[] { 1 }), }; var strategy = new BackpropagationStrategy(); var settings = new LearningSettings { EpochRepeats = 10000, InitialTheta = THETA, ThetaFactorPerEpoch = epoch => 0.9995 }; var learning = new Learning <Network, ILearningSample>(network, strategy, settings); await learning.Learn(samples); await network.Input(new double[] { 1 }); var output = (await network.Output()).First(); Assert.True(Math.Abs(output) < DELTA); }
private List <LearningSettings> GetLearningSettingsFromUI() { LearningSettings lSettings = new LearningSettings(); string[] maxIters = GetSimulatedParamsTexts(LearningSettings.MaxIterationsTitle, MaxIterations.Text); string[] badIters = GetSimulatedParamsTexts(LearningSettings.BadIterationsTitle, BadIterations.Text == ""? (int.MaxValue / 5).ToString() : BadIterations.Text); string[] lr = GetSimulatedParamsTexts(LearningSettings.LearningRateTitle, LearningRate.Text); string[] mnt = GetSimulatedParamsTexts(LearningSettings.MomentumTitle, Momentum.Text); ActivationType aType = (ActivationType)ActivationCombobox.SelectedItem; string aTypeText = ""; if (aType == ActivationType.Unipolar) { aTypeText = "U"; } else if (aType == ActivationType.Bipolar) { aTypeText = "B"; } else { throw new Exception("Unrecognized activation type"); }; string[] act = GetSimulatedParamsTexts(LearningSettings.ActivationFuncTitle, aTypeText); string[] hls = GetSimulatedParamsTexts(LearningSettings.HLTitle, LayersTextBox.Text); List <string[]> simulatedSplitLines = new List <string[]>() { maxIters, badIters, lr, mnt, act, hls }; StringBuilder inputText = new StringBuilder(); simulatedSplitLines.ForEach(l => inputText.AppendLine(string.Join(",", l))); paramsInputText = inputText.ToString(); return(SettingsMixer.BuildSettings(simulatedSplitLines)); }
private INetwork CreateNetwork(LearningSettings learningSettings, List <int> layersVal) { INetwork network = null; switch (eid.NetworkType) { case NetworkType.MLP: network = new NeuralNetwork(learningSettings.Activation, eid.UseBiases, layersVal.ToArray()); break; case NetworkType.Jordan: network = new RecursiveNetwork(RecursiveNetwork.Type.Jordan, learningSettings.Activation, eid.UseBiases, layersVal[0], layersVal[1], layersVal[2]); break; case NetworkType.Elman: network = new RecursiveNetwork(RecursiveNetwork.Type.Elman, learningSettings.Activation, eid.UseBiases, layersVal[0], layersVal[1], layersVal[2]); break; } return(network); }
public async Task TestTeachXor() { IInputLayer inputLayer = new InputLayer(() => new InputNode(), 2, new Bias()); var innerLayer = new Layer(() => new Neuron(new Logistic(0.888)), 3, new Bias()); var outputLayer = new Layer(new Neuron(new Logistic(0.777))); var network = new Network { InputLayer = inputLayer, OutputLayer = outputLayer }; network.AddInnerLayer(innerLayer); foreach (var layer in network.Layers) { foreach (var node in layer.Nodes) { node.OnResultCalculated += (n, v) => { Debug.WriteLine($"{n}: {v}"); return(Task.CompletedTask); }; } } var generator = new EachToEachSynapseGenerator(new Random()); generator.Generate(network, inputLayer, innerLayer); generator.Generate(network, innerLayer, outputLayer); var samples = new List <ILearningSample> { new LearningSample(new double[] { 0, 1 }, new double[] { 1 }), new LearningSample(new double[] { 1, 0 }, new double[] { 1 }), new LearningSample(new double[] { 0, 0 }, new double[] { 0 }), new LearningSample(new double[] { 1, 1 }, new double[] { 0 }) }; await network.Input(new double[] { 1, 0 }); var beforeLearning = network.LastCalculatedValue.First(); var strategy = new BackpropagationStrategy(); var settings = new LearningSettings { EpochRepeats = 10000, InitialTheta = THETA, ThetaFactorPerEpoch = epoch => 0.9999, ShuffleEveryEpoch = true }; var learning = new Learning <Network, ILearningSample>(network, strategy, settings); await learning.Learn(samples); await network.Input(new double[] { 1, 0 }); var afterLearning = (await network.Output()).First(); Assert.True(beforeLearning < afterLearning); await network.Input(new double[] { 1, 0 }); var output = (await network.Output()).First(); Assert.True(Math.Abs(1 - output) < DELTA); await network.Input(new double[] { 1, 1 }); output = (await network.Output()).First(); Assert.True(Math.Abs(0 - output) < DELTA); await network.Input(new double[] { 0, 0 }); output = (await network.Output()).First(); Assert.True(Math.Abs(0 - output) < DELTA); await network.Input(new double[] { 0, 1 }); output = (await network.Output()).First(); Assert.True(Math.Abs(1 - output) < DELTA); }