Resilient Backpropagation learning algorithm.

This class implements the resilient backpropagation (RProp) learning algorithm. The RProp learning algorithm is one of the fastest learning algorithms for feed-forward learning networks which use only first-order information.

Sample usage (training network to calculate XOR function):

// 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( SigmoidFunction( 2 ), 2, // two inputs in the network 2, // two neurons in the first layer 1 ); // one neuron in the second layer // create teacher ResilientBackpropagationLearning teacher = new ResilientBackpropagationLearning( network ); // loop while ( !needToStop ) { // run epoch of learning procedure double error = teacher.RunEpoch( input, output ); // check error value to see if we need to stop // ... }
Inheritance: ISupervisedLearning, IDisposable
        public void CreateActivationNetworkTest()
        {
            double[][] inputs =
            {
                new double[] { 1,1,1,0,0,0 },
                new double[] { 1,0,1,0,0,0 },
                new double[] { 1,1,1,0,0,0 },
                new double[] { 0,0,1,1,1,0 },
                new double[] { 0,0,1,1,0,0 },
                new double[] { 0,0,1,1,1,0 }
            };

            double[][] outputs =
            {
                new double[] { 0 },
                new double[] { 0 },
                new double[] { 0 },
                new double[] { 1 },
                new double[] { 1 },
                new double[] { 1 },
            };

            RestrictedBoltzmannMachine network = createNetwork(inputs);

            ActivationNetwork ann = network.ToActivationNetwork(new SigmoidFunction(1), outputs: 1);

            ParallelResilientBackpropagationLearning teacher = new ParallelResilientBackpropagationLearning(ann);

            for (int i = 0; i < 100; i++)
            {
                teacher.RunEpoch(inputs, outputs);
            }

            double[] actual = new double[outputs.Length];
            for (int i = 0; i < inputs.Length; i++)
                actual[i] = ann.Compute(inputs[i])[0];

            Assert.AreEqual(0, actual[0], 1e-10);
            Assert.AreEqual(0, actual[1], 1e-10);
            Assert.AreEqual(0, actual[2], 1e-10);
            Assert.AreEqual(1, actual[3], 1e-10);
            Assert.AreEqual(1, actual[4], 1e-10);
            Assert.AreEqual(1, actual[5], 1e-10);
        }
        public void CreateActivationNetworkTest()
        {
            double[][] inputs =
            {
                new double[] { 1,1,1,0,0,0 },
                new double[] { 1,0,1,0,0,0 },
                new double[] { 1,1,1,0,0,0 },
                new double[] { 0,0,1,1,1,0 },
                new double[] { 0,0,1,1,0,0 },
                new double[] { 0,0,1,1,1,0 }
            };

            double[][] outputs =
            {
                new double[] { 0 },
                new double[] { 0 },
                new double[] { 0 },
                new double[] { 1 },
                new double[] { 1 },
                new double[] { 1 },
            };

            DeepBeliefNetwork network = createNetwork(inputs);

            ParallelResilientBackpropagationLearning teacher = new ParallelResilientBackpropagationLearning(network);

            for (int i = 0; i < 100; i++)
            {
                teacher.RunEpoch(inputs, outputs);
            }

            double[] actual = new double[outputs.Length];
            for (int i = 0; i < inputs.Length; i++)
                actual[i] = network.Compute(inputs[i])[0];

            Assert.AreEqual(0, actual[0], 1e-10);
            Assert.AreEqual(0, actual[1], 1e-10);
            Assert.AreEqual(0, actual[2], 1e-10);
            Assert.AreEqual(1, actual[3], 1e-10);
            Assert.AreEqual(1, actual[4], 1e-10);
            Assert.AreEqual(1, actual[5], 1e-10);
        }
        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));
            }
        }
