コード例 #1
0
ファイル: BinarySerializerShould.cs プロジェクト: Diover/nets
        public void SaveAndLoadNetState()
        {
            const int inputsCount = 3;
            var net = new SimpleFuzzyNet(inputsCount, new[] {2},
                                         levelsCount: 3,
                                         littleFuzzyNumberGenerator:
                                             () => DiscreteFuzzyNumber.GenerateLittleNumber(levelsCount: 3));

            var first = new DiscreteFuzzyNumber(new TriangularFuzzyFunction(-1, 0, 1), 3);
            var second = new DiscreteFuzzyNumber(new TriangularFuzzyFunction(-1, -0.5, 0), 3);
            var third = new DiscreteFuzzyNumber(new TriangularFuzzyFunction(0, 0.5, 1), 3);

            var inputs = new List<IFuzzyNumber>
                {
                    first,
                    second,
                    third,
                };

            var expectedOutput = net.Propagate(inputs).First();

            BinaryFileSerializer.SaveNetState(_filename, net);
            var loadedNet = BinaryFileSerializer.LoadNetState(_filename);

            var actualOutput = loadedNet.PropagateLastInput().First();

            expectedOutput.ForeachLevel((alpha, level) => Assert.That(level, Is.EqualTo(actualOutput.GetAlphaLevel(alpha))));
        }
コード例 #2
0
ファイル: BackPropagationShould.cs プロジェクト: Diover/nets
        public void ReturnZeroErrorOnPatternsSameAsOutput()
        {
            var patternA = new LearningPattern
                {
                    Input = new List<IFuzzyNumber>
                        {
                            new DiscreteFuzzyNumber(new TriangularFuzzyFunction(-1, 0, 1), 3),
                            new DiscreteFuzzyNumber(new TriangularFuzzyFunction(-1, 0, 1), 3),
                            new DiscreteFuzzyNumber(new TriangularFuzzyFunction(-1, 0, 1), 3),
                        },
                    Output = new List<IFuzzyNumber>(),
                };

            var patternB = new LearningPattern
            {
                Input = new List<IFuzzyNumber>
                        {
                            new DiscreteFuzzyNumber(new TriangularFuzzyFunction(0, 1, 2), 3),
                            new DiscreteFuzzyNumber(new TriangularFuzzyFunction(0, 1, 2), 3),
                            new DiscreteFuzzyNumber(new TriangularFuzzyFunction(0, 1, 2), 3),
                        },
                Output = new List<IFuzzyNumber>()
            };

            var bp = new BackPropagation(new List<ILearningPattern> { patternA, patternB });
            var net = new SimpleFuzzyNet(3, new[] {2}, () => DiscreteFuzzyNumber.GenerateLittleNumber(levelsCount: 3),
                                         levelsCount: 3);

            patternA.Output = net.Propagate(patternA.Input);
            patternB.Output = net.Propagate(patternB.Input);

            bp.StepPerformed += (state) => Assert.That(state.CycleError, Is.EqualTo(0.0));
            bp.LearnNet(net);
        }
