예제 #1
0
        static void Main(string[] args)
        {
            double[][] inputs = { new double[] { 0, 0 }, new double[] { 0, 1 }, new double[] { 1, 0 }, new double[] { 1, 1 } };
            double[] targets = { 0, 1, 1, 1 };
            double error = 1;
            int epoch = 0;

            ActivationFunction function = new SigmoidFunction();
            Neuron neuron = new Neuron(2, function);
            LearningStrategy learning = new DeltaRuleLearning(neuron, 0.75);

            while (error > 0.01)
            {
                error = learning.RunEpoch(inputs, targets);
                epoch++;
                Console.WriteLine("Iteration {0} error: {1}", epoch, error);
            }
            Console.WriteLine("Training complete after {0} epochs using the Delta Rule learning regime.", epoch);
            Console.WriteLine("Testing");
            neuron.Update(new double[] { 0, 0 });
            Console.WriteLine("{0}", neuron.Output);
            neuron.Update(new double[] { 0, 1 });
            Console.WriteLine("{0}", neuron.Output);
            neuron.Update(new double[] { 1, 0 });
            Console.WriteLine("{0}", neuron.Output);
            neuron.Update(new double[] { 1, 1 });
            Console.WriteLine("{0}", neuron.Output);
        }
예제 #2
0
        /// <summary>
        /// Initializes the member network and the member teacher.
        /// </summary>
        /// <param name="network"></param>
        /// <param name="teacher"></param>
        private void SetUpNetwork(ActivationNetwork network, NeuralNetworkInfo info)
        {
            string teacher = info.Teacher;

            m_Network = network;

            if (teacher == "BackPropagation")
            {
                BackPropagationLearning learner = new BackPropagationLearning(m_Network);
                learner.LearningRate = info.LearningRate;
                learner.Momentum     = info.Momentum;
                m_Teacher            = learner;
            }
            else if (teacher == "Perceptron")
            {
                PerceptronLearning learner = new PerceptronLearning(m_Network);
                learner.LearningRate = info.LearningRate;
                m_Teacher            = learner;
            }
            else if (teacher == "DeltaRule")
            {
                DeltaRuleLearning learner = new DeltaRuleLearning(m_Network);
                learner.LearningRate = info.LearningRate;
                m_Teacher            = learner;
            }
            else
            {
                BackPropagationLearning learner = new BackPropagationLearning(m_Network);
                learner.LearningRate = info.LearningRate;
                learner.Momentum     = info.Momentum;
                m_Teacher            = learner;
            }
        }
        protected override ISupervisedLearning CreateTeacher()
        {
            var teacher = new DeltaRuleLearning(_network)
            {
                LearningRate = _learningRate,
            };

            return(teacher);
        }
