public void Test_Train_SimpleRow()
        {
            var network = new Network(3, 4, 2);

            Helper_InitWeights(network);

            var trainer = new BackPropagationTrainer(network);
            var rows    = new[] { new BackPropagationTrainingRow(new [] { 1.0, -2.0, 3.0 }, new [] { 0.1234, 0.8766 }) };

            trainer.Train(rows, 0.5, 10000);

            const double tolerance = 0.00001;

            Assert.AreEqual(0.07770, network.HiddenNodes[0].Incoming[0].Weight, tolerance);
            Assert.AreEqual(0.08118, network.HiddenNodes[1].Incoming[0].Weight, tolerance);
            Assert.AreEqual(0.08441, network.HiddenNodes[2].Incoming[0].Weight, tolerance);

            network.Invalidate();
            for (var i = 0; i < rows[0].Inputs.Length; i++)
            {
                network.InputNodes[i].Value = rows[0].Inputs[i];
            }
            Assert.AreEqual(rows[0].Outputs[0], network.OutputNodes[0].Value, tolerance);
            Assert.AreEqual(rows[0].Outputs[1], network.OutputNodes[1].Value, tolerance);
        }
        public void TrainXor()
        {
            NeuralNetworkBuilder builder = new NeuralNetworkBuilder();
            NeuralNetwork        network = builder.CreateNew()
                                           .AddInputLayer(2, ActivationFunctions.Sigmoid, false)
                                           .AddHiddenLayer(2, ActivationFunctions.Sigmoid, false)
                                           .AddOutputLayer(1, ActivationFunctions.Sigmoid)
                                           .GetNetwork();
            BackPropagationTrainer trainer = new BackPropagationTrainer(network);

            TrainingData[] data = new TrainingData[]
            {
                new TrainingData(new double[] { 1, 1 }, new double[] { 0 }),
                new TrainingData(new double[] { 1, 0 }, new double[] { 1 }),
                new TrainingData(new double[] { 0, 1 }, new double[] { 1 }),
                new TrainingData(new double[] { 0, 0 }, new double[] { 0 }),
            };
            var result = trainer.Train(data, 100000, 0.7, 0.0, 0.005);

            string csv = result.Errors.ToCsvString();

            Assert.IsTrue(result.IsSuccessful, "could not traing against expected error");

            double[] result1 = network.Run(new double[] { 1.0, 1.0 });
            double[] result2 = network.Run(new double[] { 1.0, 0.0 });
            double[] result3 = network.Run(new double[] { 0.0, 1.0 });
            double[] result4 = network.Run(new double[] { 0.0, 0.0 });

            Assert.IsTrue(result1[0].IsEqual(0, 0.0005));
            Assert.IsTrue(result2[0].IsEqual(1, 0.0005));
            Assert.IsTrue(result3[0].IsEqual(1, 0.0005));
            Assert.IsTrue(result4[0].IsEqual(0, 0.0005));
        }
Ejemplo n.º 3
0
        public static Network CreateAndTrainNetwork(Scenario scenario, double adjust, int rounds)
        {
            var network = CreateNetwork(scenario);
            var trainer = new BackPropagationTrainer(network);
            var rows    = GetTrainingRows(scenario);

            trainer.Train(rows, adjust, rounds);
            return(network);
        }
Ejemplo n.º 4
0
 private async void Train()
 {
     if (_trainingRows.Count > 0)
     {
         using (BusyScope())
         {
             await Task.Factory.StartNew(() =>
             {
                 var trainer = new BackPropagationTrainer(_network);
                 trainer.Train(_trainingRows, 0.5, 50);
                 IsolatedStorageHelper.Save("weights.xml", _network.GetWeights());
             });
         }
     }
 }
Ejemplo n.º 5
0
        public void Test_Train_SimpleRow()
        {
            var network = new Network(3, 4, 2);

              Helper_InitWeights(network);

              var trainer = new BackPropagationTrainer(network);
              var rows = new[] { new BackPropagationTrainingRow (new [] { 1.0, -2.0, 3.0 }, new [] { 0.1234, 0.8766 }) };
              trainer.Train(rows, 0.5, 10000);

              const double tolerance = 0.00001;
              Assert.AreEqual(0.07770, network.HiddenNodes[0].Incoming[0].Weight, tolerance);
              Assert.AreEqual(0.08118, network.HiddenNodes[1].Incoming[0].Weight, tolerance);
              Assert.AreEqual(0.08441, network.HiddenNodes[2].Incoming[0].Weight, tolerance);

              network.Invalidate();
              for (var i = 0; i < rows[0].Inputs.Length; i++)
              {
            network.InputNodes[i].Value = rows[0].Inputs[i];
              }
              Assert.AreEqual(rows[0].Outputs[0], network.OutputNodes[0].Value, tolerance);
              Assert.AreEqual(rows[0].Outputs[1], network.OutputNodes[1].Value, tolerance);
        }