Beispiel #1
0
        private static double ExecuteAlgorithmForSpecifiedData(SingleLayerPerceptron singleLayerPerceptron, string treningDataPath, string testDataPath)
        {
            char separator    = '\t';
            char decimalPoint = '.';
            var  winner       = singleLayerPerceptron.Teach(treningDataPath, separator, decimalPoint, 1000, 0.2);

            return(singleLayerPerceptron.Test(testDataPath, separator, decimalPoint, winner.Weights, winner.Bias));
        }
 public void Create(SingleLayerPerceptron p)
 {
     for (int i = 0, length = this.inputs.GetLength(1); i < length; i++)
     {
         this.inputs[i, 0]  = SingleLayerPerceptron.rng.NextFloat(-11);
         this.inputs[i, 1]  = SingleLayerPerceptron.rng.NextFloat(-11);
         this.targets[i, 0] = p.IsAboveLine(new float[] { this.inputs[i, 0], this.inputs[i, 1] });
     }
 }
Beispiel #3
0
        private static void ProgramFormula(int nInput, int nOutput, int neuronsPerLayers)
        {
            SingleLayerPerceptron slp = new SingleLayerPerceptron(nInput, neuronsPerLayers, nOutput);

            for (int i = 0; i < 1000000; i++)
            {
                Random random = new Random(Guid.NewGuid().GetHashCode());
                double value1 = random.NextDouble();
                random = new Random(Guid.NewGuid().GetHashCode());
                double value2 = random.NextDouble();

                double y = Formula(value1);

                double result = value2 > y ? 1 : 0;

                slp.Train(new List <double>()
                {
                    value1, value2
                }, new List <double> {
                    result
                });
            }

            double n = 0;

            for (int i = 0; i < 200; i++)
            {
                Random random = new Random(Guid.NewGuid().GetHashCode());
                double value1 = random.NextDouble();
                random = new Random(Guid.NewGuid().GetHashCode());
                double value2 = random.NextDouble();

                double y = Formula(value1);

                double result = value2 > y ? 1 : 0;
                var    guess  = slp.Predict(new List <double>()
                {
                    value1, value2
                });
                double proj = guess[0] > 0.5 ? 1 : 0;

                bool correct = proj == result;
                Console.WriteLine("result : " + result);
                Console.WriteLine("guess : " + guess[0]);
                Console.WriteLine("correct : " + correct);
                Console.WriteLine("--");

                if (correct)
                {
                    n++;
                }
            }
            Console.WriteLine(n / 200);
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            var slp = new SingleLayerPerceptron();

            Console.WriteLine("Accuracy in A: {0}", ExecuteAlgorithmForSpecifiedData(slp, "Data/iris_2vs3_A_tr.txt", "Data/iris_2vs3_A_te.txt"));
            Console.WriteLine("Accuracy in B: {0}", ExecuteAlgorithmForSpecifiedData(slp, "Data/iris_2vs3_B_tr.txt", "Data/iris_2vs3_B_te.txt"));
            Console.WriteLine("Accuracy in C: {0}", ExecuteAlgorithmForSpecifiedData(slp, "Data/iris_2vs3_C_tr.txt", "Data/iris_2vs3_C_te.txt"));
            Console.WriteLine("Accuracy in D: {0}", ExecuteAlgorithmForSpecifiedData(slp, "Data/iris_2vs3_D_tr.txt", "Data/iris_2vs3_D_te.txt"));
            Console.WriteLine("Accuracy in E: {0}", ExecuteAlgorithmForSpecifiedData(slp, "Data/iris_2vs3_E_tr.txt", "Data/iris_2vs3_E_te.txt"));
            Console.WriteLine("Accuracy in AND: {0}", ExecuteAlgorithmForSpecifiedData(slp, "Data/iris_and.txt", "Data/iris_and.txt"));

            Console.ReadKey();
        }
Beispiel #5
0
        private static SingleLayerPerceptron SingleLayerPerceptronXORProgram(int nInput, int nOutput, int neuronsPerLayers)
        {
            SingleLayerPerceptron slp = new SingleLayerPerceptron(nInput, neuronsPerLayers, nOutput);

            for (int i = 0; i < 100000; i++)
            {
                Random random = new Random(Guid.NewGuid().GetHashCode());
                int    value1 = random.Next() % 2;
                random = new Random(Guid.NewGuid().GetHashCode());
                int value2 = random.Next() % 2;

                int xor = XOR(value1, value2);

                slp.Train(new List <double>()
                {
                    value1, value2
                }, new List <double>()
                {
                    xor
                });
            }
            double n = 0;

            for (int i = 0; i < 2000; i++)
            {
                Random random = new Random(Guid.NewGuid().GetHashCode());
                int    value1 = random.Next() % 2;
                random = new Random(Guid.NewGuid().GetHashCode());
                int value2 = random.Next() % 2;

                int xor = XOR(value1, value2);

                var guess = slp.Predict(new List <double>()
                {
                    value1, value2
                });
                double result  = guess[0] > 0.5 ? 1 : 0;
                bool   correct = result == xor;
                Console.WriteLine("xor : " + xor);
                Console.WriteLine("guess : " + guess[0]);
                Console.WriteLine("--");
                if (correct)
                {
                    n++;
                }
            }
            Console.WriteLine(n / 2000);
            Console.Read();
            return(slp);
        }