Perceptron learning algorithm.

This learning algorithm is used to train one layer neural network of Activation Neurons with the Threshold activation function.

See information about Perceptron and its learning algorithm.

Inheritance: ISupervisedLearning
        public override ConfusionMatrix Execute()
        {
            //Create an network with one layer and one neuron in that layer
            var network = new ActivationNetwork(new ThresholdFunction(), 3, 1);

            //Bind the reference of the neuron
            var neuron = network.Layers[0].Neurons[0] as ActivationNeuron;

            //Create the Perceptron learning algorithm
            //Library perceptron implements a single layer linear classifier
            var teacher = new PerceptronLearning(network);

            teacher.LearningRate = 0.1;

            //Enrich the dimensions of the vectors, padding 1 to the end
            var richTraining = AlgorithmHelpers.PaddDimension(trainingSet);
            var richTesting = AlgorithmHelpers.PaddDimension(testSet);

            //Training the network until the error is small enough
            //or 500 hundred iterations have been computed
            int epochs = 0;
            while (true)
            {
                double error = teacher.RunEpoch(richTraining, trainingOutput);/// trainingSet.Length;
                ++epochs;
                if (error < 0.025 * trainingSet.Length || epochs == 500) break;
            }

            var predicted = richTesting
                   .Select(x => neuron.Compute(x))
                   .Select(x => Convert.ToInt32(x))
                   .ToArray();


            //Create a confusion matrix with the calculated parameters
            ConfusionMatrix cmatrix = new ConfusionMatrix(predicted, expected, POSITIVE, NEGATIVE);

            OnAlgorithmEnded(Enumerable.Repeat(neuron, 1), cmatrix);
            return cmatrix;
        }
示例#2
0
        static void Main()
        {
            weights = new double[3];
            network = new ActivationNetwork(new SignumActivationFunction(), 2, 1);
            weights[0] = ((ActivationNeuron)network.Layers[0].Neurons[0]).Threshold = 0;
            weights[1] = network.Layers[0].Neurons[0].Weights[0] = 0.9;
            weights[2] = network.Layers[0].Neurons[0].Weights[1] = 0.2;

            learning = new PerceptronLearning(network);
            learning.LearningRate = 0.005;

            form = new MyForm() { WindowState = FormWindowState.Maximized };
            form.Paint += (s, a) => Redraw(a.Graphics);

            var timer = new System.Windows.Forms.Timer();
            timer.Interval = 10;
            timer.Tick += NextSample;
            timer.Start();
            Application.Run(form);
        }