コード例 #3
0
ファイル: SimpleFuzzyNetShould.cs プロジェクト: Diover/nets
        public void ContainsCorrectLastInputsVectorAfterSecondPropagationWithWeightsChanged()
        {
            const int inputs = 2;
            var hidden = new[] { 2, 2 };
            const int outputs = 2;
            Func<double, double> activation = x => x;
            var net = new SimpleFuzzyNet(inputs, hidden, GenerateNumbers, activation, outputs);
            var input = new List<IFuzzyNumber>
                {
                    new RealNumber(1.0),
                    new RealNumber(2.0),
                };
            var lastInputs = net.GetLastInputsForWeights();

            net.Propagate(input);
            ChangeFirstWeightsOfNeuronsToOne(net.Layers);
            net.Propagate(input);

            var expectedLastInputs = new IFuzzyNumber[]
                {
                    //output layer
                    //first neuron
                    new RealNumber(-5.0),
                    new RealNumber(-5.0),
                    //second neuron
                    new RealNumber(-5.0),
                    new RealNumber(-5.0),

                    //hidden 1
                    new RealNumber(1.0),
                    new RealNumber(2.0),
                    new RealNumber(1.0),
                    new RealNumber(2.0),

                    //hidden 2
                    new RealNumber(-2.0),
                    new RealNumber(3.0),
                    new RealNumber(-2.0),
                    new RealNumber(3.0),
                };

            int i = 0;
            foreach (var lastInput in lastInputs)
            {
                Assert.That(lastInput.Signal.GetMod().X, Is.EqualTo(expectedLastInputs[i].GetMod().X));
                i++;
            }
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: Diover/nets
        static void Main(string[] args)
        {
            /*var f =
                new Func<IVector, double>(
                    vector =>
                    -Math.Exp(-Math.Pow(vector[0].GetMod().X - 1, 2)) -
                    Math.Exp(-0.5 * Math.Pow(vector[1].GetMod().X - 2, 2)));

            Func<IVector, IVector> g = vector => new Vector(new[]
                {
                    // df/dx = {-2 e^(-    (x-1)^2) (x-1)}
                    new RealNumber(2*Math.Exp(-Math.Pow(vector[0].GetMod().X - 1, 2))*(vector[0].GetMod().X - 1)),

                    // df/dy = {-  e^(-1/2 (y-2)^2) (y-2)}
                    new RealNumber(Math.Exp(-0.5*Math.Pow(vector[1].GetMod().X - 2, 2))*(vector[1].GetMod().X - 2))
                });*/

            /*var f = new Func<IVector, double>(
                    vector =>
                    //(1 - x)^2 + 100(-x^2 + y)^2
                    Math.Pow(1 - vector[0].GetMod().X, 2) + 100 * Math.Pow(-Math.Pow(vector[0].GetMod().X, 2) + vector[1].GetMod().X, 2) );
            Func<IVector, IVector> g = vector => new Vector(new[]
                {
                    // df/dx = {-2x + 200(-x^2 + y)(-2)x }
                    new RealNumber( -2.0*vector[0].GetMod().X - 400*(-Math.Pow(vector[0].GetMod().X, 2) + vector[1].GetMod().X)*vector[0].GetMod().X ),

                    // df/dy = {200(-x^2 + y)}
                    new RealNumber(200*( -Math.Pow(vector[0].GetMod().X, 2) + vector[1].GetMod().X ))
                });*/

            /*var f = new Func<IVector, double>(
                    vector =>
                        //(1 - x)^2 + (1 - y)^2
                    Math.Pow(1.0 - vector[0].GetMod().X, 2) + Math.Pow(1.0 - vector[1].GetMod().X, 2));
            Func<IVector, IVector> g = vector => new Vector(new[]
                {
                    // df/dx = {-2(1 - x)}
                    new RealNumber( -2.0*(1.0 - vector[0].GetMod().X) ),

                    // df/dy = {-2(1 - y)}
                    new RealNumber( -2.0*(1.0 - vector[1].GetMod().X) )
                });*/

            //var b = new BfgsMethod();
            //b.Minimize(f, g, 2);

            //var x = b.Values;
            //var fx = b.Minimum;
            //return;

            const int inputsCount = 5;
            const int hiddenNeuronsCount = 11;
            const int outputNeuronsCount = 3;
            //var net = new SimpleFuzzyNet(inputsCount, new[] {hiddenNeuronsCount}, () => DiscreteFuzzyNumber.GenerateLittleNumber(levelsCount: 11), levelsCount: 11);
            var net = new SimpleFuzzyNet(inputsCount, new[] { hiddenNeuronsCount, hiddenNeuronsCount}, RealNumber.GenerateLittleNumber, outputNeuronsCount: outputNeuronsCount);
            /*
            //weights for 2-2-1 net for XOR function
            var weights = new Vector(new IFuzzyNumber[]
                {
                    //output layer
                    //one neuron
                    new RealNumber(0.032680),
                    new RealNumber(0.020701),

                    //hidden 0
                    new RealNumber(-0.082843),
                    new RealNumber(0.018629),
                    new RealNumber(-0.011006),
                    new RealNumber(-0.071407),
                });
            net.SetWeights(weights);
            */
            const string filename = "marketPatterns-params5-3outputs-learning.txt";
            var numberParser = new RealNumberParser();
            //const string filename = "testPatterns.txt";
            //var numberParser = new FuzzyNumberParser();

            //var patterns = new TestPatternPreparer(Path.Combine("../../../Misc", filename), numberParser).PreparePatterns();
            var patterns = new MarketSeriesPatternPreparer(Path.Combine("../../../Misc", filename), numberParser).PreparePatterns();
            //var patterns = CreatePatterns((x, y) => Math.Abs(Math.Sin(x) + Math.Sin(y))/2.0);

            var bp = new BackPropagation(patterns, 0.05, 0.01);
            bp.CyclePerformed +=
                (state) =>
                {
                    //Console.ReadKey();
                    if (state.Cycle%50 == 0)
                    {
                        Console.WriteLine("cycle: " + state.Cycle +
                                          " error: " + state.CycleError.ToString("0.#####################"));
                    }
                };

            /*var bp = new BackPropagationWithPseudoNeuton(patterns);
            bp.CyclePerformed +=
                (state) =>
                    {
                        //Console.ReadKey();
                        //if (state.Cycle % 10 == 0)
                            Console.WriteLine("cycle: " + state.Cycle +
                                              " error: " + state.CycleError.ToString("0.#########################"));
                    };*/

            bp.LearnNet(net);

            var key = new ConsoleKeyInfo();
            do
            {
                while(Console.KeyAvailable == false)
                    Thread.Sleep(200);
                key = Console.ReadKey();
            } while (key.Key != ConsoleKey.Escape);

            bp.StopLearning();

            Console.WriteLine("Simple method finished");

            var bp2 = new BackPropagationWithPseudoNeuton(patterns);
            bp2.CyclePerformed +=
                (state) =>
                {
                    //Console.ReadKey();
                    //if (state.Cycle % 10 == 0)
                    Console.WriteLine("cycle: " + state.Cycle +
                                      " error: " + state.CycleError.ToString("0.#########################"));
                };

            //bp2.LearnNet(net);

            var key1 = new ConsoleKeyInfo();
            do
            {
                while (Console.KeyAvailable == false)
                    Thread.Sleep(200);
                key1 = Console.ReadKey();
            } while (key1.Key != ConsoleKey.Escape);

            bp2.StopLearning();

            Console.WriteLine("Learning finished. Press any key...");
            Console.ReadKey();

            BinaryFileSerializer.SaveNetState(
                "../../../Misc/LearnedNet " + Path.GetFileNameWithoutExtension(filename) +
                " " + inputsCount + "-" + hiddenNeuronsCount + "-" + outputNeuronsCount + ".net", net);

            var output = patterns.Select(pattern => net.Propagate(pattern.Input)).ToList();

            for (int i = 0; i < patterns.Count; i++)
            {
                Console.WriteLine("Test: {0}, Real: {1}", string.Join(";", patterns.ElementAt(i).Output), string.Join(";", output.ElementAt(i)));
            }

            Console.WriteLine("Finished. Press any key...");
            Console.ReadKey();
        }
コード例 #5
0
ファイル: SimpleFuzzyNetShould.cs プロジェクト: Diover/nets
        public void SetPropagatedErrorToNullAfterClear()
        {
            const int inputs = 2;
            var hidden = new[] { 2, 2 };
            var net = new SimpleFuzzyNet(inputs, hidden, () => new RealNumber(0.0));

            SetPropagatedErrorToZero(net);
            net.ClearPropagatedError();

            foreach (var layer in net.Layers)
            {
                layer.ForeachNeuron((i, neuron) => Assert.IsNull(neuron.PropagatedError));
            }
        }
コード例 #6
0
ファイル: SimpleFuzzyNetShould.cs プロジェクト: Diover/nets
        public void PropagateSignalWithZeroWeights()
        {
            const int inputs = 2;
            var hidden = new[] { 2, 2 };
            const int outputs = 2;
            Func<double, double> activation = x => 1.0/(1.0 + Math.Exp(-x));
            var net = new SimpleFuzzyNet(inputs, hidden, () => new RealNumber(0.0), activation, outputNeuronsCount: outputs);

            var input = new List<IFuzzyNumber>
                {
                    new RealNumber(1.0),
                    new RealNumber(2.0),
                };

            var output = net.Propagate(input);

            var expected = new List<IFuzzyNumber>
                {
                    new RealNumber(0.5),
                    new RealNumber(0.5),
                };

            int i = 0;
            foreach (var expectedNumber in expected)
            {
                Assert.That(output.ElementAt(i).GetMod().X, Is.EqualTo(expectedNumber.GetMod().X));
                i++;
            }
        }
コード例 #7
0
ファイル: SimpleFuzzyNetShould.cs プロジェクト: Diover/nets
        public void PropagateSignalWithXMinusOneFunction()
        {
            const int inputs = 2;
            var hidden = new[] { 2, 2 };
            const int outputs = 2;
            Func<double, double> activation = x => x - 1;
            var net = new SimpleFuzzyNet(inputs, hidden, GenerateNumbers, activation, outputs);

            var input = new List<IFuzzyNumber>
                {
                    new RealNumber(1.0),
                    new RealNumber(2.0),
                };

            var output = net.Propagate(input);

            var expected = new List<IFuzzyNumber>
                {
                    new RealNumber(-23.0),
                    new RealNumber(-25.0),
                };

            int i = 0;
            foreach (var expectedNumber in expected)
            {
                Assert.That(output.ElementAt(i).GetMod().X, Is.EqualTo(expectedNumber.GetMod().X));
                i++;
            }
        }
コード例 #8
0
ファイル: SimpleFuzzyNetShould.cs プロジェクト: Diover/nets
        public void PropagateSignalAfterWeightsChanged()
        {
            const int inputs = 2;
            var hidden = new[] { 2, 2 };
            const int outputs = 2;
            Func<double, double> activation = x => x;
            var net = new SimpleFuzzyNet(inputs, hidden, () => new RealNumber(0.0), activation, outputs);

            var input = new List<IFuzzyNumber>
                {
                    new RealNumber(1.0),
                    new RealNumber(2.0),
                };
            var weights = new Vector(new IFuzzyNumber[]
                {
                    //output layer
                    //first neuron
                    new RealNumber(2.0),
                    new RealNumber(2.0),
                    //second neuron
                    new RealNumber(4.0),
                    new RealNumber(2.0),

                    //hidden 1
                    new RealNumber(1.0),
                    new RealNumber(-1.5),
                    new RealNumber(2.0),
                    new RealNumber(1.0),

                    //hidden 2
                    new RealNumber(-1.0),
                    new RealNumber(-1.0),
                    new RealNumber(2.0),
                    new RealNumber(-1.0),
                });

            net.SetWeights(weights);
            var output = net.Propagate(input);

            var expected = new List<IFuzzyNumber>
                {
                    new RealNumber(-20.0),
                    new RealNumber(-24.0),
                };

            int i = 0;
            foreach (var expectedNumber in expected)
            {
                Assert.That(output.ElementAt(i).GetMod().X, Is.EqualTo(expectedNumber.GetMod().X));
                i++;
            }
        }
コード例 #9
0
ファイル: SimpleFuzzyNetShould.cs プロジェクト: Diover/nets
        public void PropagateSignalAfterSomeWeightsChangedToZero()
        {
            const int inputs = 2;
            var hidden = new[] { 2, 2 };
            const int outputs = 2;
            Func<double, double> activation = x => x;
            var net = new SimpleFuzzyNet(inputs, hidden, GenerateNumbers, activation, outputs);

            var input = new List<IFuzzyNumber>
                {
                    new RealNumber(1.0),
                    new RealNumber(2.0),
                };

            net.Propagate(input);
            ChangeFirstWeightsOfNeuronsToZero(net.Layers);
            var output = net.Propagate(input);

            var expected = new List<IFuzzyNumber>
                {
                    new RealNumber(-4.0),
                    new RealNumber(-4.0),
                };

            int i = 0;
            foreach (var expectedNumber in expected)
            {
                Assert.That(output.ElementAt(i).GetMod().X, Is.EqualTo(expectedNumber.GetMod().X));
                i++;
            }
        }
コード例 #10
0
ファイル: SimpleFuzzyNetShould.cs プロジェクト: Diover/nets
        public void CreateNulledLastInputsVectorAtStart()
        {
            const int inputs = 2;
            var hidden = new[] { 2, 2 };
            const int outputs = 2;
            Func<double, double> activation = x => x;
            var net = new SimpleFuzzyNet(inputs, hidden, () => new RealNumber(0.0), activation, outputs);

            var lastInputs = net.GetLastInputsForWeights();

            foreach (var input in lastInputs)
            {
                Assert.IsNull(input.Signal);
            }
        }
コード例 #11
0
ファイル: SimpleFuzzyNetShould.cs プロジェクト: Diover/nets
        public void CreateCorrectLastInputsVectorAfterFirstPropagation()
        {
            const int inputs = 2;
            var hidden = new[] { 2, 2 };
            const int outputs = 2;
            Func<double, double> activation = x => x;
            var net = new SimpleFuzzyNet(inputs, hidden, GenerateNumbers, activation, outputs);
            var input = new List<IFuzzyNumber>
                {
                    new RealNumber(1.0),
                    new RealNumber(2.0),
                };
            var lastInputsLinks = net.GetLastInputsForWeights();
            net.Propagate(input);
            var lastInputs = lastInputsLinks.ToSignalsVector();

            var expectedLastInputs = new IFuzzyNumber[]
                {
                    //output layer
                    //first neuron
                    new RealNumber(-2.0),
                    new RealNumber(-8.0),
                    //second neuron
                    new RealNumber(-2.0),
                    new RealNumber(-8.0),

                    //hidden 1
                    new RealNumber(1.0),
                    new RealNumber(2.0),
                    new RealNumber(1.0),
                    new RealNumber(2.0),

                    //hidden 2
                    new RealNumber(-2.0),
                    new RealNumber(4.0),
                    new RealNumber(-2.0),
                    new RealNumber(4.0),
                };

            for (int i = 0; i < expectedLastInputs.Length; i++)
            {
                Assert.That(lastInputs[i].GetMod().X, Is.EqualTo(expectedLastInputs[i].GetMod().X));
            }
        }
コード例 #12
0
ファイル: SimpleFuzzyNetShould.cs プロジェクト: Diover/nets
        public void CorrectlySetWeightsVector()
        {
            const int inputs = 2;
            var hidden = new[] { 2, 2 };
            const int outputs = 2;
            var net = new SimpleFuzzyNet(inputs, hidden, ()=>new RealNumber(0.0), outputNeuronsCount: outputs);
            var weights = new Vector(new IFuzzyNumber[]
                {
                    //output layer
                    //first neuron
                    new RealNumber(2.0),
                    new RealNumber(2.0),
                    //second neuron
                    new RealNumber(4.0),
                    new RealNumber(2.0),

                    //hidden 1
                    new RealNumber(1.0),
                    new RealNumber(-1.5),
                    new RealNumber(2.0),
                    new RealNumber(1.0),

                    //hidden 2
                    new RealNumber(-1.0),
                    new RealNumber(-1.0),
                    new RealNumber(2.0),
                    new RealNumber(-1.0),
                });

            //tuple: (layer, neuron, weight)
            var expectedWeights = new Dictionary<Tuple<int, int, int>, IFuzzyNumber>();
            for (int i = 0; i < weights.Length; i++)
            {
                expectedWeights.Add(new Tuple<int, int, int>(ToLayerIndex(i), ToNeuronIndex(i), ToWeightIndex(i)),
                                    weights[i]);
            }

            net.SetWeights(weights);

            int l = 0;
            foreach (var layer in net.Layers)
            {
                layer.ForeachNeuron((i, neuron) =>
                {
                    neuron.ForeachWeight((j, weight) =>
                        {
                            Assert.That(weight.Signal.GetMod().X,
                                        Is.EqualTo(expectedWeights[new Tuple<int, int, int>(l, i, j)].GetMod().X));
                        });
                });
                l++;
            }
        }
コード例 #13
0
ファイル: SimpleFuzzyNetShould.cs プロジェクト: Diover/nets
        public void CorrectlyReturnNumberOfWeights()
        {
            const int inputs = 5;
            var hidden = new[] {7, 2};
            const int output = 1;
            var net = new SimpleFuzzyNet(inputs, hidden, RealNumber.GenerateLittleNumber);

            var weightsCount = net.WeightsCount;

            Assert.That(weightsCount, Is.EqualTo(inputs * hidden[0] + hidden[0] * hidden[1] + hidden[1] * output));
        }
コード例 #14
0
ファイル: SimpleFuzzyNetShould.cs プロジェクト: Diover/nets
        public void CorrectlyGetWeightsOfNextLayer()
        {
            const int inputs = 2;
            var hidden = new[] { 2, 2 };
            const int outputs = 2;
            var net = new SimpleFuzzyNet(inputs, hidden, GenerateNumbers, outputNeuronsCount: outputs);

            var weights = new Vector(new IFuzzyNumber[]
                {
                    //output layer
                    //first neuron
                    new RealNumber(2.0),
                    new RealNumber(2.0),
                    //second neuron
                    new RealNumber(4.0),
                    new RealNumber(2.0),

                    //hidden 1
                    new RealNumber(1.0),
                    new RealNumber(-1.5),
                    new RealNumber(2.0),
                    new RealNumber(1.0),

                    //hidden 2
                    new RealNumber(-1.0),
                    new RealNumber(-1.0),
                    new RealNumber(2.0),
                    new RealNumber(-1.0),
                });

            //tuple: (layer, neuron, weight)
            var expectedWeights = new Dictionary<Tuple<int, int, int>, IFuzzyNumber>();
            for (int i = 0; i < weights.Length; i++)
            {
                expectedWeights.Add(new Tuple<int, int, int>(ToLayerIndex(i), ToNeuronIndex(i), ToWeightIndex(i)),
                                    weights[i]);
            }

            for (int i = net.Layers.Count - 2; i >= 0; i--)
            {
                var layer = net.Layers.ElementAt(i);
                var nextLayer = net.Layers.ElementAt(i + 1);

                layer.ForeachNeuron((neuronIndex, neuron) =>
                {
                    for (int j = 0; j < nextLayer.NeuronsCount; j++)
                    {
                        var weight = nextLayer.GetNeuron(j).GetWeight(neuronIndex).Signal;
                        Assert.That(weight.GetMod().X,
                                    Is.EqualTo(expectedWeights[new Tuple<int, int, int>(i + 1, j, neuronIndex)].GetMod().X));
                    }
                });
            }
        }
コード例 #15
0
ファイル: SimpleFuzzyNetShould.cs プロジェクト: Diover/nets
        public void CorrectlyCreateWeightsVector()
        {
            const int inputs = 2;
            var hidden = new[] { 2, 2 };
            const int outputs = 2;
            var net = new SimpleFuzzyNet(inputs, hidden, GenerateNumbers, outputNeuronsCount: outputs);

            var weights = net.GetWeights();
            var expectedWeights = new Vector(new IFuzzyNumber[]
                {
                    //output layer
                    //first neuron
                    new RealNumber(2.0),
                    new RealNumber(2.0),
                    //second neuron
                    new RealNumber(4.0),
                    new RealNumber(2.0),

                    //hidden 1
                    new RealNumber(1.0),
                    new RealNumber(-1.5),
                    new RealNumber(2.0),
                    new RealNumber(1.0),

                    //hidden 2
                    new RealNumber(-1.0),
                    new RealNumber(-1.0),
                    new RealNumber(2.0),
                    new RealNumber(-1.0),
                });

            for (int i = 0; i < expectedWeights.Length; i++)
            {
                Assert.That(weights[i].GetMod().X, Is.EqualTo(expectedWeights[i].GetMod().X));
            }
        }