Пример #1
0
        public void TestAlternativeMatrix()
        {
            double[][] from_l1 = new double[][]
            {
                new double[] { -30, 20, 20 },
                new double[] { 10, -20, -20 }
            };
            double[][] from_l2 = new double[][]
            {
                new double[] { -10, 20, 20 }
            };
            double[][][] weights = new double[][][]
            {
                from_l1,
                from_l2
            };
            NNetwork n = NNetwork.SigmoidNetwork(new int[] { 2, 2, 1 });

            n.SetWeightMatrix(weights);

            double[]   from_l1_expected = new double[] { -30, 20, 20, 10, -20, -20 };
            double[]   from_l2_expected = new double[] { -10, 20, 20 };
            double[][] weights_expected = new double[][]
            {
                from_l1_expected,
                from_l2_expected
            };
            Assert.AreEqual(weights_expected, n.GetWeightMatrix());
        }
Пример #2
0
        private void buttonCreateNetwork_Click(object sender, EventArgs e)
        {
            String[] layers_string = textLayers.Text.Split(";".ToCharArray());
            int[]    layers        = new int[layers_string.Length];
            for (int i = 0; i < layers_string.Length; i++)
            {
                layers[i] = int.Parse(layers_string[i]);
            }
            if (radioHyperbolic.Checked)
            {
                network       = NNetwork.HyperbolicNetwork(layers);
                is_hyperbolic = true;
                is_sigmoid    = false;
            }
            if (radioSigmoid.Checked)
            {
                network       = NNetwork.SigmoidNetwork(layers);
                is_hyperbolic = false;
                is_sigmoid    = true;
            }
            if (radioCombined.Checked)
            {
                network       = NNetwork.CombinedNetwork(layers);
                is_hyperbolic = false;
                is_sigmoid    = true;
            }
            bool two_steps   = network.OutputCount() >= 2;
            bool three_steps = network.OutputCount() >= 3;

            checkTrain2.Enabled  = two_steps;
            checkTest2.Enabled   = two_steps;
            checkTrain3.Enabled  = three_steps;
            checkTest3.Enabled   = three_steps;
            groupWeights.Enabled = true;
        }
Пример #3
0
        public void TestDimensions()
        {
            int[]    neurons_in_layers = new int[] { 3, 4, 2, 1 };
            NNetwork network           = NNetwork.SigmoidNetwork(neurons_in_layers);

            Assert.AreEqual(network.LayerCount, neurons_in_layers.Length);
            Assert.AreEqual(network.NeuronsInLayersWithoutBias, neurons_in_layers);
        }
Пример #4
0
        public void TrainPrediction()
        {
            NNetwork       network = NNetwork.SigmoidNetwork(new int[] { 5, 2, 2 });
            NetworkTrainer trainer = new NetworkTrainer(network);

            double[] train_set = new double[] { 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1 };
            trainer.TrainPrediction(train_set);
            //todo
        }
Пример #5
0
        public void TestInputEqualsOutput()
        {
            NNetwork n = NNetwork.SigmoidNetwork(new int[] { 1 });

            n.SetWeightMatrix(new double[][]
            {
                new double[] { 1, 1 }
            });
            n.SetInput(new double[] { 9 });
            Assert.AreEqual(n.GetOutput()[0], 9);
        }
Пример #6
0
        public void CanApplyTrainingForWholeNetwork()
        {
            NNetwork n = NNetwork.SigmoidNetwork(new int[] { 1, 2, 2, 1 });

            n.SetInput(new double[] { 0.3 });
            n.SetAnswers(new double[] { 0.8 });
            n.BackPropagate();
            var output_before = n.GetOutput();

            n.ApplyTraining();
            var output_after = n.GetOutput();

            Assert.AreNotEqual(output_after, output_before);
        }
Пример #7
0
        public void TestWeightMatrix()
        {
            double[]   from_l1 = new double[] { -30, 20, 20, 10, -20, -20 };
            double[]   from_l2 = new double[] { -10, 20, 20 };
            double[][] weights = new double[][]
            {
                from_l1,
                from_l2
            };
            NNetwork n = NNetwork.SigmoidNetwork(new int[] { 2, 2, 1 });

            n.SetWeightMatrix(weights);
            Assert.AreEqual(n.GetWeightMatrix(), weights);
        }
Пример #8
0
        public void DimensionTestCheck()
        {
            NNetwork       network = NNetwork.SigmoidNetwork(new int[] { 2, 4, 3 });
            NetworkTrainer trainer = new NetworkTrainer(network);

            double[][] incorrect_input  = new double[1][] { new double[3] };
            double[][] correct_input    = new double[1][] { new double[2] };
            double[][] incorrect_output = new double[1][] { new double[4] };
            double[][] correct_output   = new double[1][] { new double[3] };
            Assert.Throws(typeof(IncorrectInputDimensionException),
                          () => trainer.TrainClassification(incorrect_input, correct_output));
            Assert.Throws(typeof(IncorrectOutputDimensionException),
                          () => trainer.TrainClassification(correct_input, incorrect_output));
        }
Пример #9
0
        public static NNetwork XorNetwork()
        {
            double[]   from_l1 = new double[] { -30, 20, 20, 10, -20, -20 };
            double[]   from_l2 = new double[] { -10, 20, 20 };
            double[][] weights = new double[][]
            {
                from_l1,
                from_l2
            };
            NNetwork xor_network = NNetwork.SigmoidNetwork(new int[] { 2, 2, 1 });

            xor_network.SetWeightMatrix(weights);
            return(xor_network);
        }