Example #4
0
        static void NeuralNetworkAccompanimentTest()
        {
            // 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}
            };
            SigmoidFunction sig = new SigmoidFunction();

            Accord.Neuro.Networks.RestrictedBoltzmannMachine boltz = new Accord.Neuro.Networks.RestrictedBoltzmannMachine(200, 200);

            // create neural network
            ActivationNetwork network = new ActivationNetwork(
                new SigmoidFunction(2),
                200,
                20,
                200);
            //BackPropagationLearning teacher = new BackPropagationLearning(network);
            //LevenbergMarquardtLearning teacher = new LevenbergMarquardtLearning(network);
            Accord.Neuro.Learning.ParallelResilientBackpropagationLearning teacher = new ParallelResilientBackpropagationLearning(network);
            Accord.Neuro.Networks.DeepBeliefNetwork dpn = new Accord.Neuro.Networks.DeepBeliefNetwork(200, 20);

            // teacher.IncreaseFactor = 1.01;
            Composition c = Composition.LoadFromMIDI("test/other/ff7tifa.mid");

            MusicPlayer player = new MusicPlayer();
            //player.Play(c.Tracks[0]);

            List<double[]> inputs = new List<double[]>(); List<double[]> outputs = new List<double[]>();

            inputs.Add(GetDoublesFromNotes((c.Tracks[0].GetMainSequence() as MelodySequence).ToArray()));
            outputs.Add(GetDoublesFromNotes((c.Tracks[1].GetMainSequence() as MelodySequence).ToArray()));

            //  inputs.Add(GetDoublesFromNotes((c.Tracks[1].GetMainSequence() as MelodySequence).ToArray()));
            //    outputs.Add(GetDoublesFromNotes((c.Tracks[2].GetMainSequence() as MelodySequence).ToArray()));

            // inputs.Add(GetDoublesFromNotes((c.Tracks[0].GetMainSequence() as MelodySequence).ToArray()));
            // outputs.Add(GetDoublesFromNotes((c.Tracks[3].GetMainSequence() as MelodySequence).ToArray()));

            int its = 0;
            while (its++ < 10000)
            {

                double error = teacher.RunEpoch(inputs.ToArray(), outputs.ToArray());
                Console.WriteLine("{0}: Error - {1}", its, error);
            }

            var input_melody = (c.Tracks[0].GetMainSequence() as MelodySequence);
            var new_notes = network.Compute(GetDoublesFromNotes(input_melody.ToArray()));

            var new_mel = GetMelodyFromDoubles(new_notes);

            player.Play(new_mel);

            Console.ReadLine();
        }
Example #5
0
        // 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 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);
            }
        }
Example #7
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);

            if (useNguyenWidrow)
            {
                NguyenWidrow initializer = new NguyenWidrow(network);
                initializer.Randomize();
            }

            // create teacher
            var teacher = new ParallelResilientBackpropagationLearning(network);

            // 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
                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);
        }
Example #8
0
        // Worker thread
        void SearchSolution()
        {
            // number of learning samples
            int samples = data.GetLength(0);

            // prepare learning data
            DoubleRange unit = new DoubleRange(-1, 1);
            double[][] input = Tools.Scale(from: xRange, to: unit, x: data.GetColumn(0)).ToArray();
            double[][] output = Tools.Scale(from: yRange, to: unit, x: data.GetColumn(1)).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] = Tools.Scale(from: unit, to: xRange, x: x);
                    solution[j, 1] = Tools.Scale(from: unit, to: yRange, x: y);
                }

                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);
        }
