public void MulticlassTest1() { Accord.Math.Tools.SetupGenerator(0); // Neuron.RandGenerator = new ThreadSafeRandom(0); int numberOfInputs = 3; int numberOfClasses = 4; int hiddenNeurons = 5; double[][] input = { new double[] { -1, -1, -1 }, // 0 new double[] { -1, 1, -1 }, // 1 new double[] { 1, -1, -1 }, // 1 new double[] { 1, 1, -1 }, // 0 new double[] { -1, -1, 1 }, // 2 new double[] { -1, 1, 1 }, // 3 new double[] { 1, -1, 1 }, // 3 new double[] { 1, 1, 1 } // 2 }; int[] labels = { 0, 1, 1, 0, 2, 3, 3, 2, }; double[][] outputs = Accord.Statistics.Tools .Expand(labels, numberOfClasses, -1, 1); var function = new BipolarSigmoidFunction(2); var network = new ActivationNetwork(function, numberOfInputs, hiddenNeurons, numberOfClasses); new NguyenWidrow(network).Randomize(); var teacher = new LevenbergMarquardtLearning(network); double error = Double.PositiveInfinity; for (int i = 0; i < 10; i++) error = teacher.RunEpoch(input, outputs); for (int i = 0; i < input.Length; i++) { int answer; double[] output = network.Compute(input[i]); double response = output.Max(out answer); int expected = labels[i]; Assert.AreEqual(expected, answer); } }
public void RunEpochTest1() { Accord.Math.Tools.SetupGenerator(0); double[][] input = { new double[] { -1, -1 }, new double[] { -1, 1 }, new double[] { 1, -1 }, new double[] { 1, 1 } }; double[][] output = { new double[] { -1 }, new double[] { 1 }, new double[] { 1 }, new double[] { -1 } }; //Neuron.RandGenerator = new ThreadSafeRandom(0); ActivationNetwork network = new ActivationNetwork( new BipolarSigmoidFunction(2), 2, 2, 1); var teacher = new ParallelResilientBackpropagationLearning(network); double error = 1.0; while (error > 1e-5) error = teacher.RunEpoch(input, output); for (int i = 0; i < input.Length; i++) { double actual = network.Compute(input[i])[0]; double expected = output[i][0]; Assert.AreEqual(expected, actual, 0.01); Assert.IsFalse(Double.IsNaN(actual)); } }
public void MulticlassTest1() { Accord.Math.Tools.SetupGenerator(0); // Suppose we would like to teach a network to recognize // the following input vectors into 3 possible classes: // double[][] inputs = { new double[] { 0, 1, 1, 0 }, // 0 new double[] { 0, 1, 0, 0 }, // 0 new double[] { 0, 0, 1, 0 }, // 0 new double[] { 0, 1, 1, 0 }, // 0 new double[] { 0, 1, 0, 0 }, // 0 new double[] { 1, 0, 0, 0 }, // 1 new double[] { 1, 0, 0, 0 }, // 1 new double[] { 1, 0, 0, 1 }, // 1 new double[] { 0, 0, 0, 1 }, // 1 new double[] { 0, 0, 0, 1 }, // 1 new double[] { 1, 1, 1, 1 }, // 2 new double[] { 1, 0, 1, 1 }, // 2 new double[] { 1, 1, 0, 1 }, // 2 new double[] { 0, 1, 1, 1 }, // 2 new double[] { 1, 1, 1, 1 }, // 2 }; int[] classes = { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, }; // First we have to convert this problem into a way that the neural // network can handle. The first step is to expand the classes into // indicator vectors, where a 1 into a position signifies that this // position indicates the class the sample belongs to. // double[][] outputs = Statistics.Tools.Expand(classes, -1, +1); // Create an activation function for the net var function = new BipolarSigmoidFunction(); // Create an activation network with the function and // 4 inputs, 5 hidden neurons and 3 possible outputs: var network = new ActivationNetwork(function, 4, 5, 3); // Randomly initialize the network new NguyenWidrow(network).Randomize(); // Teach the network using parallel Rprop: var teacher = new ParallelResilientBackpropagationLearning(network); double error = 1.0; while (error > 1e-5) error = teacher.RunEpoch(inputs, outputs); // Checks if the network has learned for (int i = 0; i < inputs.Length; i++) { double[] answer = network.Compute(inputs[i]); int expected = classes[i]; int actual; answer.Max(out actual); Assert.AreEqual(expected, actual, 0.01); } }
// Worker thread void SearchSolution() { // number of learning samples int samples = data.GetLength(0); // prepare learning data DoubleRange unit = new DoubleRange(-1, 1); double[][] input = data.GetColumn(0).Scale(fromRange: xRange, toRange: unit).ToArray(); double[][] output = data.GetColumn(1).Scale(fromRange: yRange, toRange: unit).ToArray(); // create multi-layer neural network ActivationNetwork network = new ActivationNetwork( new BipolarSigmoidFunction(sigmoidAlphaValue), 1, neuronsInFirstLayer, 1); if (useNguyenWidrow) { new NguyenWidrow(network).Randomize(); } // create teacher var teacher = new ParallelResilientBackpropagationLearning(network); // iterations int iteration = 1; // solution array double[,] solution = new double[samples, 2]; // loop while (!needToStop) { // run epoch of learning procedure double error = teacher.RunEpoch(input, output) / samples; // calculate solution for (int j = 0; j < samples; j++) { double x = input[j][0]; double y = network.Compute(new[] { x })[0]; solution[j, 0] = x.Scale(fromRange: unit, toRange: xRange); solution[j, 1] = y.Scale(fromRange: unit, toRange: yRange); } chart.UpdateDataSeries("solution", solution); // calculate error double learningError = 0.0; for (int j = 0; j < samples; j++) { double x = input[j][0]; double expected = data[j, 1]; double actual = network.Compute(new[] { x })[0]; learningError += Math.Abs(expected - actual); } // set current iteration's info SetText(currentIterationBox, iteration.ToString()); SetText(currentErrorBox, 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 static double computeError(double[][] inputs, double[][] outputs, ActivationNetwork ann) { // Compute the machine outputs int miss = 0; for (int i = 0; i < inputs.Length; i++) { var y = System.Math.Sign(ann.Compute(inputs[i])[0]); var o = outputs[i][0]; if (y != o) miss++; } return (double)miss / inputs.Length; }
// 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 var teacher = new ParallelResilientBackpropagationLearning(network); teacher.Reset(initialStep); // run at least one backpropagation epoch //teacher2.RunEpoch(input, output); // 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); }
public void RunEpochTest1() { Accord.Math.Tools.SetupGenerator(0); double[][] input = { new double[] { -1, -1 }, new double[] { -1, 1 }, new double[] { 1, -1 }, new double[] { 1, 1 } }; double[][] output = { new double[] { -1 }, new double[] { 1 }, new double[] { 1 }, new double[] { -1 } }; // Neuron.RandGenerator = new ThreadSafeRandom(0); ActivationNetwork network = new ActivationNetwork( new BipolarSigmoidFunction(2), 2, 2, 1); var teacher = new LevenbergMarquardtLearning(network, false, JacobianMethod.ByFiniteDifferences); double error = 1.0; while (error > 1e-5) error = teacher.RunEpoch(input, output); for (int i = 0; i < input.Length; i++) Assert.AreEqual(network.Compute(input[i])[0], output[i][0], 0.1); }
public void ZeroLambdaTest() { Accord.Math.Random.Generator.Seed = 0; double[,] data = null; // open selected file using (TextReader stream = new StringReader(Properties.Resources.ZeroLambda)) using (CsvReader reader = new CsvReader(stream, false)) { data = reader.ToTable().ToMatrix(System.Globalization.CultureInfo.InvariantCulture); } // number of learning samples int samples = data.GetLength(0); var ranges = data.GetRange(dimension: 0); Assert.AreEqual(2, ranges.Length); var rangeX = ranges[0]; var rangeY = ranges[1]; // data transformation factor double yFactor = 1.7 / rangeY.Length; double yMin = rangeY.Min; double xFactor = 2.0 / rangeX.Length; double xMin = 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]; input[i][0] = (data[i, 0] - xMin) * xFactor - 1.0; // set input output[i][0] = (data[i, 1] - yMin) * yFactor - 0.85; // set output } // Neuron.RandGenerator = new ThreadSafeRandom(0); Accord.Math.Random.Generator.Seed = 0; // create multi-layer neural network var network = new ActivationNetwork( new BipolarSigmoidFunction(5), 1, 12, 1); // create teacher var teacher = new LevenbergMarquardtLearning(network, true); teacher.LearningRate = 1; // iterations int iteration = 1; int iterations = 2000; // solution array double[,] solution = new double[samples, 2]; double[] networkInput = new double[1]; bool needToStop = false; double learningError = 0; // loop while (!needToStop) { Assert.AreNotEqual(0, teacher.LearningRate); // run epoch of learning procedure double error = teacher.RunEpoch(input, output) / samples; // calculate solution for (int j = 0; j < samples; j++) { networkInput[0] = (solution[j, 0] - xMin) * xFactor - 1.0; solution[j, 1] = (network.Compute(networkInput)[0] + 0.85) / yFactor + yMin; } // calculate error learningError = 0.0; for (int j = 0; j < samples; j++) { networkInput[0] = input[j][0]; learningError += Math.Abs(data[j, 1] - ((network.Compute(networkInput)[0] + 0.85) / yFactor + yMin)); } // increase current iteration iteration++; // check if we need to stop if ((iterations != 0) && (iteration > iterations)) break; } Assert.IsTrue(learningError < 0.13); }
public void RunEpochTest4() { Accord.Math.Tools.SetupGenerator(0); double[][] input = { new double[] { 0, 0 }, }; double[][] output = { new double[] { 0 }, }; // Neuron.RandGenerator = new ThreadSafeRandom(0); ActivationNetwork network = new ActivationNetwork( new BipolarSigmoidFunction(2), 2, 1); var teacher = new LevenbergMarquardtLearning(network, true, JacobianMethod.ByBackpropagation); double error = 1.0; for (int i = 0; i < 1000; i++) error = teacher.RunEpoch(input, output); for (int i = 0; i < input.Length; i++) Assert.AreEqual(network.Compute(input[i])[0], output[i][0], 0.1); }
public void RunEpochTest3() { double[,] dataset = yinyang; double[][] input = dataset.GetColumns(new[] { 0, 1 }).ToJagged(); double[][] output = dataset.GetColumn(2).ToJagged(); // Neuron.RandGenerator = new ThreadSafeRandom(0); Accord.Math.Random.Generator.Seed = 0; ActivationNetwork network = new ActivationNetwork( new BipolarSigmoidFunction(2), 2, 5, 1); var teacher = new LevenbergMarquardtLearning(network, true, JacobianMethod.ByBackpropagation); Assert.IsTrue(teacher.UseRegularization); double error = 1.0; for (int i = 0; i < 500; i++) error = teacher.RunEpoch(input, output); double[][] actual = new double[output.Length][]; for (int i = 0; i < input.Length; i++) actual[i] = network.Compute(input[i]); for (int i = 0; i < input.Length; i++) Assert.AreEqual(Math.Sign(output[i][0]), Math.Sign(actual[i][0])); }