Пример #10
0
        public void TestSimplestConnection()
        {
            NNetwork n = NNetwork.SigmoidNetwork(new int[] { 1, 1 });

            n.SetWeightMatrix(new double[][]
            {
                new double[] { 1, 1 },
                new double[] { 1, 1 }
            });
            n.SetInput(new double[] { 1 });
            var output  = n.GetOutput()[0];
            var desired = 1 / (1 + Math.Pow(Math.E, -2));

            MyAssert.CloseTo(output, desired);
        }
Пример #11
0
        public void TestCostFunctionAccumulation()
        {
            NNetwork       network = NNetwork.SigmoidNetwork(new int[] { 2, 4, 3 });
            NetworkTrainer trainer = new NetworkTrainer(network);

            double[] train_set = new[] { 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1 };
            Assert.Throws(typeof(NoErrorInfoYetException), () => trainer.GetError());
            double error;

            trainer.TrainPrediction(train_set);
            error = trainer.GetError();
            Assert.AreNotEqual(error, 0);
            trainer.TrainPrediction(train_set);
            Assert.AreNotEqual(error, trainer.GetError());
        }
Пример #12
0
        public void TestNetworkSetAnswerAndGetDelta()
        {
            NNetwork n = NNetwork.SigmoidNetwork(new int[] { 2, 3, 2 });

            n.SetInput(new double[] { 0, 0 });
            double[] outputs = n.GetOutput();
            double[] answers = new double[] { 0.1, 0.9 };
            n.SetAnswers(answers);
            n.BackPropagate();
            double[] deltas = n.GetDeltasForLayer(3);
            for (int i = 0; i < answers.Length; i++)
            {
                MyAssert.CloseTo(deltas[i], answers[i] - outputs[i]);
            }
        }
Пример #13
0
        public void TestRandomInit()
        {
            NNetwork n = NNetwork.SigmoidNetwork(new int[] { 2, 3, 2 });

            n.RandomizeWeights(seed: 0);
            var first = n.GetWeightMatrix();

            n.RandomizeWeights(seed: 0);
            var equal = n.GetWeightMatrix();

            Assert.AreEqual(first, equal);
            n.RandomizeWeights(seed: 1);
            var not_equal = n.GetWeightMatrix();

            Assert.AreNotEqual(first, not_equal);
        }
Пример #14
0
        public void TestDerivative()
        {
            //Fails with square error function
            NNetwork n = NNetwork.SigmoidNetwork(new int[] { 2, 2, 1 });

            n.RandomizeWeights(-1, 10);
            Random random = new Random();
            double x;
            double y;
            double z;

            x = random.NextDouble();
            y = random.NextDouble();
            z = some_function(x, y);
            n.SetInput(new double[] { x, y });
            n.SetAnswers(new double[] { z });
            n.BackPropagate();
            double[] ders = n.Derivatives();
            double[] ests = n.Estimation(0.0001);
            for (int i = 0; i < ders.Length; i++)
            {
                MyAssert.CloseTo(ders[i], ests[i], 0.0001);
            }
        }
Пример #15
0
//        public static void Main()
//        {
//            TrainPrediction();
////            Sinus();
////            TestTanhLearningOnSinus();
////            TestTanhDerivative();
//
//        }

        public static void TrainPrediction()
        {
            NNetwork network = NNetwork.SigmoidNetwork(new int[] { 5, 1 });

            network.RandomizeWeights(-1, 20);
            NetworkTrainer trainer = new NetworkTrainer(network);
            List <double>  tr      = new List <double>();

            for (double i = 0; i <= 1; i = i + 0.05)
            {
                tr.Add(i);
            }
            double[] train_set = tr.ToArray();//new double[] { 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1 };
            double   error     = 1;
            double   delta     = 1;
            int      j         = 0;

            for (; error > 0.01 && !(delta <= 0.00001) || j == 1; j++)
            {
                trainer.TrainPrediction(train_set, 0.0001, 0.2);
                double new_cost = trainer.GetError();
                delta = error - new_cost;
                error = new_cost;
            }
            Console.Out.WriteLine(j + ": " + error);
            for (double i = 0; i <= 0.5; i = i + 0.05)
            {
                network.SetInput(new double[] { i + 0.0, i + 0.1, i + 0.2, i + 0.3, i + 0.4 });
                Show(new double[]
                {
                    i + 0.5,
                    network.GetOutput()[0],
//                        network.GetOutput()[1]
                });
            }
        }
Пример #16
0
        public void CanNotGetDeltasForFirstLayer()
        {
            NNetwork n = NNetwork.SigmoidNetwork(new int[] { 2, 3, 2 });

            Assert.Throws(typeof(CannotGetDeltasForThisLayer), () => n.GetDeltasForLayer(1));
        }
Пример #17
0
 public void IllegalDimensionsTest()
 {
     Assert.Throws(typeof(InvalidDimensionException), () => NNetwork.SigmoidNetwork(new int[] {}));
     Assert.Throws(typeof(InvalidDimensionException), () => NNetwork.SigmoidNetwork(new int[] { 1, 0 }));
 }
Пример #18
0
 public void TestNetworkCreation()
 {
     NNetwork network = NNetwork.SigmoidNetwork(new int[] { 1, 1, 1 });
 }
Пример #19
0
 public void CreateTrainer()
 {
     NNetwork       network = NNetwork.SigmoidNetwork(new int[] { 1, 2, 1 });
     NetworkTrainer trainer = new NetworkTrainer(network);
 }