Exemplo n.º 1
0
        public void TestBP_XOR()
        {
            double[] w1 = {0.0, 0.0};
            double[] w2 = {0, 1.0};
            double[] w3 = {1.0, 0.0};

            double[] w4 = { 1.0, 1.0};

            double[][] input = new double[][]{w1,w2,w3,w4};

            _backPropagationLearning_XOR = new BackPropagationLearning( _network_XOR_BP );

            double[] out1 = {0.0};
            double[] out2 = {1.0};
            double[] out3 = {1.0};
            double[] out4 = {0.0};

            double[][] output = new double[][]{out1, out2, out3,out4};

            _backPropagationLearning_XOR.LearningRate = 0.5;

            for (int i = 0 ; i < 20000; i++){

                _backPropagationLearning_XOR.LearningRate *= 0.999;

                int index = 0;

                foreach(double[] wektor in  input){

                    this._network_XOR_BP.Layers[0].Compute(new List<double>(wektor));
                    IList<double> BPInput = this._network_XOR_BP.Layers[0].Outputs;

                    //double[] BPInputArray = new double[40];

                    //BPInput.CopyTo(BPInputArray,0);

                    Console.WriteLine( _backPropagationLearning_XOR.Run(wektor, output[index]) );

                    index++;
                }

            }

            Console.WriteLine("wagi:");
            INeuronsCollection neurony = this._network_XOR_BP.Layers[0].Neurons;

            int counter = 0;
            foreach (INeuron n in neurony)
            {
                foreach (double val in n.Weights)
                {
                    Console.Write(val.ToString("##.###") + "  ||   " );
                    counter++;
                    if(counter % 3 == 0)
                        Console.WriteLine("");
                }
                Console.WriteLine("--------");
            }

            Console.WriteLine("----------------------WARSTWA DRUGA WAGI -----------------------------");

             neurony = this._network_XOR_BP.Layers[1].Neurons;

             counter = 0;
            foreach (INeuron n in neurony)
            {

                Console.WriteLine("THRESHOLD: " +  n.Threshold);

                foreach (double val in n.Weights)
                {
                    Console.Write(val.ToString("##.###") + "  ||   " );
                    counter++;
                    if(counter % 3 == 0)
                        Console.WriteLine("");
                }
                Console.WriteLine("--------");
            }

            this._network_XOR_BP.Compute(DataLoader.ReadInputs("00.txt"));

            Console.WriteLine("00:")	;
            ShowOut(this._network_XOR_BP.Outputs);

            this._network_XOR_BP.Compute(DataLoader.ReadInputs("01.txt"));

            Console.WriteLine("01:")	;
            ShowOut(this._network_XOR_BP.Outputs);

            this._network_XOR_BP.Compute(DataLoader.ReadInputs("10.txt"));

            Console.WriteLine("10:")	;
            ShowOut(this._network_XOR_BP.Outputs);

            this._network_XOR_BP.Compute(DataLoader.ReadInputs("11.txt"));

            Console.WriteLine("11:")	;
            ShowOut(this._network_XOR_BP.Outputs);
        }