예제 #4
0
        // Worker thread
        void SearchSolution()
        {
            var reducedNetwork = ((this.classesCount == 2) && (this.useOneNeuronForTwoClasses));

            // prepare learning data
            var input  = new double[this.samples][];
            var output = new double[this.samples][];

            for (var i = 0; i < this.samples; i++)
            {
                input[i]  = new double[this.variables];
                output[i] = new double[this.neuronsCount];

                // set input
                for (var j = 0; j < this.variables; j++)
                {
                    input[i][j] = this.data[i, j];
                }
                // set output
                if (reducedNetwork)
                {
                    output[i][0] = this.classes[i];
                }
                else
                {
                    output[i][this.classes[i]] = 1;
                }
            }

            // create perceptron
            var network = new ActivationNetwork(
                new SigmoidFunction(this.sigmoidAlphaValue), this.variables, this.neuronsCount);
            var layer = network[0];
            // create teacher
            var teacher = new DeltaRuleLearning(network);

            // set learning rate
            teacher.LearningRate = this.learningRate;

            // iterations
            var iteration = 1;

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

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

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

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

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

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

                    // show current iteration & error
                    this.currentIterationBox.Text = iteration.ToString();
                    this.currentErrorBox.Text     = error.ToString();
                    iteration++;

                    // check if we need to stop
                    if ((this.useErrorLimit) && (error <= this.learningErrorLimit))
                    {
                        break;
                    }
                    if ((!this.useErrorLimit) && (this.iterationLimit != 0) && (iteration > this.iterationLimit))
                    {
                        break;
                    }
                }

                // show perceptron's weights
                this.weightsList.Items.Clear();
                for (var i = 0; i < this.neuronsCount; i++)
                {
                    var          neuronName = string.Format("Neuron {0}", i + 1);
                    ListViewItem item       = null;

                    // add all weights
                    for (var j = 0; j < this.variables; j++)
                    {
                        item = this.weightsList.Items.Add(neuronName);
                        item.SubItems.Add(string.Format("Weight {0}", j + 1));
                        item.SubItems.Add(layer[i][0].ToString("F6"));
                    }
                    // threshold
                    item = this.weightsList.Items.Add(neuronName);
                    item.SubItems.Add("Threshold");
                    item.SubItems.Add(layer[i].Threshold.ToString("F6"));
                }

                // show error's dynamics
                var 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];
                }

                this.errorChart.RangeX = new DoubleRange(0, errorsList.Count - 1);
                this.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
        // Worker thread
        void SearchSolution()
        {
            bool reducedNetwork = ((classesCount == 2) && (useOneNeuronForTwoClasses));

            // 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[neuronsCount];

                // set input
                for (int j = 0; j < variables; j++)
                {
                    input[i][j] = data[i, j];
                }
                // set output
                if (reducedNetwork)
                {
                    output[i][0] = classes[i];
                }
                else
                {
                    output[i][classes[i]] = 1;
                }
            }

            // create perceptron
            ActivationNetwork network = new ActivationNetwork(
                new SigmoidFunction(sigmoidAlphaValue), variables, neuronsCount);
            ActivationLayer layer = network.Layers[0] as ActivationLayer;
            // create teacher
            DeltaRuleLearning teacher = new DeltaRuleLearning(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 < neuronsCount; i++)
                        {
                            weightsFile.Write("neuron" + i + ",");
                            for (int j = 0; j < variables; j++)
                            {
                                weightsFile.Write(layer.Neurons[i].Weights[j] + ",");
                            }
                            weightsFile.WriteLine(((ActivationNeuron)layer.Neurons[i]).Threshold);
                        }
                    }

                    // run epoch of learning procedure
                    double error = teacher.RunEpoch(input, output) / samples;
                    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 ((useErrorLimit) && (error <= learningErrorLimit))
                    {
                        break;
                    }
                    if ((!useErrorLimit) && (iterationLimit != 0) && (iteration > iterationLimit))
                    {
                        break;
                    }
                }

                // show perceptron's weights
                ClearList(weightsList);
                for (int i = 0; i < neuronsCount; i++)
                {
                    string       neuronName = string.Format("Neuron {0}", i + 1);
                    ListViewItem item       = null;

                    // add all weights
                    for (int j = 0; j < variables; j++)
                    {
                        item = AddListItem(weightsList, neuronName);
                        AddListSubitem(item, string.Format("Weight {0}", j + 1));
                        AddListSubitem(item, layer.Neurons[i].Weights[0].ToString("F6"));
                    }
                    // threshold
                    item = AddListItem(weightsList, neuronName);
                    AddListSubitem(item, "Threshold");
                    AddListSubitem(item, ((ActivationNeuron)layer.Neurons[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 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();
                }
                if (weightsFile != null)
                {
                    weightsFile.Close();
                }
            }

            // enable settings controls
            EnableControls(true);
        }
예제 #6
0
        public BackPropogation()
        {
            InitializeComponent();

            activation_nework = new ActivationNetwork(new BipolarSigmoidFunction(), 50, 50, 10, 1);

            watch1 = new Stopwatch();

            watch2 = new Stopwatch();

            watch3 = new Stopwatch();

            backgroundWorkerTrainer.Disposed                  += backgroundWorkerTrainer_Disposed;
            backgroundWorkerTrainer.DoWork                    += backgroundWorkerTrainer_DoWork;
            backgroundWorkerTrainer.ProgressChanged           += backgroundWorkerTrainer_ProgressChanged;
            backgroundWorkerTrainer.WorkerSupportsCancellation = true;
            backgroundWorkerTrainer.WorkerReportsProgress      = true;
            saveFileDialog1.Filter           = "feed forward network files (*.ffn)|*.ffn";
            saveFileDialog1.Title            = "Save neural networkfile";
            saveFileDialog1.InitialDirectory = null;
            saveFileDialog1.FileName         = null;

            openFileDialog1.Filter           = "feed forward network files (*.ffn)|*.ffn";
            openFileDialog1.Title            = "Load neural network file";
            openFileDialog1.InitialDirectory = null;
            openFileDialog1.FileName         = null;

            backgroundWorkerSignal.Disposed += backgroundWorkerSignal_Disposed;
            backgroundWorkerSignal.WorkerSupportsCancellation = true;
            backgroundWorkerSignal.WorkerReportsProgress      = true;
            backgroundWorkerSignal.DoWork             += backgroundWorkerSignal_DoWork;
            backgroundWorkerSignal.RunWorkerCompleted += backgroundWorkerSignal_RunWorkerCompleted; //80, 70, 60, 50, 40,
            network1                = activation_nework;
            network2                = activation_nework;                                            // new ActivationNetwork(new BipolarSigmoidFunction(), 50, 50, 40, 1);
            network3                = activation_nework;                                            // new ActivationNetwork(new BipolarSigmoidFunction(), 50, 50, 40, 1);
            network4                = activation_nework;                                            // new ActivationNetwork(new BipolarSigmoidFunction(), 50, 50, 40, 1);
            network5                = new ActivationNetwork(new BipolarSigmoidFunction(), 50, 1);
            network6                = new ActivationNetwork(new BipolarSigmoidFunction(), 50, 1);
            teacher                 = new BackPropagationLearning(network1);
            evteacher               = new EvolutionaryLearning(network2, 100);
            reprop                  = new ResilientBackpropagationLearning(network3);
            lbteacher               = new LevenbergMarquardtLearning(network4);
            delta                   = new DeltaRuleLearning(network5);
            perceptron              = new PerceptronLearning(network6);
            delta.LearningRate      = 1;
            perceptron.LearningRate = 0.1;
            myPane                  = new GraphPane();
            listPointsOne           = new PointPairList();

            myPane = zedGraphControl1.GraphPane;

            // set a title
            myPane.Title.Text = "Error VS Time";

            // set X and Y axis titles
            myPane.XAxis.Title.Text = "Time in Milliseconds";
            myPane.YAxis.Title.Text = "Error";
            myCurveOne = myPane.AddCurve("Learning curve", listPointsOne, Color.Red, SymbolType.None);
            // myCurveOne = myPane.AddCurve("Resilient Back Propagation", listPointstwo, Color.Green, SymbolType.None);
            // myCurveOne = myPane.AddCurve("Genetic Learning", listPointsthree, Color.Blue, SymbolType.None);
        }
        public BackPropogation()
        {
            InitializeComponent();

            activation_nework = new ActivationNetwork(new BipolarSigmoidFunction(), 50, 50,  10, 1);

            watch1 = new Stopwatch();

            watch2 = new Stopwatch();

            watch3 = new Stopwatch();

            backgroundWorkerTrainer.Disposed += backgroundWorkerTrainer_Disposed;
            backgroundWorkerTrainer.DoWork += backgroundWorkerTrainer_DoWork;
            backgroundWorkerTrainer.ProgressChanged += backgroundWorkerTrainer_ProgressChanged;
            backgroundWorkerTrainer.WorkerSupportsCancellation = true;
            backgroundWorkerTrainer.WorkerReportsProgress = true;
            saveFileDialog1.Filter = "feed forward network files (*.ffn)|*.ffn";
            saveFileDialog1.Title = "Save neural networkfile";
            saveFileDialog1.InitialDirectory = null;
            saveFileDialog1.FileName = null;

            openFileDialog1.Filter = "feed forward network files (*.ffn)|*.ffn";
            openFileDialog1.Title = "Load neural network file";
            openFileDialog1.InitialDirectory = null;
            openFileDialog1.FileName = null;

            backgroundWorkerSignal.Disposed += backgroundWorkerSignal_Disposed;
            backgroundWorkerSignal.WorkerSupportsCancellation = true;
            backgroundWorkerSignal.WorkerReportsProgress = true;
            backgroundWorkerSignal.DoWork += backgroundWorkerSignal_DoWork;
            backgroundWorkerSignal.RunWorkerCompleted += backgroundWorkerSignal_RunWorkerCompleted;//80, 70, 60, 50, 40,
            network1 = activation_nework;
            network2 = activation_nework; // new ActivationNetwork(new BipolarSigmoidFunction(), 50, 50, 40, 1);
            network3 = activation_nework; // new ActivationNetwork(new BipolarSigmoidFunction(), 50, 50, 40, 1);
            network4 = activation_nework; // new ActivationNetwork(new BipolarSigmoidFunction(), 50, 50, 40, 1);
            network5 = new ActivationNetwork(new BipolarSigmoidFunction(), 50,1);
            network6 = new ActivationNetwork(new BipolarSigmoidFunction(), 50, 1);
            teacher = new BackPropagationLearning(network1);
            evteacher = new EvolutionaryLearning(network2, 100);
            reprop = new ResilientBackpropagationLearning(network3);
            lbteacher = new LevenbergMarquardtLearning(network4);
            delta = new DeltaRuleLearning(network5);
            perceptron = new PerceptronLearning(network6);
            delta.LearningRate = 1;
            perceptron.LearningRate = 0.1;
            myPane = new GraphPane();
            listPointsOne = new PointPairList();

            myPane = zedGraphControl1.GraphPane;

            // set a title
            myPane.Title.Text = "Error VS Time";

            // set X and Y axis titles
            myPane.XAxis.Title.Text = "Time in Milliseconds";
            myPane.YAxis.Title.Text = "Error";
            myCurveOne = myPane.AddCurve("Learning curve", listPointsOne, Color.Red, SymbolType.None);
               // myCurveOne = myPane.AddCurve("Resilient Back Propagation", listPointstwo, Color.Green, SymbolType.None);
               // myCurveOne = myPane.AddCurve("Genetic Learning", listPointsthree, Color.Blue, SymbolType.None);
        }
        static void Main(string[] args)
        {
            //this below 3 lines will shuffle the data whenever i run the application
            List <string> lstFile = File.ReadAllLines(srFileName).ToList();

            lstFile = lstFile.OrderBy(a => Guid.NewGuid()).ToList();

            File.WriteAllLines(srFileName, lstFile);

            int irNumberOfExamples = File.ReadAllLines(srFileName).Count();

            //the first array holds all of the instances
            double[][] input   = new double[irNumberOfExamples][];
            double[][] output  = new double[irNumberOfExamples][];
            double[][] output2 = new double[irNumberOfExamples][];

            List <double> lstOutPutClasses = new List <double>();

            NumberFormatInfo formatProvider = new NumberFormatInfo();

            formatProvider.NumberDecimalSeparator = ".";
            formatProvider.NumberGroupSeparator   = ",";

            foreach (var vrPerLine in File.ReadAllLines(srFileName))
            {
                var vrOutPut = Convert.ToDouble(vrPerLine.Split(',').Last(), formatProvider);

                if (lstOutPutClasses.Contains(vrOutPut) == false)
                {
                    lstOutPutClasses.Add(vrOutPut);
                }
            }

            int irCounter = 0;

            foreach (var vrPerLine in File.ReadAllLines(srFileName))
            {
                input[irCounter] = vrPerLine.Split(',').SkipLast(1).
                                   Select(pr => Convert.ToDouble(pr.Replace("I", "0.0").Replace("M", "0.5").Replace("F", "1.0"), formatProvider)).ToArray();

                output2[irCounter] = new double[] { Convert.ToDouble(vrPerLine.Split(',').Last()) };

                output[irCounter] = new double[lstOutPutClasses.Count];

                var vrCurrentOutClass = Convert.ToDouble(vrPerLine.Split(',').Last(), formatProvider);

                output[irCounter][lstOutPutClasses.IndexOf(vrCurrentOutClass)] = 1;

                irCounter++;
            }



            int irFinalClassCount = lstOutPutClasses.Count;

            double learningRate       = 0.1;
            int    irNumberOfFeatures = input[0].Length;

            int[] numberOfNeurons = new int[] { 100, 100, irFinalClassCount };

            ThresholdFunction function = new ThresholdFunction();

            ActivationNetwork network3 = new ActivationNetwork(
                new SigmoidFunction(2),
                irNumberOfFeatures,
                12,                 // two inputs in the network
                irFinalClassCount); // one neuron in the second layer
                                    // create teacher
            BackPropagationLearning bpteacher = new BackPropagationLearning(network3);

            // loop

            bpteacher.LearningRate = 0.1;
            bpteacher.Momentum     = 0.5;

            for (int i = 0; i < 200000; i++)
            {
                double error = bpteacher.RunEpoch(input, output);

                var vrAcc = calculateAcurracy(network3, input, output);

                Console.WriteLine("BackPropagationLearning -> " + i + ", Error = " + error.ToString("N2") + "\t\t accuracy: " + vrAcc);
            }



            // create activation network
            ActivationNetwork network = new ActivationNetwork(
                function,
                irNumberOfFeatures,
                irFinalClassCount); // one neuron in the second layer

            //double initialStep = 0.125;
            //double sigmoidAlphaValue = 2.0;

            //// create neural network
            //ActivationNetwork neuralNetwork = new ActivationNetwork(new SigmoidFunction(sigmoidAlphaValue), irNumberOfFeatures, 100, irFinalClassCount);

            //// create teacher
            //ParallelResilientBackpropagationLearning neuralTeacher = new ParallelResilientBackpropagationLearning(neuralNetwork);

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

            //for (int i = 0; i < 5000; i++)
            //{
            //    double error = neuralTeacher.RunEpoch(input, output);

            //        Console.WriteLine("Supervised -> " + i + ", Error = " + error);

            //}

            var Layers = network.Layers.Length;

            //int irCounter_2 = 0;
            //BackPropagationLearning teacher2 = new BackPropagationLearning(network);
            //// loop
            //while (true)
            //{
            //    // run epoch of learning procedure
            //    double error = teacher2.RunEpoch(input, output);
            //    // check error value to see if we need to stop

            //    Console.WriteLine("current iteration: " + irCounter_2.ToString("N0") + "error rate: " + error.ToString("N3"));
            //    // ...
            //    irCounter_2++;
            //}

            ActivationNeuron neuron = network.Layers[0].Neurons[0] as ActivationNeuron;
            // create teacher
            DeltaRuleLearning teacher = new DeltaRuleLearning(network);

            // set learning rate
            teacher.LearningRate = learningRate;

            // iterations
            int iteration = 1;

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

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

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

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

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

                errorsList.Add(error);

                // show current iteration
                Console.WriteLine("current iteration: " + iteration.ToString("N0") + "error rate: " + error.ToString("N3"));


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

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

                iteration++;
            }

            Console.ReadLine();
        }