Example #9
0
        public static void Optimize(
            FsdParser inputParser, string outputFileName,
            int[][] layers,
            double minThreshold, double maxThreshold, double thresholdStepSize,
            int minTrades,
            int iterations,
            int randomize,
            FsdParser[] validationParsers)
        {
            HashSet<int[]> layerCombinations;
            getLayerCombinations(layers, out layerCombinations);

            Network bestNetwork = null;
            double bestThreshold = double.NaN;
            double bestMinWinRate = double.NegativeInfinity;

            int cursorLeft = Console.CursorLeft;
            int cursorTop = Console.CursorTop;

            int numTotalIterations = (int)(layerCombinations.Count * ((maxThreshold - minThreshold) / thresholdStepSize + 1) * iterations);
            int numIterations = 0;

            foreach (int[] layerCombination in layerCombinations)
            {
                ActivationNetwork network = new ActivationNetwork(
                    new SigmoidFunction(),
                    inputParser.InputVectors[0].Length,
                    layerCombination);

                network.Randomize();

                ParallelResilientBackpropagationLearning teacher = new ParallelResilientBackpropagationLearning(network);

                for (double threshold = minThreshold; threshold <= maxThreshold; threshold += thresholdStepSize)
                {
                    for (int iteration = 1; iteration <= iterations; iteration++)
                    {
                        numIterations++;

                        Console.CursorLeft = cursorLeft;
                        Console.CursorTop = cursorTop;

                        Console.Write(new string(' ', Console.BufferWidth * 10));

                        Console.CursorLeft = cursorLeft;
                        Console.CursorTop = cursorTop;

                        Console.Write("layerCombination[]: { ");

                        for (int layerComponentIdx = 0; layerComponentIdx < layerCombination.Length; layerComponentIdx++)
                        {
                            Console.Write(layerCombination[layerComponentIdx]);
                            if (layerComponentIdx < layerCombination.Length - 1)
                                Console.Write(", ");
                        }

                        Console.WriteLine(" }");

                        Console.WriteLine("threshold: {0:0.00}", threshold);
                        Console.WriteLine("iteration: {0}", iteration);
                        Console.WriteLine("bestMinWinRate: {0:0.00}%", bestMinWinRate);
                        Console.WriteLine("");
                        Console.WriteLine("Progress: {0:0.00}%", (double)numIterations / numTotalIterations * 100.0);

                        if (randomize > 0 && (iteration - 1) % randomize == 0)
                            network.Randomize();

                        teacher.RunEpoch(inputParser.InputVectors, inputParser.OutputVectors);

                        bool validData = true;
                        double minWinRate = double.PositiveInfinity;

                        foreach (FsdParser validationParser in validationParsers)
                        {
                            int numTradesWon, numTradesLost;
                            double tradeWinRate;

                            getStatistics(network, validationParser, threshold, out numTradesWon, out numTradesLost, out tradeWinRate);

                            if (numTradesWon + numTradesLost < minTrades)
                            {
                                validData = false;
                                break;
                            }

                            minWinRate = Math.Min(minWinRate, tradeWinRate);

                            if (minWinRate < bestMinWinRate)
                            {
                                validData = false;
                                break;
                            }
                        }

                        if (validData)
                        {
                            bestNetwork = network;
                            bestThreshold = threshold;
                            bestMinWinRate = minWinRate;

                            network.Save(outputFileName);

                            // Konfigurationsinformationen speichern
                            string configuration = "";
                            configuration += "layerCombination[]: { ";

                            for (int layerComponentIdx = 0; layerComponentIdx < layerCombination.Length; layerComponentIdx++)
                            {
                                configuration += layerCombination[layerComponentIdx];
                                if (layerComponentIdx < layerCombination.Length - 1)
                                    configuration += ", ";
                            }

                            configuration += " }\r\n";

                            configuration += string.Format("threshold: {0:0.00}\r\n", threshold);
                            configuration += string.Format("iteration: {0}\r\n", iteration);
                            configuration += string.Format("bestMinWinRate: {0:0.00}%\r\n", bestMinWinRate);

                            File.WriteAllText(outputFileName + ".txt", configuration);
                        }
                    }
                }
            }
        }
Example #10
0
		// 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
			{
				// bipolar 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 neural network
			ActivationNetwork	network = new ActivationNetwork(
				( sigmoidType == 0 ) ? 
					(IActivationFunction) new SigmoidFunction( sigmoidAlphaValue ) :
					(IActivationFunction) new BipolarSigmoidFunction( sigmoidAlphaValue ),
				2, 2, 1 );

			// create teacher
            var teacher = new ParallelResilientBackpropagationLearning(network);


            // set learning rate and momentum
            teacher.Reset(initialStep);


			// iterations
			int iteration = 0;

			// 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
				ArrayList errorsList = new ArrayList( );

				// 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
                    SetText( currentIterationBox, iteration.ToString( ) );
                    SetText( currentErrorBox, 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] = (double) errorsList[i];
				}

				errorChart.RangeX = new Range( 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 );
		}