Exemplo n.º 2
0
        //    [Test]
        public void TestBP_parz()
        {
            double[] w1 = {0.0, 0.0, 0.0};
            double[] w2 = {0, 0, 1.0};
            double[] w3 = {0, 1.0, 0};

            double[] w4 = {0.0, 1.0, 1.0};
            double[] w5 = {1.0, 0.0, 0};
            double[] w6 = {1.0, 0, 1.0};

            double[] w7 = {1.0, 1.0, 0};
            double[] w8 = {1.0, 1.0, 1.0};

            double[][] input = new double[][]{w1,w2,w3,w4,w5,w6,w7,w8};

            /*
            this._networkKohonen2.Layers[0].Alpha = 0.12;
            this._networkKohonen2.Layers[0].Dim = 0;
            this._networkKohonen2.Layers[0].Learn(5000,input);

            _kohonenLearning_parz = new KohonenLearning(this._networkKohonen_parz.Layers[0]);
            _kohonenLearning_parz.LearningRate = 0.12;
            _kohonenLearning_parz.LearningRadius = 0;

            for(int i=0;i<5000;i++){
                _kohonenLearning_parz.LearningRate *= 0.999;
                _kohonenLearning_parz.RunEpoch(input);

            }

            WidrowHoffLearning _widrowHoff = new WidrowHoffLearning(this._networkKohonen_parz.Layers[1]);
            */

            _backPropagationLearning_parz = new BackPropagationLearning(_network_parz_BP);

            double[] out1 = {0.0,1.0};
            double[] out2 = {1.0, 0.0};
            double[] out3 = {1.0, 0.0};
            double[] out4 = {0.0,1.0};
            double[] out5 = {1.0, 0.0};
            double[] out6 = {0.0,1.0};
            double[] out7 = {0.0,1.0};
            double[] out8 = {1.0, 0.0};

            double[][] output = new double[][]{out1, out2, out3,out4,out5, out6, out7, out8};

            /*
            _widrowHoff.LearningRate = 0.6;

            for(int i=0;i<200;i++){
                _widrowHoff.LearningRate *= 0.99;

                int index = 0;

                foreach(double[] wektor in  input){

                    this._networkKohonen_parz.Layers[0].Compute(new List<double>(wektor));
                    IList<double> widrowInput = this._networkKohonen_parz.Layers[0].Outputs;

                    double[] widrowInputArray = new double[8];

                    widrowInput.CopyTo(widrowInputArray,0);

                //	foreach(double d in widrowInputArray)
                //		Console.WriteLine(d + " " );

                    _widrowHoff.Run(widrowInputArray, output[index]);
                    //Console.WriteLine( _widrowHoff.Run(widrowInputArray, output[index%3]));
                    index++;
                }

            }
            */

            _backPropagationLearning_parz.LearningRate = 0.2;

            for (int i = 0 ; i < 20000; i++){

                _backPropagationLearning_parz.LearningRate *= 0.999;

                int index = 0;

                foreach(double[] wektor in  input){

                    this._network_parz_BP.Layers[0].Compute(new List<double>(wektor));
                    IList<double> BPInput = this._network_parz_BP.Layers[0].Outputs;

                    //double[] BPInputArray = new double[40];

                    //BPInput.CopyTo(BPInputArray,0);

                    Console.WriteLine( _backPropagationLearning_parz.Run(wektor, output[index]) );

                    index++;
                }

            }

            Console.WriteLine("wagi:");
            INeuronsCollection neurony = this._network_parz_BP.Layers[0].Neurons;

            int counter = 0;
            foreach (INeuron n in neurony)
            {
                foreach (double val in n.Weights)
                {
                    Console.Write(val.ToString("##.###") + "  ||   " );
                    counter++;
                    if(counter % 10 == 0)
                        Console.WriteLine("");
                }
                Console.WriteLine("--------");
            }

            Console.WriteLine("----------------------WARSTWA DRUGA WAGI -----------------------------");

             neurony = this._network_parz_BP.Layers[1].Neurons;

             counter = 0;
            foreach (INeuron n in neurony)
            {

                Console.WriteLine("THRESHOLD: " +  n.Threshold);

                foreach (double val in n.Weights)
                {
                    Console.Write(val.ToString("##.###") + "  ||   " );
                    counter++;
                    if(counter % 10 == 0)
                        Console.WriteLine("");
                }
                Console.WriteLine("--------");
            }

            this._network_parz_BP.Compute(DataLoader.ReadInputs("000.txt"));

            Console.WriteLine("000:")	;
            ShowOut(this._network_parz_BP.Outputs);

            this._network_parz_BP.Compute(DataLoader.ReadInputs("001.txt"));

            Console.WriteLine("001:")	;
            ShowOut(this._network_parz_BP.Outputs);

            this._network_parz_BP.Compute(DataLoader.ReadInputs("010.txt"));

            Console.WriteLine("010:")	;
            ShowOut(this._network_parz_BP.Outputs);

            this._network_parz_BP.Compute(DataLoader.ReadInputs("011.txt"));

            Console.WriteLine("011:")	;
            ShowOut(this._network_parz_BP.Outputs);

            this._network_parz_BP.Compute(DataLoader.ReadInputs("100.txt"));

            Console.WriteLine("100:")	;
            ShowOut(this._network_parz_BP.Outputs);

            this._network_parz_BP.Compute(DataLoader.ReadInputs("101.txt"));

            Console.WriteLine("101:")	;
            ShowOut(this._network_parz_BP.Outputs);

            this._network_parz_BP.Compute(DataLoader.ReadInputs("110.txt"));

            Console.WriteLine("110:")	;
            ShowOut(this._network_parz_BP.Outputs);

            this._network_parz_BP.Compute(DataLoader.ReadInputs("111.txt"));

            Console.WriteLine("111:")	;
            ShowOut(this._network_parz_BP.Outputs);
        }