public RunEpoch ( double input, double output ) : double | ||
input | double | Array of input vectors. |
output | double | Array of output vectors. |
return | double |
public void test() { // initialize input and output values var input = new double[4][] { new double[] { 0, 0 }, new double[] { 0, 1 }, new double[] { 1, 0 }, new double[] { 1, 1 } }; var output = new double[4][] { new double[] { 0 }, new double[] { 1 }, new double[] { 1 }, new double[] { 1 } }; // create neural network var network = new ActivationNetwork( new SigmoidFunction(2), 2, // two inputs in the network //2, // two neurons in the first layer 1); // one neuron in the second layer // create teacher var teacher = new BackPropagationLearning(network); // loop while (true) { // run epoch of learning procedure var error = teacher.RunEpoch(input, output); // check error value to see if we need to stop // ... if (error < 0.001) { break; } } Console.WriteLine(network.Compute(new double[] { 0, 0 })[0] + "," + network.Compute(new double[] { 0, 1 })[0] + "," + network.Compute(new double[] { 1, 0 })[0] + "," + network.Compute(new double[] { 1, 1 })[0]); }
public void Test() { ActivationNetwork network = new ActivationNetwork( new SigmoidFunction(), 2, // two inputs in the network 2, // two neurons in the first layer 1); // one neuron in the second layer BackPropagationLearning teacher = new BackPropagationLearning(network); double lastError = double.MaxValue; int counter = 0; while (true) { counter++; var error = teacher.RunEpoch(input, output); if (lastError - error < 0.0000001 && error < 0.001) break; lastError = error; } //var bla = network.Compute(input[0])[0]; //var round = Math.Round(network.Compute(input[0])[0], 2); //var result = output[0][0]; //Assert.IsTrue(Math.Abs(round - result) < double.Epsilon); Assert.IsTrue(Math.Abs(network.Compute(input[0])[0] - output[0][0]) < 0.03); Assert.IsTrue(Math.Abs(network.Compute(input[1])[0] - output[1][0]) < 0.03); Assert.IsTrue(Math.Abs(network.Compute(input[2])[0] - output[2][0]) < 0.03); Assert.IsTrue(Math.Abs(network.Compute(input[3])[0] - output[3][0]) < 0.03); Console.WriteLine($"Loop counter = {counter}."); }
static void Main(string[] args) { // initialize input and output values double[][] input = new double[4][] { new double[] {0, 0}, new double[] {0, 1}, new double[] {1, 0}, new double[] {1, 1} }; double[][] output = new double[4][] { new double[] {0}, new double[] {1}, new double[] {1}, new double[] {0} }; // create neural network ActivationNetwork network = new ActivationNetwork( new SigmoidFunction(1), 2, // two inputs in the network 2, // two neurons in the first layer 1); // one neuron in the second layer // create teacher BackPropagationLearning teacher = new BackPropagationLearning(network); // loop for (int i = 0; i < 10000; i++) { // run epoch of learning procedure double error = teacher.RunEpoch(input, output); // check error value to see if we need to stop // ... Console.Out.WriteLine("#" + i + "\t" + error); } double[] ret1 = network.Compute(new double[] { 0, 0 }); double[] ret2 = network.Compute(new double[] { 1, 0 }); double[] ret3 = network.Compute(new double[] { 0, 1 }); double[] ret4 = network.Compute(new double[] { 1, 1 }); Console.Out.WriteLine(); Console.Out.WriteLine("Eval(0, 0) = " + ret1[0]); Console.Out.WriteLine("Eval(1, 0) = " + ret2[0]); Console.Out.WriteLine("Eval(0, 1) = " + ret3[0]); Console.Out.WriteLine("Eval(1, 1) = " + ret4[0]); Console.ReadLine(); }
public string Learn(string knowFile) { var network = new ActivationNetwork(new BipolarSigmoidFunction(), Constants.StoneCount, 1); var teacher = new BackPropagationLearning(network); //new PerceptronLearning(network); var data = LoadData(knowFile); double error = int.MaxValue; int index = 0; while (error > 1.0 && index++ < 5000) { error = teacher.RunEpoch(data.Item1, data.Item2); } var networkFile = knowFile + ".net"; network.Save(networkFile); Console.WriteLine("Learn: {0}, Gen: {1}", knowFile, networkFile); return networkFile; }
public NeuralNetworkOperations(int characterSize) { neuralNet = new ActivationNetwork(new BipolarSigmoidFunction(2.0f), characterSize, 400, characterCount); neuralNet.Randomize(); teacher = new AForge.Neuro.Learning.BackPropagationLearning(neuralNet); teacher.LearningRate = 0.5f; teacher.Momentum = 0.1f; prepareDataForTeacher(); //var letters = treningLetterListInput.Zip(treningLetterListOutput, (i,o) => new { treningLetterListInput = i, treningLetterListOutput = o }); double err = 400.0f; int count = 0; while(err >= 30.0f) { err = teacher.RunEpoch(treningLetterListInput.ToArray(), treningLetterListOutput.ToArray()); count++; } }
public void Test() { var network = new ActivationNetwork(new SigmoidFunction(), inputCount, firstLayerNeurons, secondLayerNeurons, thirdLayerNeurons, lastLayerNeurons); var teacher = new BackPropagationLearning(network); var lastError = double.MaxValue; var counter = 0; while (true) { counter++; var error = teacher.RunEpoch(input, output); if ((lastError - error < 0.00001 && error < 0.01) || counter > 1200000) break; lastError = error; } var result1 = network.Compute(new double[] {1, 0, 1, 0, 1, 0, 1, 0}); Console.WriteLine($"2 + 2, 2 * 2 = {result1[0]}, {result1[1]}"); var result2 = network.Compute(new double[] {0, 1, 0, 1, 1, 0, 0, 1}); Console.WriteLine($"1 + 1, 2 * 1 = {result2[0]}, {result2[1]}"); var result3 = network.Compute(new double[] {1, 0, 1, 0, 0, 1, 0, 0}); Console.WriteLine($"2 + 2, 1 * 0 = {result3[0]}, {result3[1]}"); var result4 = network.Compute(new double[] {0, 1, 0, 0, 0, 1, 1, 0}); Console.WriteLine($"1 + 0, 1 * 2 = {result4[0]}, {result4[1]}"); }
/* * Naucz sieć neuronową. * Uczy perceptron przy pomocy algorytmu wstecznej propagacji. * Sieć oczy się doputy, dopuki błąd uczenia w danej epoce nie będzie mniejszy niż * dany margines błędu (this.errorRate). Zwraca ilość epok nauczania. * double [][] input - tablica wektorów wejściowych * double [][] output - tablica oczekiwanych wektorów wyjściowych */ public int Learn(double [][] input, double [][] output) { if (input.Length == output.Length) { double samples = (double)input.Length; this.network = new ActivationNetwork(new BipolarSigmoidFunction(this.sigmoidAlphaValue), input[0].Length, this.neuronsInFirstLayer, 1); BackPropagationLearning teacher = new BackPropagationLearning(network); teacher.LearningRate = this.learningRate; teacher.Momentum = this.momentum; int epoch = 0; while (teacher.RunEpoch(input, output) < this.errorRate) { epoch++; } return epoch; } else { throw new Exception("Size of input and output arrays differs!"); } }
static void Learn() { var network = new ActivationNetwork( new SigmoidFunction(), baseMaker.InputSize, arguments.NeuronsCount, baseMaker.OutputSize ); network.Randomize(); foreach (var l in network.Layers) foreach (var n in l.Neurons) for (int i = 0; i < n.Weights.Length; i++) n.Weights[i] = rnd.NextDouble() * 2 - 1; var teacher = new BackPropagationLearning(network); teacher.LearningRate = 1; teacher.Momentum = 0; while (true) { var watch = new Stopwatch(); watch.Start(); while (watch.ElapsedMilliseconds < 500) { teacher.RunEpoch(baseMaker.Inputs, baseMaker.Answers); } watch.Stop(); var count = 0; percentage = new double[baseMaker.OutputSize, baseMaker.OutputSize]; for (int i = 0; i < baseMaker.OutputSize; i++) for (int j = 0; j < baseMaker.OutputSize * 5; j++) { var task = baseMaker.GenerateRandom(i); var output = network.Compute(task); var max = output.Max(); var maxIndex = Enumerable.Range(0, output.Length).Where(z => output[z] == max).First(); percentage[i, maxIndex]++; if (i != maxIndex) totalErrors++; count++; } var maxPercentage = percentage.Cast<double>().Max(); for (int i = 0; i < baseMaker.OutputSize; i++) for (int j = 0; j < baseMaker.OutputSize; j++) percentage[i, j] /= maxPercentage; totalErrors /= count; form.BeginInvoke(new Action(Update)); } }
public void Train(List<Session> train, List<Session> cv, out List<double> trainErrors, out List<double> cvErrors, IActivationFunction function) { trainErrors = new List<double>(); cvErrors = new List<double>(); var count = train.Count; // prepare learning data Console.WriteLine("prepare learning data"); double[][] input = new double[count][]; double[][] output = new double[count][]; // preparing the data for (int i = 0; i < count; i++) { input[i] = CreateInput(train[i]); output[i] = CreateOutput(train[i]); } Console.WriteLine("feature scaling"); mean = new double[inputSize]; dev = new double[inputSize]; for (int i = 0; i < inputSize; i++) { var query = input.Select(p => p[i]); mean[i] = query.Average(); dev[i] = query.Deviation(mean[i]); } for (int i = 0; i < count; i++) for (int j = 0; j < inputSize; j++) { input[i][j] = (input[i][j] - mean[j]) / dev[j]; } Console.WriteLine("prepare cv data"); // prepare cv data double[][] cvIn = new double[cv.Count][]; double[][] cvOut = new double[cv.Count][]; // preparing the data for (int i = 0; i < cv.Count; i++) { cvIn[i] = CreateInput(cv[i]); cvOut[i] = CreateOutput(cv[i]); } Console.WriteLine("cv feature scaling"); for (int i = 0; i < cv.Count; i++) cvIn[i] = ScaleInput(cvIn[i]); Console.WriteLine("create network"); // create perceptron _network = new ActivationNetwork(function, inputSize, inputSize, classesCount); _network.Randomize(); // create teacher //PerceptronLearning teacher = new PerceptronLearning(_network); BackPropagationLearning teacher = new BackPropagationLearning(_network); // set learning rate teacher.LearningRate = 0.01; // loop int iter = 0; double error = 999; double delta = 999; Console.WriteLine("Train Network"); //while (iter < 1000) while (delta > 1 && iter < 5000) //while (iter < 2000) { // run epoch of learning procedure double trainError = teacher.RunEpoch(input, output); double trainError2 = ComputeCVError(_network, input, output); double cvError = ComputeCVError(_network, cvIn, cvOut); delta = Math.Abs(error - trainError); error = trainError; trainErrors.Add(trainError2); cvErrors.Add(cvError); iter++; if (iter % 100 == 0) Console.WriteLine(iter); } Console.WriteLine(iter); }
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { if (!(e.Argument is TrainingOptions)) { backgroundWorker.ReportProgress(0, "Bad thread argument!"); e.Cancel = true; return; } TrainingOptions options = (TrainingOptions)e.Argument; //Create Teacher BackPropagationLearning networkTeacher = new BackPropagationLearning(m_networkContainer.ActivationNetwork); networkTeacher.LearningRate = options.firstLearningRate; networkTeacher.Momentum = options.momentum; //Start Training bool stop = false; int lastStatusEpoch = 0; int lastGraphEpoch = 0; int lastSaveEpoch = 0; backgroundWorker.ReportProgress(0, "Training..."); while (!stop) { #region Training Epoch this.m_networkState.ErrorTraining = networkTeacher.RunEpoch(options.TrainingVectors.Input, options.TrainingVectors.Output) /* / options.TrainingVectors.Input.Length */; this.m_networkState.ErrorValidation = networkTeacher.MeasureEpochError(options.ValidationVectors.Input, options.ValidationVectors.Output) /* / options.ValidationVectors.Input.Length */; // Adjust Training Rate if (options.secondLearningRate.HasValue) networkTeacher.LearningRate = options.secondLearningRate.Value; #endregion #region Mark Network Savepoint if (Properties.Settings.Default.training_Autosave == true && m_networkState.Epoch >= lastSaveEpoch + Properties.Settings.Default.training_AutosaveEpochs) { backgroundWorker.ReportProgress(0,UpdateType.NetworkSave); lastSaveEpoch = m_networkState.Epoch; } #endregion #region Graph Update if (Properties.Settings.Default.graph_Disable == false && m_networkState.Epoch >= lastGraphEpoch + Properties.Settings.Default.graph_UpdateRate) { backgroundWorker.ReportProgress(0,UpdateType.Graph); lastGraphEpoch = m_networkState.Epoch; } #endregion #region Statusbar Update if (Properties.Settings.Default.display_UpdateByTime == false && m_networkState.Epoch >= lastStatusEpoch + Properties.Settings.Default.display_UpdateRate) { if (options.TrainingType == TrainingType.ByError) { if (m_networkState.ErrorTraining != 0) m_networkState.Progress = Math.Max(Math.Min((int)((options.limError * 100) / m_networkState.ErrorTraining), 100), 0); } else { if (m_networkState.Epoch != 0) m_networkState.Progress = Math.Max(Math.Min((int)((m_networkState.Epoch * 100) / options.limEpoch), 100), 0); } backgroundWorker.ReportProgress(0, UpdateType.Statusbar); lastStatusEpoch = m_networkState.Epoch; } #endregion ++m_networkState.Epoch; // Sleep thread according to specified delay System.Threading.Thread.Sleep(Properties.Settings.Default.training_delay); #region Stop Conditions if (options.TrainingType == TrainingType.ByError) { if (m_networkState.ErrorTraining <= options.limError) stop = true; } else if (options.TrainingType == TrainingType.ByEpoch) { if (m_networkState.Epoch >= options.limEpoch) stop = true; } if (backgroundWorker.CancellationPending) { e.Cancel = true; stop = true; } #endregion } backgroundWorker.ReportProgress(0); }
// Worker thread void SearchSolution() { // number of learning samples int samples = data.GetLength(0); // data transformation factor double yFactor = 1.7 / chart.RangeY.Length; double yMin = chart.RangeY.Min; double xFactor = 2.0 / chart.RangeX.Length; double xMin = chart.RangeX.Min; // prepare learning data double[][] input = new double[samples][]; double[][] output = new double[samples][]; for (int i = 0; i < samples; i++) { input[i] = new double[1]; output[i] = new double[1]; // set input input[i][0] = (data[i, 0] - xMin) * xFactor - 1.0; // set output output[i][0] = (data[i, 1] - yMin) * yFactor - 0.85; } // create multi-layer neural network ActivationNetwork network = new ActivationNetwork( new BipolarSigmoidFunction(sigmoidAlphaValue), 1, neuronsInFirstLayer, 1); // create teacher BackPropagationLearning teacher = new BackPropagationLearning(network); // set learning rate and momentum teacher.LearningRate = learningRate; teacher.Momentum = momentum; // iterations int iteration = 1; // solution array double[,] solution = new double[50, 2]; double[] networkInput = new double[1]; // calculate X values to be used with solution function for (int j = 0; j < 50; j++) { solution[j, 0] = chart.RangeX.Min + (double)j * chart.RangeX.Length / 49; } // loop while (!needToStop) { // run epoch of learning procedure double error = teacher.RunEpoch(input, output) / samples; // calculate solution for (int j = 0; j < 50; j++) { networkInput[0] = (solution[j, 0] - xMin) * xFactor - 1.0; solution[j, 1] = (network.Compute(networkInput)[0] + 0.85) / yFactor + yMin; } chart.UpdateDataSeries("solution", solution); // calculate error double learningError = 0.0; for (int j = 0, k = data.GetLength(0); j < k; j++) { networkInput[0] = input[j][0]; learningError += Math.Abs(data[j, 1] - ((network.Compute(networkInput)[0] + 0.85) / yFactor + yMin)); } // set current iteration's info UpdateTextbox(currentIterationBox, iteration.ToString()); //currentIterationBox.Text = iteration.ToString(); UpdateTextbox(currentErrorBox, learningError.ToString("F3")); //currentErrorBox.Text = learningError.ToString("F3"); // increase current iteration iteration++; // check if we need to stop if ((iterations != 0) && (iteration > iterations)) break; } // enable settings controls EnableControls(true); }
private void Worker() { ExtendedIOHelpers.ShowAlert("Generate new samples before training? y/n ", () => GenerateSamples()); if (!LoadSamples()) { return; } Console.WriteLine("Loaded {0} samples", _inputList.Count); var network = new ActivationNetwork(new SigmoidFunction(1), InputCount, OutputCount); var teacher = new BackPropagationLearning(network) { LearningRate = 0.01 }; Console.WriteLine("Start training the network."); int iteration = 0; const int iterations = 5000; double error = 1.0; var st = new Stopwatch(); st.Start(); while (iteration < iterations && error > 0.00005) { error = teacher.RunEpoch(_inputList.ToArray(), _outputList.ToArray()) / _inputList.Count; iteration++; } var time = st.ElapsedMilliseconds / 1000.0; st.Stop(); Console.WriteLine("Network successfully trained! Error = {0:0.######}, Iteration = {1}", error, iteration); Console.WriteLine("Time = {0:0.000} s\n", time); // Normalize weights and convert to string format. var weights = network.Layers .Select(layer => layer.Neurons .Select(neuron => neuron.Weights .Select(x => string.Format("{0,6}", Convert.ToInt32(x * 1000.0))))); ExtendedIOHelpers.ShowAlert("Do you want to save network to file? y/n ", () => { SaveWeights(weights); network.Save(NetworkFile); }); Console.ReadLine(); }
// teach the network public void learn() { network = new ActivationNetwork(new SigmoidFunction(sigmoidAV), 4, 5, 4); teacher = new BackPropagationLearning(network); teacher.LearningRate = learningRate; teacher.Momentum = momentum; error = 0; Random randomG = new Random(); for (int i = 0; i < iteration; i++) { if (randomMomentum) { double random = (randomG.NextDouble() * 0.3) + 0.5; teacher.Momentum = random; } error = teacher.RunEpoch(input, output) / sampleCount; if(outputErrorComponent != null) { outputErrorComponent.Text = "Neural network error is : " + error; } } Console.WriteLine("error is " + error); }
private void SearchSolution() { MemoryStream ms = new MemoryStream(); mNetwork = new ActivationNetwork( new BipolarSigmoidFunction(sigmoidAlphaValue), mInput[0].Length, mHiddenNeurons, mOutput[0].Length); // create teacher BackPropagationLearning teacher = new BackPropagationLearning(mNetwork); // set learning rate and momentum teacher.LearningRate = mLearningRate; teacher.Momentum = mMomentum; bool needToStop = false; int iteration = 0; while (!needToStop) { double error = teacher.RunEpoch(mInput, mOutput) / mInput.Length; mErrors[iteration] = error; double test_error = 0.0; for (int i = 0; i < mTestInput.Length; i++) { double[] test_result = mNetwork.Compute(mTestInput[i]); test_error += ( mTestOutput[i][0] - test_result[0])*( mTestOutput[i][0] - test_result[0]); } mTestErrors[iteration] = Math.Sqrt(test_error); if (min_test_error > mTestErrors[iteration]) { min_test_error = mTestErrors[iteration]; // mTestBestNetwork = mNetwork; ms = new MemoryStream(); mNetwork.Save(this.Id + ".txt"); } iteration++; if (iteration >= mIterations) needToStop = true; } mTestBestNetwork = (ActivationNetwork)ActivationNetwork.Load(this.Id + ".txt"); }
public static void ExactTrainingData() { TrainData[] tdatas; // AForge.Neuro.Learning.BackPropagationLearning beural=new AForge.Neuro.Learning.BackPropagationLearning(new AForge.Neuro.ActivationNetwork( tdatas = traindatas.ToArray(); double[][] output = new double[tdatas.Length-1][]; double[][] input = new double[tdatas.Length-1][]; for (int i = 1; i < tdatas.Length; i++) { int spddiff = 0, voldiff = 0, occdiff = 0, u_spddifft = 0, u_voldifft = 0, u_occdifft = 0, d_spddifft = 0, d_voldifft = 0, d_occdifft = 0,level=0; tdatas[i].getTrainData(ref voldiff, ref spddiff, ref occdiff); u_spddifft = tdatas[i].vd1.AvgSpd - tdatas[i - 1].vd1.AvgSpd; u_voldifft = tdatas[i].vd1.Volume - tdatas[i-1].vd1.Volume; u_occdifft = tdatas[i].vd1.Occupancy - tdatas[i - 1].vd1.Occupancy; d_spddifft = tdatas[i].vd2.AvgSpd - tdatas[i - 1].vd2.AvgSpd; d_voldifft = tdatas[i].vd2.Volume - tdatas[i - 1].vd2.Volume; d_occdifft = tdatas[i].vd2.Occupancy - tdatas[i - 1].vd2.Occupancy; level = tdatas[i-1].Level; output[i-1] = new double[1]; output[i-1][0] = level; input[i-1] = new double[9]; input[i-1][0] = spddiff; input[i-1][1] = voldiff; input[i-1][2] = occdiff; input[i-1][3] = u_spddifft; input[i-1][4] = u_voldifft; input[i-1][5] = u_occdifft; input[i-1][6] = d_spddifft; input[i-1][7] = d_voldifft; input[i-1][8] = d_occdifft; Console.WriteLine(spddiff + "," + voldiff + "," + occdiff + "," + u_spddifft + "," + u_voldifft + "," + u_occdifft + "," + d_spddifft + "," + d_voldifft + "," + d_occdifft+","+level); } ActivationNetwork network = new ActivationNetwork( new BipolarSigmoidFunction(1.5),9, 25, 1 ); // create teacher BackPropagationLearning teacher = new BackPropagationLearning(network); teacher.Momentum = 0.1; teacher.LearningRate = 0.01; // loop double err=100; int cnt = 0; while (err / tdatas.Length >0.00079) { // run epoch of learning procedure err = teacher.RunEpoch( input, output ); Console.WriteLine(err / tdatas.Length); cnt++; } for (int i = 0; i < tdatas.Length-1; i++) { if (System.Convert.ToInt32(output[i][0]) != System.Convert.ToInt32(network.Compute(input[i])[0])) Console.WriteLine("fail"); // Console.WriteLine("chreck:"+Math.Abs((output[i][0] - network.Compute(input[i])[0]))>0,5?:"fail",""); } }
public EstimationResult Estimate(IEnumerable<IDateValue> dateValues) { var data = dateValues.ToArray(); var samplesCount = data.Length - LayerWidth; var factor = 1.7 / data.Length; var yMin = data.Min(x => x.Value); var input = new double[samplesCount][]; var output = new double[samplesCount][]; for (var i = 0; i < samplesCount; i++) { input[i] = new double[LayerWidth]; output[i] = new double[1]; for (var j = 0; j < LayerWidth; j++) input[i][j] = (data[i + j].Value - yMin) * factor - 0.85; output[i][0] = (data[i + LayerWidth].Value - yMin) * factor - 0.85; } var network = new ActivationNetwork( new BipolarSigmoidFunction(SigmoidAlphaValue), LayerWidth, LayerWidth * 2, 1); var teacher = new BackPropagationLearning(network) { LearningRate = LearningRate, Momentum = Momentum }; var solutionSize = data.Length - LayerWidth; var solution = new double[solutionSize, 2]; var networkInput = new double[LayerWidth]; for (var j = 0; j < solutionSize; j++) solution[j, 0] = j + LayerWidth; TimesLoop.Do(Iterations, () => { teacher.RunEpoch(input, output); for (int i = 0, n = data.Length - LayerWidth; i < n; i++) { for (var j = 0; j < LayerWidth; j++) networkInput[j] = (data[i + j].Value - yMin) * factor - 0.85; solution[i, 1] = (network.Compute(networkInput)[0] + 0.85) / factor + yMin; } }); return EstimationResult.Create(solution[0, 1], this); }
static void Main(string[] args) { Console.WriteLine("This is a demo application that combines Linear Discriminant Analysis (LDA) and Multilayer Perceptron(MLP)."); double[,] inputs = { { 4, 1 }, { 2, 4 }, { 2, 3 }, { 3, 6 }, { 4, 4 }, { 9, 10 }, { 6, 8 }, { 9, 5 }, { 8, 7 }, { 10, 8 } }; int[] output = { 1, 1, 2, 1, 1, 2, 2, 2, 1, 2 }; Console.WriteLine("\r\nProcessing sample data, pease wait..."); //1.1 var lda = new LinearDiscriminantAnalysis(inputs, output); //1.2 Compute the analysis lda.Compute(); //1.3 double[,] projection = lda.Transform(inputs); //both LDA and MLP have a little bit different inputs //e.x double[,] to double[][], etc. //e.x. LDA needs int classes and MLP needs classes to be in the range [0..1] #region convertions int vector_count = projection.GetLength(0); int dimensions = projection.GetLength(1); //==================================================================== // conver for NN double[][] input2 = new double[vector_count][]; double[][] output2 = new double[vector_count][]; for (int i = 0; i < input2.Length; i++) { input2[i] = new double[projection.GetLength(1)]; for (int j = 0; j < projection.GetLength(1); j++) { input2[i][j] = projection[i, j]; } output2[i] = new double[1]; //we turn classes from ints to doubles in the range [0..1], because we use sigmoid for the NN output2[i][0] = Convert.ToDouble(output[i]) / 10; } #endregion //2.1 create neural network ActivationNetwork network = new ActivationNetwork( new SigmoidFunction(2), dimensions, // inputs neurons in the network dimensions, // neurons in the first layer 1); // one neuron in the second layer //2.2 create teacher BackPropagationLearning teacher = new BackPropagationLearning(network); //2.3 loop int p = 0; while (true) { // run epoch of learning procedure double error = teacher.RunEpoch(input2, output2); p++; if (p > 1000000) break; // instead of iterations we can check error values to see if we need to stop } //==================================================================== //3. Classify double[,] sample = { { 10, 8 } }; double[,] projectedSample = lda.Transform(sample); double[] projectedSample2 = new double[2]; projectedSample2[0] = projectedSample[0, 0]; projectedSample2[1] = projectedSample[0, 1]; double[] classs = network.Compute(projectedSample2); Console.WriteLine("========================"); //we convert back to int classes by first rounding and then multipling by 10 (because we devided to 10 before) //if you do not get the expected result //- rounding might be a problem //- try more training Console.WriteLine(Math.Round(classs[0], 1, MidpointRounding.AwayFromZero)*10); Console.ReadLine(); }
public void Learn() { var network = new ActivationNetwork(new BipolarSigmoidFunction(), Constants.StoneCount, 1); var teacher = new BackPropagationLearning(network);//new PerceptronLearning(network); var data = LoadData("4-6-2012-04-24.know"); double error = 1.0; int index = 0; while (error > 0.001 && index < 100000) { error = teacher.RunEpoch(data.Item1, data.Item2); index++; } network.Save("4-6-2012-04-24.bp.net"); var text = "□○○○●○○□○●●□□●□□"; var i = ToDouble(text);//-2 var o = network.Compute(i); var eval = o[0] * 2 * Constants.StoneCount - Constants.StoneCount; Console.WriteLine("{0} {1}", text, eval); }
// Worker thread void SearchSolution() { // initialize input and output values double[][] input = null; double[][] output = null; if (sigmoidType == 0) { // unipolar data input = new double[4][] { new double[] {0, 0}, new double[] {0, 1}, new double[] {1, 0}, new double[] {1, 1} }; output = new double[4][] { new double[] {0}, new double[] {1}, new double[] {1}, new double[] {0} }; } else { // biipolar data input = new double[4][] { new double[] {-1, -1}, new double[] {-1, 1}, new double[] { 1, -1}, new double[] { 1, 1} }; output = new double[4][] { new double[] {-1}, new double[] { 1}, new double[] { 1}, new double[] {-1} }; } // create perceptron ActivationNetwork network = new ActivationNetwork( (sigmoidType == 0) ? (IActivationFunction)new SigmoidFunction(sigmoidAlphaValue) : (IActivationFunction)new BipolarSigmoidFunction(sigmoidAlphaValue), 2, 2, 1); // create teacher BackPropagationLearning teacher = new BackPropagationLearning(network); // set learning rate and momentum teacher.LearningRate = learningRate; teacher.Momentum = momentum; // iterations int iteration = 1; // statistic files StreamWriter errorsFile = null; try { // check if we need to save statistics to files if (saveStatisticsToFiles) { // open files errorsFile = File.CreateText("errors.csv"); } // erros list List<double> errorsList = new List<double>(); // loop while (!needToStop) { // run epoch of learning procedure double error = teacher.RunEpoch(input, output); errorsList.Add(error); // save current error if (errorsFile != null) { errorsFile.WriteLine(error); } // show current iteration & error UpdateTextbox(currentIterationBox, iteration.ToString()); //currentIterationBox.Text = iteration.ToString(); UpdateTextbox(currentErrorBox, error.ToString()); //currentErrorBox.Text = error.ToString(); iteration++; // check if we need to stop if (error <= learningErrorLimit) break; } // show error's dynamics double[,] errors = new double[errorsList.Count, 2]; for (int i = 0, n = errorsList.Count; i < n; i++) { errors[i, 0] = i; errors[i, 1] = errorsList[i]; } errorChart.RangeX = new DoubleRange(0, errorsList.Count - 1); errorChart.UpdateDataSeries("error", errors); } catch (IOException) { MessageBox.Show("Failed writing file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { // close files if (errorsFile != null) errorsFile.Close(); } // enable settings controls EnableControls(true); }
private void Button_Click_1(object sender, RoutedEventArgs e) { var learningTask = new Task(delegate { var learningData = GenerateInputOutput().Take(10); BackPropagationLearning trainer = new BackPropagationLearning(net); double prErr = 10000000; // Ошибка сети double error = 100; // Сначала скорость обучения должна быть высока trainer.LearningRate = 100; int iteration = 0; // Обучаем сеть пока ошибка сети станет небольшой while (error > 0.001) { iteration++; // Получаем ошибку сети var che = false; Dispatcher.Invoke(DispatcherPriority.Normal, new Action( delegate { if (ShowResultsCheckbox.IsChecked != null) che = ShowResultsCheckbox.IsChecked.Value; })); if (!che) { error = trainer.RunEpoch(learningData.Select(i => i.Item1).ToArray(), learningData.Select(i => i.Item2).ToArray()); Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate { label.Content = error + "\nIteration: " + iteration; })); } else { Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate { label.Content = "See?"; })); foreach (var data in learningData) { Dispatcher.Invoke(DispatcherPriority.Normal, new Action( delegate { if (ShowResultsCheckbox.IsChecked != null) che = ShowResultsCheckbox.IsChecked.Value; })); if (!che) break; // Если ошибка сети изменилась на небольшое значения, в сравнении ошибкой предыдущей эпохи Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate { _answerLable.Content = String.Join(", ", data.Item2.Select(d => d.ToString("N2"))); _learnedLable.Content = String.Join(", ", net.Compute(data.Item1).Select(d => d.ToString("N2"))); im.Source = BitmapSourceConvert.ToBitmapSource(data.Item3); im.UpdateLayout(); })); Thread.Sleep(1000); } } if (Math.Abs(error - prErr) < 0.000000001) { // Уменьшаем коэффициент скорости обучения на 2 trainer.LearningRate /= 2; if (trainer.LearningRate < 0.001) trainer.LearningRate = 0.001; } prErr = error; learningData = GenerateInputOutput().Take(10); } }); learningTask.Start(); }
// Worker thread void SearchSolution( ) { // number of learning samples int samples = data.Length - predictionSize - windowSize; // data transformation factor double factor = 1.7 / chart.RangeY.Length; double yMin = chart.RangeY.Min; // prepare learning data double[][] input = new double[samples][]; double[][] output = new double[samples][]; for ( int i = 0; i < samples; i++ ) { input[i] = new double[windowSize]; output[i] = new double[1]; // set input for ( int j = 0; j < windowSize; j++ ) { input[i][j] = ( data[i + j] - yMin ) * factor - 0.85; } // set output output[i][0] = ( data[i + windowSize] - yMin ) * factor - 0.85; } // create multi-layer neural network ActivationNetwork network = new ActivationNetwork( new BipolarSigmoidFunction( sigmoidAlphaValue ), windowSize, windowSize * 2, 1 ); // create teacher BackPropagationLearning teacher = new BackPropagationLearning( network ); // set learning rate and momentum teacher.LearningRate = learningRate; teacher.Momentum = momentum; // iterations int iteration = 1; // solution array int solutionSize = data.Length - windowSize; double[,] solution = new double[solutionSize, 2]; double[] networkInput = new double[windowSize]; // calculate X values to be used with solution function for ( int j = 0; j < solutionSize; j++ ) { solution[j, 0] = j + windowSize; } // loop while ( !needToStop ) { // run epoch of learning procedure double error = teacher.RunEpoch( input, output ) / samples; // calculate solution and learning and prediction errors double learningError = 0.0; double predictionError = 0.0; // go through all the data for ( int i = 0, n = data.Length - windowSize; i < n; i++ ) { // put values from current window as network's input for ( int j = 0; j < windowSize; j++ ) { networkInput[j] = ( data[i + j] - yMin ) * factor - 0.85; } // evalue the function solution[i, 1] = ( network.Compute( networkInput)[0] + 0.85 ) / factor + yMin; // calculate prediction error if ( i >= n - predictionSize ) { predictionError += Math.Abs( solution[i, 1] - data[windowSize + i] ); } else { learningError += Math.Abs( solution[i, 1] - data[windowSize + i] ); } } // update solution on the chart chart.UpdateDataSeries( "solution", solution ); // set current iteration's info SetText( currentIterationBox, iteration.ToString( ) ); SetText( currentLearningErrorBox, learningError.ToString( "F3" ) ); SetText( currentPredictionErrorBox, predictionError.ToString( "F3" ) ); // increase current iteration iteration++; // check if we need to stop if ( ( iterations != 0 ) && ( iteration > iterations ) ) break; } // show new solution for ( int j = windowSize, k = 0, n = data.Length; j < n; j++, k++ ) { AddSubItem( dataList, j, solution[k, 1].ToString( ) ); } // enable settings controls EnableControls( true ); }
private void DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; e.Result = new double[0]; if (worker.CancellationPending) { e.Cancel = true; return; } TrainingDialog obj = (TrainingDialog)e.Argument; _Series++; #region Prepare data to be trained. Involves copying. int numTrainingSets = obj._DataSet.Training.Rows.Count; ArrayList inputs = new ArrayList(); ArrayList outputs = new ArrayList(); for (int i = 0; i < numTrainingSets; i++) { // Input data double[] inData = new double[_NumInputNeurons]; inData[0] = obj._DataSet.Training[i].LipCornerLeftX; inData[1] = obj._DataSet.Training[i].LipCornerLeftY; inData[2] = obj._DataSet.Training[i].LipCornerRightX; inData[3] = obj._DataSet.Training[i].LipCornerRightY; inData[4] = obj._DataSet.Training[i].LipUpLeftX; inData[5] = obj._DataSet.Training[i].LipUpLeftY; inData[6] = obj._DataSet.Training[i].LipUpCenterX; inData[7] = obj._DataSet.Training[i].LipUpCenterY; inData[8] = obj._DataSet.Training[i].LipUpRightX; inData[9] = obj._DataSet.Training[i].LipUpRightY; inData[10] = obj._DataSet.Training[i].LipBottomLeftX; inData[11] = obj._DataSet.Training[i].LipBottomLeftY; inData[12] = obj._DataSet.Training[i].LipBottomCenterX; inData[13] = obj._DataSet.Training[i].LipBottomCenterY; inData[14] = obj._DataSet.Training[i].LipBottomRightX; inData[15] = obj._DataSet.Training[i].LipBottomRightY; inData[16] = obj._DataSet.Training[i].LeftEyeCenterX; inData[17] = obj._DataSet.Training[i].LeftEyeCenterY; inData[18] = obj._DataSet.Training[i].LeftLidBottomX; inData[19] = obj._DataSet.Training[i].LeftLidBottomY; inData[20] = obj._DataSet.Training[i].LeftLidCornerLeftX; inData[21] = obj._DataSet.Training[i].LeftLidCornerLeftY; inData[22] = obj._DataSet.Training[i].LeftLidCornerRightX; inData[23] = obj._DataSet.Training[i].LeftLidCornerRightY; inData[24] = obj._DataSet.Training[i].LeftLidUpX; inData[25] = obj._DataSet.Training[i].LeftLidUpY; inData[26] = obj._DataSet.Training[i].MouthCenterX; inData[27] = obj._DataSet.Training[i].MouthCenterY; inData[28] = obj._DataSet.Training[i].RightEyeCenterX; inData[29] = obj._DataSet.Training[i].RightEyeCenterY; inData[30] = obj._DataSet.Training[i].RightLidBottomX; inData[31] = obj._DataSet.Training[i].RightLidBottomY; inData[32] = obj._DataSet.Training[i].RightLidCornerLeftX; inData[33] = obj._DataSet.Training[i].RightLidCornerLeftY; inData[34] = obj._DataSet.Training[i].RightLidCornerRightX; inData[35] = obj._DataSet.Training[i].RightLidCornerRightY; inData[36] = obj._DataSet.Training[i].RightLidUpX; inData[37] = obj._DataSet.Training[i].RightLidUpY; inputs.Add(inData); // Output data double[] outData = new double[_NumOutputNeurons]; int eid = obj._DataSet.Training[i].ExpressionOID; string expression = obj._DataSet.Expression.FindByExpressionOID(eid).Expression.ToLower(); outData[0] = expression.Contains("anger") ? 1 : 0; outData[1] = expression.Contains("disg") ? 1 : 0; outData[2] = expression.Contains("fear") ? 1 : 0; outData[3] = expression.Contains("happy") ? 1 : 0; outData[4] = expression.Contains("neutr") ? 1 : 0; outData[5] = expression.Contains("sad") ? 1 : 0; outData[6] = expression.Contains("surp") ? 1 : 0; outputs.Add(outData); } #endregion #region Norm datasets per input neuron for (int j = 0; j < _NumInputNeurons; j++) { double min = 100000000.0; double max = -100000000.0; for (int i = 0; i < numTrainingSets; i++) { double cur = ((double[])inputs[i])[j]; if (min > cur) { min = cur; } if (max < cur) { max = cur; } } for (int i = 0; (max - min) != 0 && i < numTrainingSets; i++) { ((double[])inputs[i])[j] = (((double[])inputs[i])[j] - min) / (max - min); } } #endregion #region Pick random train-, validate and test datasets // Like Mr. Schneider ;) int numTestDataSets = (int)Math.Floor((double)numTrainingSets * 0.1); int numValidationDataSets = (int)Math.Floor((double)(numTrainingSets - numTestDataSets) * 0.2); int numTrainDataSets = numTrainingSets - numTestDataSets - numValidationDataSets; Random rand = new Random(); // Get random training data double[][] trainingInputs = new double[numTrainDataSets][]; double[][] trainingOutputs = new double[numTrainDataSets][]; for (int i = 0; i < trainingInputs.GetLength(0); i++) { int idx = rand.Next(inputs.Count); trainingInputs[i] = (double[])inputs[idx]; trainingOutputs[i] = (double[])outputs[idx]; inputs.Remove(idx); outputs.Remove(idx); } // Get random validation data double[][] validateInputs = new double[numValidationDataSets][]; double[][] validateOutputs = new double[numValidationDataSets][]; for (int i = 0; i < validateInputs.GetLength(0); i++) { int idx = rand.Next(numTrainDataSets); validateInputs[i] = trainingInputs[idx]; validateOutputs[i] = trainingOutputs[idx]; } // Get random test data double[][] testInputs = new double[numTestDataSets][]; double[][] testOutputs = new double[numTestDataSets][]; for (int i = 0; i < testInputs.GetLength(0); i++) { int idx = rand.Next(inputs.Count); testInputs[i] = (double[])inputs[idx]; testOutputs[i] = (double[])outputs[idx]; inputs.Remove(idx); outputs.Remove(idx); } #endregion #region _Network.SetActivationFunction(new SigmoidFunction(_SigmoidAlpha)); BackPropagationLearning teacher = new BackPropagationLearning(_Network); teacher.LearningRate = obj._LearningRate; teacher.Momentum = obj._Momentum; double error = 1; double maxError = -10000.0; int maxIterations = obj._MaxIterations; double epsilon = obj._Epsilon; // Prepare the error Chart double[,] errorValues = new double[maxIterations, 2]; ProgressState state = new ProgressState(); double[] errors = new double[maxIterations]; for (int i = 0; i < maxIterations || error <= epsilon; i++) { if (maxIterations > 0) { if (i >= maxIterations) { break; } } if (epsilon > 0.0) { if (error <= epsilon) { break; } } // Abort if use requested it if (worker.CancellationPending) { e.Cancel = true; break; } // Train error = teacher.RunEpoch(trainingInputs, trainingOutputs); // Store error for result errors[i] = error; state.Error = error; // Plot size if (maxError < error) { maxError = error; } // Plot values errorValues[i, 0] = i; errorValues[i, 1] = error; // Report progress if (i % 10 == 0 || i == (maxIterations - 1)) { state.Iteration = i + 1; state.Error = error; worker.ReportProgress((int)((float)(100 * (i + 1)) / (float)maxIterations), state); } } e.Result = errors; obj._Chart.RangeX = new DoubleRange(0.0, (double)maxIterations); obj._Chart.RangeY = new DoubleRange(0.0, maxError); // add new data series to the chart obj._Chart.AddDataSeries("Error " + _Series, System.Drawing.Color.DarkGreen, Chart.SeriesType.ConnectedDots, 2); // update the chart obj._Chart.UpdateDataSeries("Error " + _Series, errorValues); #endregion }