Example #11
0
        static void main(String[] args)
        {
            int[] electrode_list;
            int[] power_bands;
            if (args == null || args.Length == 0)
            {
                electrode_list = new int[19];
                for (int i = 0; i < 19; i++)
                {
                    electrode_list[i] = i;
                }
                power_bands = new int[] { 0, 1, 2, 3, 4, 5 };
            }
            else if (args.Length == 1)
            {
                String ls = args[0];
                electrode_list = ls.Split(',').Select(e => int.Parse(e)).ToArray();
                power_bands    = new int[] { 0, 1, 2, 3, 4, 5 };
            }
            else
            {
                String ls = args[0];
                electrode_list = ls.Split(',').Select(e => int.Parse(e)).ToArray();
                ls             = args[1];
                power_bands    = ls.Split(',').Select(e => int.Parse(e)).ToArray();
            }

            /* power bands:
             * 0 = delta (2 - 4)
             * 1 = theta (5 - 7)
             * 2 = alpha (8 - 15)
             * 3 = beta (16 - 31)
             * 4 = gamma (32 - 49)
             * 5 = gamma (50 - 120)
             */

            load_inputs(electrode_list, power_bands);


            //Console.WriteLine("starting training;");
            // var nb = new NaiveBayesLearning<NormalDistribution>(); // naive bayes init
            // var classifier = nb.Learn(classifier_inputs, classifier_tags); // naive bayes

            shuffle_inputs(new Random());

            double[][] outputs = new double[classifier_tags.Length][];
            for (int i = 0; i < classifier_tags.Length; i++)
            {
                outputs[i] = new double[] { (double)classifier_tags[i] };
            }

            // LinearDiscriminantAnalysis lda = new LinearDiscriminantAnalysis(); // lda init
            //  var classifier = lda.Learn(classifier_inputs, classifier_tags); // lda

            // neural net

            /*
             * var network = new ActivationNetwork(new BipolarSigmoidFunction(), inputsCount: classifier_inputs[0].Length, neuronsCount: new[] { 30, 30, 1 });
             * var teacher = new Accord.Neuro.Learning.ParallelResilientBackpropagationLearning(network);
             * //var teacher = new Accord.Neuro.Learning.LevenbergMarquardtLearning(network);
             * GaussianWeights initializer = new GaussianWeights(network);
             * initializer.Randomize();
             *
             *
             *
             *
             * var y = outputs;
             */

            // Iterate until stop criteria is met
            //double error = double.PositiveInfinity;

            /*
             *
             * for (int i = 0; i <25 ; i++)
             * {
             *  error = teacher.RunEpoch(classifier_inputs, y);
             *
             * }
             */
            //Console.WriteLine("done training;");

            //int[] answers = classifier_inputs.Apply(network.Compute).GetColumn(0).Apply(System.Math.Sign);

            // get input set score

            /*
             * int correct = 0;
             * int wrong = 0;
             * int total = classifier_tags.Length;
             */
            /*
             * for (var i = 0; i < classifier_inputs.Length; i++)
             * {
             *  int cls = classifier.Decide(classifier_inputs[i]);
             *  Console.Write("expected: ");
             *  Console.Write(classifier_tags[i]);
             *  Console.Write(" : received: ");
             *  Console.Write(Convert.ToInt32(cls));
             *  Console.WriteLine("");
             *
             *  if (cls == classifier_tags[i]) correct++;
             *  else wrong++;
             * }
             */
            /*
             * for (int i = 0; i < classifier_tags.Length; i++)
             * {
             *
             *  var ans = (int)Math.Round(network.Compute(classifier_inputs[i])[0]);
             *
             *  if (ans == classifier_tags[i])
             *  {
             *      correct++;
             *  }
             *  else wrong++;
             * }
             * double acc = (double)correct / (double)total;  // training set accuracy
             */

            // cross validation code starts here

            var             cross_validation_accuracies = new List <double>();
            List <double[]> arr    = classifier_inputs.ToList();
            List <double[]> outarr = outputs.ToList();


            int folds     = 10;
            int fold_size = classifier_inputs.Length / folds;

            int cross_correct_total = 0;
            int cross_wrong_total   = 0;
            int cross_total_total   = 0;

            for (int i = 0; i < folds; i++)
            {
                var training_set    = new List <double[]>();
                var training_tags   = new List <double[]>();
                var validation_set  = new List <double[]>();
                var validation_tags = new List <double[]>();

                training_set.AddRange(arr.Take(i * fold_size));
                training_tags.AddRange(outarr.Take(i * fold_size));

                validation_set.AddRange(arr.Skip(i * fold_size).Take(fold_size));
                validation_tags.AddRange(outarr.Skip(i * fold_size).Take(fold_size));

                training_set.AddRange(arr.Skip(i * fold_size + fold_size));
                training_tags.AddRange(outarr.Skip(i * fold_size + fold_size));

                var training_array      = training_set.ToArray();
                var training_array_tags = training_tags.ToArray();

                var validation_array      = validation_set.ToArray();
                var validation_array_tags = validation_tags.ToArray();

                var cross_network = new ActivationNetwork(new BipolarSigmoidFunction(), inputsCount: training_array[0].Length, neuronsCount: new[] { 30, 1 });
                var cross_teacher = new Accord.Neuro.Learning.ParallelResilientBackpropagationLearning(cross_network);
                //var teacher = new Accord.Neuro.Learning.LevenbergMarquardtLearning(network);
                GaussianWeights cross_initializer = new GaussianWeights(cross_network);
                cross_initializer.Randomize();

                var z = training_array_tags;

                double cross_error = double.PositiveInfinity;
                // training network
                for (int t = 0; t < 25; t++)
                {
                    cross_error = cross_teacher.RunEpoch(training_array, z);
                }
                // network trained - calculate accuracy.


                int cross_correct = 0;
                int cross_wrong   = 0;
                int cross_total   = validation_array_tags.Length;

                for (int t = 0; t < validation_array_tags.Length; t++)
                {
                    var ans = (int)Math.Round(cross_network.Compute(validation_array[t])[0]);
                    if (ans == validation_array_tags[t][0])
                    {
                        cross_correct++;
                    }
                    else
                    {
                        cross_wrong++;
                    }
                }

                cross_correct_total += cross_correct;
                cross_wrong_total   += cross_wrong;
                cross_total_total   += cross_total;
                double cross_acc = (double)cross_correct / (double)cross_total;

                cross_validation_accuracies.Add(cross_acc);
            }

            var acc = cross_validation_accuracies.Average();

            // write cross_validation_accuracies to a file.
            Console.WriteLine(cross_validation_accuracies.ToString());



            Dictionary <string, object> collection = new Dictionary <string, object>()
            {
                { "Accuracy", acc },
                { "Fold_Accuracies", cross_validation_accuracies.ToArray() }
            };

            JObject Result = new JObject(
                new JProperty("metrics",
                              JObject.FromObject(collection)
                              )
                );

            Console.WriteLine("BEGIN METRICS");
            Console.WriteLine(Result.ToString());
            Console.WriteLine("END METRICS");
            Console.ReadKey();
        }
