Exemplo n.º 1
0
        static void A()
        {
            FullMesh <double> mesh = new FullMesh <double>(
                inputCounts: 2,
                neuroCounts: new int[] { 10, 1 },
                errorFunction: (a, b) => Math.Abs(a - b),
                activationFunction: TangensHyperbolicus,
                adderFunction: (a, b) => a + b,
                weightingFunction: (a, b) => a * b,
                defaultWeight: 0.5
                );

            Func <double, double, double>[] funcs = new Func <double, double, double>[]
            {
                (a, b) => a + b,
                (a, b) => a - b,
                (a, b) => a * b,
                (a, b) => a % b,
            };

            double[][] input = new double[][]
            {
                new[] { 0.0, 0.0 },
                new[] { 0.0, 1.0 },
                new[] { 1.0, 0.0 },
                new[] { 1.0, 1.0 },
            };

            double[][] output = new double[][]
            {
                new[] { 1.0 },
                new[] { 1.0 },
                new[] { 0.0 },
                new[] { 1.0 },
            };

            //for (int i = 0; i < 20; i++)
            //    mesh.LearnIteration(new DeepRandom<double>(250, d => d + GlobalRandom.Get.NextDouble() * 0.4 - 0.2) { PrintProgress = true }, input, output, -0.0001);
            //for (int i = 0; i < 10000 && mesh.LastError > 0.00001; i++)
            //    mesh.LearnIteration(new Evolution<double>(d => d + GlobalRandom.Get.NextDouble() * 0.4 - 0.2), input, output, -0.00001);
            mesh.LearnIteration(new BruteForce <double>(d => d + GlobalRandom.Get.NextDouble() * 0.4 - 0.2, () => funcs[GlobalRandom.Get.Next(0, funcs.Length)])
            {
                PrintProgress = true
            }, input, output, 0.01);

            Array.ForEach(input, d => Console.WriteLine(Format(d) + " : " + Format(mesh.Calc(d))));

            while (true)
            {
                Console.WriteLine(Format(mesh.Calc(Console.ReadLine().Split(' ').Select(s => double.Parse(s)).ToArray())));
            }
        }
Exemplo n.º 2
0
        static void B()
        {
            FullMesh <bool> mesh = new FullMesh <bool>(
                inputCounts: 2,
                neuroCounts: new int[] { 10, 10, 1 },
                errorFunction: (a, b) => (a != b) ? 1 : 0,
                activationFunction: b => b,
                adderFunction: (a, b) => a || b,
                weightingFunction: (a, b) => a || b,
                defaultWeight: false
                );

            Func <bool, bool, bool>[] funcs = new Func <bool, bool, bool>[]
            {
                (a, b) => !(a && b),
                (a, b) => !(!a && b),
                (a, b) => !(a && !b),
                (a, b) => !(!a && !b),
                (a, b) => a && b,
                (a, b) => !a && b,
                (a, b) => a && !b,
                (a, b) => !a && !b,
                (a, b) => !(a || b),
                (a, b) => !(!a || b),
                (a, b) => !(a || !b),
                (a, b) => !(!a || !b),
                (a, b) => a || b,
                (a, b) => !a || b,
                (a, b) => a || !b,
                (a, b) => !a || !b,
            };

            mesh.Neurons.ForEach(n => n.ForEach(nn => nn.WeightingFunction = funcs[GlobalRandom.Get.Next(0, funcs.Length)]));

            bool[][] input = new bool[][]
            {
                new[] { false, false },
                new[] { false, true },
                new[] { true, false },
                new[] { true, true },
            };

            bool[][] output = new bool[][]
            {
                new[] { true },
                new[] { true },
                new[] { false },
                new[] { true },
            };


            mesh.LearnIteration(new BruteForce <bool>(b => b, () => funcs[GlobalRandom.Get.Next(0, funcs.Length)])
            {
                PrintProgress = true
            }, input, output, 0.01);

            Array.ForEach(input, d => Console.WriteLine(Format(d) + " : " + Format(mesh.Calc(d))));

            Console.ReadKey();
        }
Exemplo n.º 3
0
        public double LearnIteration(FullMesh <T> net, T[][] input, T[][] output, double switchThreshold)
        {
            double temp;
            int    x   = GlobalRandom.Get.Next(0, net.Neurons.Count);
            int    y   = GlobalRandom.Get.Next(0, net.Neurons[x].Count);
            int    w   = GlobalRandom.Get.Next(0, net.Neurons[x][y].Weights.Count);
            T      old = net.Neurons[x][y].Weights[w];

            net.Neurons[x][y].Weights[w] = Permutater(net.Neurons[x][y].Weights[w]);
            temp = net.GetError(input.Select(i => net.Calc(i)).ToArray(), output);

            if (temp - net.LastError < switchThreshold)
            {
                Console.WriteLine("new Error : " + temp);
                return(temp);
            }
            else
            {
                net.Neurons[x][y].Weights[w] = old;
                return(net.LastError);
            }
        }
Exemplo n.º 4
0
        public double LearnIteration(FullMesh <T> net, T[][] input, T[][] output, double switchThreshold = -0.1)
        {
            double lastError = net.LastError;
            double temp      = 0;

            foreach (List <Neuron <T> > l in net.Neurons)
            {
                if (PrintProgress)
                {
                    Console.WriteLine("new Layer");
                }
                for (int i = 0; i < InnerIterations; i++)
                {
                    int y   = GlobalRandom.Get.Next(0, l.Count);
                    int w   = GlobalRandom.Get.Next(0, l[y].Weights.Count);
                    T   old = l[y].Weights[w];
                    l[y].Weights[w] = Permutater(l[y].Weights[w]);
                    T[][] c = input.Select(t => net.Calc(t)).ToArray();
                    temp = net.GetError(c, output);
                    if (!(temp - lastError < switchThreshold))
                    {
                        l[y].Weights[w] = old;
                    }
                    else
                    {
                        lastError = temp;
                        if (PrintProgress)
                        {
                            Console.WriteLine("new Error : " + temp);
                        }
                    }
                }
            }
            if (PrintProgress)
            {
                Console.WriteLine("finished");
            }
            return(lastError);
        }
Exemplo n.º 5
0
        public double LearnIteration(FullMesh <T> net, T[][] input, T[][] output, double switchThreshold)
        {
            double lastError = net.LastError;
            int    counter   = 1;

            while (lastError > switchThreshold)
            {
                int x = GlobalRandom.Get.Next(0, net.Neurons.Count);
                int y = GlobalRandom.Get.Next(0, net.Neurons[x].Count);
                int w = GlobalRandom.Get.Next(0, net.Neurons[x][y].Weights.Count);
                net.Neurons[x][y].Weights[w]        = Permutater(net.Neurons[x][y].Weights[w]);
                net.Neurons[x][y].WeightingFunction = WeightingChanging();
                lastError = net.GetError(input.Select(i => net.Calc(i)).ToArray(), output);
                if (PrintProgress)
                {
                    Console.WriteLine(counter++ + ": Curr Error : " + lastError);
                }
            }
            if (PrintProgress)
            {
                Console.WriteLine("finished");
            }
            return(lastError);
        }