示例#3
0
		// Worker thread
		void SearchSolution( )
		{
			// prepare learning data
			double[][] input = new double[samples][];
			double[][] output = new double[samples][];

			for ( int i = 0; i < samples; i++ )
			{
				input[i] = new double[variables];
				output[i] = new double[1];

				// copy input
				for ( int j = 0; j < variables; j++ )
					input[i][j] = data[i, j];
				// copy output
				output[i][0] = classes[i];
			}

			// create perceptron
			ActivationNetwork	network = new ActivationNetwork( new ThresholdFunction( ), variables, 1 );
			ActivationNeuron	neuron = network[0][0];
			// create teacher
			PerceptronLearning teacher = new PerceptronLearning( network );
			// set learning rate
			teacher.LearningRate = learningRate;

			// iterations
			int iteration = 1;

			// statistic files
			StreamWriter errorsFile = null;
			StreamWriter weightsFile = null;

			try
			{
				// check if we need to save statistics to files
				if ( saveStatisticsToFiles )
				{
					// open files
					errorsFile	= File.CreateText( "errors.csv" );
					weightsFile	= File.CreateText( "weights.csv" );
				}

				// erros list
				ArrayList errorsList = new ArrayList( );

				// loop
				while ( !needToStop )
				{
					// save current weights
					if ( weightsFile != null )
					{
						for ( int i = 0; i < variables; i++ )
						{
							weightsFile.Write( neuron[i] + "," );
						}
						weightsFile.WriteLine( neuron.Threshold );
					}

					// run epoch of learning procedure
					double error = teacher.RunEpoch( input, output );
					errorsList.Add( error );

					// show current iteration
                    SetText( iterationsBox, iteration.ToString( ) );

					// save current error
					if ( errorsFile != null )
					{
						errorsFile.WriteLine( error );
					}				

					// show classifier in the case of 2 dimensional data
					if ( ( neuron.InputsCount == 2 ) && ( neuron[1] != 0 ) )
					{
						double k = - neuron[0] / neuron[1];
						double b = - neuron.Threshold / neuron[1];

						double[,] classifier = new double[2, 2] {
							{ chart.RangeX.Min, chart.RangeX.Min * k + b },
							{ chart.RangeX.Max, chart.RangeX.Max * k + b }
																};
						// update chart
						chart.UpdateDataSeries( "classifier", classifier );
					}

					// stop if no error
					if ( error == 0 )
						break;

					iteration++;
				}

				// show perceptron's weights
                ListViewItem item = null;
                
                ClearList( weightsList );
				for ( int i = 0; i < variables; i++ )
				{
                    item = AddListItem( weightsList, string.Format( "Weight {0}", i + 1 ) );
                    AddListSubitem( item, neuron[i].ToString( "F6" ) );
				}
                item = AddListItem( weightsList, "Threshold" );
                AddListSubitem( item, neuron.Threshold.ToString( "F6" ) );

				// 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 DoubleRange( 0, errorsList.Count - 1 );
				errorChart.RangeY = new DoubleRange( 0, samples );
				errorChart.UpdateDataSeries( "error", errors );
			}
			catch ( IOException )
			{
				MessageBox.Show( "Failed writing file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
			}
			finally
			{
				// close files
				if ( errorsFile != null )
					errorsFile.Close( );
				if ( weightsFile != null )
					weightsFile.Close( );
			}

			// enable settings controls
			EnableControls( true );
		}
示例#4
0
        // Worker thread
        void SearchSolution( )
        {
            // prepare learning data
            double[][] input = new double[samples][];
            double[][] output = new double[samples][];

            for ( int i = 0; i < samples; i++ )
            {
                input[i] = new double[2];
                output[i] = new double[classesCount];

                // set input
                input[i][0] = data[i, 0];
                input[i][1] = data[i, 1];
                // set output
                output[i][classes[i]] = 1;
            }

            // create perceptron
            ActivationNetwork	network = new ActivationNetwork( new ThresholdFunction( ), 2, classesCount );
            ActivationLayer		layer = network[0];
            // create teacher
            PerceptronLearning teacher = new PerceptronLearning( network );
            // set learning rate
            teacher.LearningRate = learningRate;

            // iterations
            int iteration = 1;

            // statistic files
            StreamWriter errorsFile = null;
            StreamWriter weightsFile = null;

            try
            {
                // check if we need to save statistics to files
                if ( saveStatisticsToFiles )
                {
                    // open files
                    errorsFile	= File.CreateText( "errors.csv" );
                    weightsFile	= File.CreateText( "weights.csv" );
                }

                // erros list
                ArrayList errorsList = new ArrayList( );

                // loop
                while ( !needToStop )
                {
                    // save current weights
                    if ( weightsFile != null )
                    {
                        for ( int i = 0; i < classesCount; i++ )
                        {
                            weightsFile.Write( "neuron" + i + ";" );
                            weightsFile.Write( layer[i][0] + ";" );
                            weightsFile.Write( layer[i][1] + ";" );
                            weightsFile.WriteLine( layer[i].Threshold );
                        }
                    }

                    // 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
                    iterationsBox.Text = iteration.ToString( );

                    // stop if no error
                    if ( error == 0 )
                        break;

                    // show classifiers
                    for ( int j = 0; j < classesCount; j++ )
                    {
                        double k = - layer[j][0] / layer[j][1];
                        double b = - layer[j].Threshold / layer[j][1];

                        double[,] classifier = new double[2, 2] {
                            { chart.RangeX.Min, chart.RangeX.Min * k + b },
                            { chart.RangeX.Max, chart.RangeX.Max * k + b }
                                                                };

                        // update chart
                        chart.UpdateDataSeries( string.Format( "classifier" + j ), classifier );
                    }

                    iteration++;
                }

                // show perceptron's weights
                weightsList.Items.Clear( );
                for ( int i = 0; i < classesCount; i++ )
                {
                    string neuronName = string.Format( "Neuron {0}", i + 1 );

                    // weight 0
                    ListViewItem item = weightsList.Items.Add( neuronName );
                    item.SubItems.Add( "Weight 1" );
                    item.SubItems.Add( layer[i][0].ToString( "F6" ) );
                    // weight 1
                    item = weightsList.Items.Add( neuronName );
                    item.SubItems.Add( "Weight 2" );
                    item.SubItems.Add( layer[i][1].ToString( "F6" ) );
                    // threshold
                    item = weightsList.Items.Add( neuronName );
                    item.SubItems.Add( "Threshold" );
                    item.SubItems.Add( layer[i].Threshold.ToString( "F6" ) );
                }

                // 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 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( );
                if ( weightsFile != null )
                    weightsFile.Close( );
            }

            // enable settings controls
            EnableControls( true );
        }
示例#5
0
        public void LearnDemo()
        {
            ActivationNetwork network = new ActivationNetwork(new ThresholdFunction(), 2, 1);//Constants.StoneCount

            PerceptronLearning teacher = new PerceptronLearning(network);

            double[][] input = new double[4][];
            double[][] output = new double[4][];

            input[0] = new double[] { 0, 0 };
            output[0] = new double[] { 0 };
            input[1] = new double[] { 0, 1 };
            output[1] = new double[] { 0 };
            input[2] = new double[] { 1, 0 };
            output[2] = new double[] { 0 };
            input[3] = new double[] { 1, 1 };
            output[3] = new double[] { 1 };

            double error = 1.0;

            while (error > 0.001) {
                error = teacher.RunEpoch(input, output);
            }

            var k = network.Compute(new double[] { 0.9, 0.7 });
            var o = network.Output;

            network.Save("a.txt");
        }
示例#6
0
        void Proc1()
        {
            int learnIterations = 100;
            double desiredError = 0;

            Model model = getModelAnd();
            var activationFunc1 = new ThresholdFunction();
            var activationFunc2 = new SigmoidFunction(1);
            IActivationFunction activationFunc = activationFunc2;
            int inputsCount = model.InputsCount;
            int neuronsCount = 1;

            ActivationNetwork net = new ActivationNetwork(activationFunc, inputsCount, neuronsCount);
            PerceptronLearning lerning = new PerceptronLearning(net);
            ErrorCalculator errorCalculator = new ErrorCalculator();

            Process(learnIterations, desiredError, net, lerning,
                model.input, model.desiredOutput, errorCalculator);

            Console.ReadLine();
        }
示例#7
0
        //The Single Layer Perceptron classifier algorithm
        //It uses an Activation Network library object, with one layer and one neuron to the layer
        //To train the network it uses a PerceptronLearning library object
        private ConfusionMatrix RunPerceptron(Double[][] trainingSet, Double[][] trainingOutput, Double[][] testSet, int[] expected)
        {
            //Create an network with one layer and one neuron in that layer
            ActivationNetwork network = new ActivationNetwork(new ThresholdFunction(), 3, 1);

            //Bind the reference of the neuron
            ActivationNeuron neuron = network.Layers[0].Neurons[0] as ActivationNeuron;

            //Create the Perceptron learning algorithm
            //Library perceptron implements a single layer linear classifier
            PerceptronLearning teacher = new PerceptronLearning(network);

            teacher.LearningRate = 0.1;

            //Enrich the dimensions of the vectors, padding 1 to the end
            var richTraining = UtilityProvider.PaddDimension(trainingSet);
            var richTesting = UtilityProvider.PaddDimension(testSet);

            //Training the network until the error is small enough
            //or 500 hundred iterations have been computed
            int epochs = 0;
            while (true)
            {
                double error = teacher.RunEpoch(richTraining, trainingOutput);/// trainingSet.Length;
                ++epochs;
                if (error < 0.025 * trainingSet.Length || epochs == 500) break;
                // Console.Write("Iter: " + epochs + " " + error + "\n");
            }

            var predicted = richTesting
                   .Select(x => neuron.Compute(x))
                   .Select(x => Convert.ToInt32(x))
                   .ToArray();

            // Calculate the coordinates of the classifier
            double k = (neuron.Weights[1] != 0) ? (-neuron.Weights[0] / neuron.Weights[1]) : 0;
            double b = (neuron.Weights[1] != 0) ? (-((ActivationNeuron)neuron).Threshold / neuron.Weights[1]) : 0;

            // Create the line and feed it to the data series
            double[,] classifier = new double[2, 2]{
               { perceChart.RangeX.Min, perceChart.RangeX.Min * k + b},
               { perceChart.RangeX.Max, perceChart.RangeX.Max * k + b}
              };

            //For test, assume 1 as positive and 0 as negative
            int positive = 0;
            int negative = 1;

            //Create a confusion matrix with the calculated parameters
            ConfusionMatrix cmatrix = new ConfusionMatrix(predicted, expected, positive, negative);

            //Update the most accurate classification
            if (MostAccuratePerceptron == null || cmatrix.Accuracy > MostAccuratePerceptron.Item2.Accuracy)
            {
                MostAccuratePerceptron = Tuple.Create(classifier, cmatrix);
            }

            return cmatrix;
        }