Example #12
0
        public List<double> Treino(string path)
        {
            double learningUtheil = 0.0;
            double learningError = 0.0;

            double validationError = 0.0;
            double validationUtheil = 0.0;
            double validationMAPE = 0.0;

            //setando o erro de comparação da validação em um valor extremo
            menoresErros[2] = 1000;

            //variável que conta a quantidade de iterações
            int iteration;

            //variável auxiliar que define quando o processo de treino acaba
            bool needToStop = false;

            //fator de normalização
            fatorNormal = 2.0 / (Serie.Max - Serie.Min);

            //lista contendo todos os ids de 1 a 52
            List<int> ids = Serie.Ids;
            //variavel que possuirá o id binário
            int[] id = new int[6];

            // número de amostras para a aprendizagem
            int samples = dadosTreino.Count - windowSize - predictionSize;

            // preparação dos dados para a aprendizagem
            double[][] input = new double[samples][];//vetor de entrada
            double[][] output = new double[samples][];//vetor de saída

            for (int i = 0; i < samples; i++)
            {
                //define o tamanho da entrada
                input[i] = new double[windowSize + predictionSize * 6];
                //define o tamanho da saída
                output[i] = new double[predictionSize];

                // configura a entrada com os dados formatados
                for (int j = 0; j < windowSize + predictionSize * 6; j++)
                {
                    if (j < windowSize)
                    {
                        input[i][j] = (dadosTreino[i + j] - Serie.Min) * fatorNormal - 1.0;
                    }
                    else
                    {
                        if (j == windowSize)
                            id = CUtil.ConversaoBinario(ids[i + windowSize]);

                        input[i][j] = id[j - windowSize];
                    }
                }// fim do for interno

                // configura os dados de saída com os dados transformados
                for (int k = 0; k < predictionSize; k++)
                {
                    output[i][k] = (dadosTreino[i + k + windowSize] - Serie.Min) * fatorNormal - 1;
                }
            }//fim do for externo

            //cria o "Professor" da rede neural
            ParallelResilientBackpropagationLearning teacher = new ParallelResilientBackpropagationLearning(network);

            //Variável para contar o número de iterações
            iteration = 1;

            //vetor que armazena a solução encontrada pela rede neural
            int solutionSize = dadosTreino.Count - windowSize;
            double[] solution = new double[solutionSize];

            //Vetor auxiliar que seta as entrada a serem computadas pela rede neural
            double[] networkInput = new double[windowSize + predictionSize * 6];

            // loop que efetua as iterações
            while (!needToStop)
            {
                learningError = 0.0;
                learningUtheil = 0.0;

                //roda uma iteração do processo de aprendizagem retornando o erro obtido
                double error = teacher.RunEpoch(input, output) / samples;

                //variaveis auxiliares para calculo do utheil
                double somaY = 0.0;
                double somaF = 0.0;

                //variavel auxiliar para calcular os erros
                int amostra = 0;

                //variavel auxiliar para o id binário
                int contador = 0;

                // computa as saídas através de toda a lista de dados, armazena os valores de saída da rede em solution
                for (int i = 0, n = dadosTreino.Count - windowSize - predictionSize + 1; i < n; i++)
                {
                    int a = windowSize;
                    contador = 0;
                    // seta os valores da atual janela de previsão como entrada da rede neural
                    for (int j = 0; j < windowSize + predictionSize; j++)
                    {
                        if (j < windowSize)
                        {
                            //entrada tem de ser formatada
                            networkInput[j] = (dadosTreino[i + j] - Serie.Min) * fatorNormal - 1.0;
                        }
                        else
                        {
                            id = CUtil.ConversaoBinario(ids[i + a]);
                            a++;

                            for (int c = 0; c < 6; c++)
                            {
                                networkInput[windowSize + contador] = id[c];
                                contador++;
                            }
                        }//fim do else
                    }//fim do for interno

                    //computa a saída da rede e armazena o dado solution diferenca
                    for (int k = 0; k < network.Compute(networkInput).Length; k++)
                    {
                        double diferenca = (network.Compute(networkInput)[k] + 1.0) / fatorNormal + Serie.Min;
                        if ((i + k) < solutionSize) solution[i + k] = (diferenca) + Serie.Dados[windowSize + i, 1];
                    }

                    //calcula o erro de aprendizagem
                    amostra++;

                    //variaveis auxiliares do u theil
                    somaY += ((Serie.Dados[windowSize + i, 1]) * (Serie.Dados[windowSize + i, 1]));
                    somaF += ((solution[i] * solution[i]));

                    learningError += ((solution[i] - Serie.Dados[windowSize + i + 1, 1]) * (solution[i] - Serie.Dados[windowSize + i, 1]));

                }//fim do for externo

                learningError = (learningError) / amostra;
                somaF = somaF / amostra;
                somaY = somaY / amostra;

                learningUtheil = Math.Sqrt(learningError) / (Math.Sqrt(somaY) + Math.Sqrt(somaF));

                // validação
                if (iteration >= 30)
                    Validacao();

                // incrementa a iteração atual
                iteration++;

                // confere se precisamos ou não parar, fator de parada é o número de iterações
                if (iteration > 400)
                    break;
            }//fim do while

            validationError = menoresErros[0];
            validationMAPE = menoresErros[1];
            validationUtheil = menoresErros[2];

            return solution.ToList();
        }