コード例 #1
0
 public static void TestTanhLearningOnSinus()
 {
     NNetwork network = NNetwork.HyperbolicNetwork(new int[] { 1, 2, 1 });
     network.RandomizeWeights(1, 2);
     NetworkTrainer trainer = new NetworkTrainer(network);
     double[][] inputs = SinusTrainSet()[0];
     double[][] outputs = SinusTrainSet()[1];
     double error = 1;
     double delta = 1;
     int j = 0;
     for (; error > 0.01 && !(delta <= 0.000001) || j == 1; j++)
     {
         trainer.TrainClassification(inputs, outputs);
         double new_cost = trainer.GetError();
         delta = error - new_cost;
         error = new_cost;
     }
     double[][] input_test = SinusTrainSet(20)[0];
     double[][] output_test = SinusTrainSet(20)[1];
     trainer.IsLearning = false;
     trainer.TrainClassification(input_test, output_test);
     error = trainer.GetError();
     Console.Out.WriteLine(error);
     for (int i = 0; i < input_test.Length; i++ )
     {
         network.SetInput(input_test[i]);
         Show(new []{input_test[i][0], network.GetOutput()[0], Math.Sin(input_test[i][0])});
     }
 }
コード例 #2
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
 }
コード例 #3
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());
 }
コード例 #4
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));
 }
コード例 #5
0
 public void TestHyperbolicClassification()
 {
     NNetwork network = NNetwork.HyperbolicNetwork(new int[] { 1, 2, 1 });
     network.RandomizeWeights(1, 2);
     NetworkTrainer trainer = new NetworkTrainer(network);
     double[][] inputs = SinusTrainSet()[0];
     double[][] outputs = SinusTrainSet()[1];
     double error = 1;
     double delta = 1;
     int j = 0;
     for (; error > 0.01 && !(delta <= 0.000001) || j == 1; j++)
     {
         trainer.TrainClassification(inputs, outputs);
         double new_cost = trainer.GetError();
         delta = error - new_cost;
         error = new_cost;
     }
     double[][] input_test = SinusTrainSet(20)[0];
     double[][] output_test = SinusTrainSet(20)[1];
     trainer.IsLearning = false;
     trainer.TrainClassification(input_test, output_test);
     error = trainer.GetError();
     Assert.Less(error, 0.55);
 }
コード例 #6
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]
             });
     }
 }
コード例 #7
0
 public void CreateTrainer()
 {
     NNetwork network = NNetwork.SigmoidNetwork(new int[]{1, 2, 1});
     NetworkTrainer trainer = new NetworkTrainer(network);
 }
コード例 #8
0
 private double GetControlError(NetworkTrainer networkTrainer, double lambda, double alpha)
 {
     networkTrainer.IsLearning = false;
     networkTrainer.TrainPrediction(test_data, lambda, alpha);
     double error = Math.Abs(trainer.GetError());
     networkTrainer.IsLearning = true;
     return error;
 }
コード例 #9
0
 private void buttonTrain_Click(object sender, EventArgs e)
 {
     groupPlotting.Enabled = false;
     trainer = new NetworkTrainer(network);
     double lambda = double.Parse(textLambda.Text);
     double alpha = double.Parse(textAlpha.Text);
     AssignData();
     double error = 1;
     double delta = 1;
     double required_error = double.Parse(textErrorStop.Text);
     double required_delta = double.Parse(textDeltaStop.Text);
     int time_limit = int.Parse(textStopTime.Text)*1000;
     int j = 0;
     Stopwatch stopwatch = new Stopwatch();
     stopwatch.Reset();
     stopwatch.Start();
     //            while(error > required_error && (delta >= required_delta) || j == 1)
     while (error > required_error && (delta >= 0) || j == 1)
     {
         trainer.TrainPrediction(train_data, lambda, alpha);
         double new_error = Math.Abs(trainer.GetError());
         delta = error - new_error;
         error = new_error;
         j++;
         if (stopwatch.ElapsedMilliseconds > time_limit) break;
     }
     textError.Text = Math.Round(error, 5).ToString();
     Double control_error = GetControlError(trainer, lambda, alpha);
     textControlError.Text = Math.Round(control_error, 5).ToString();
     textTimes.Text = j.ToString();
     groupPlotting.Enabled = true;
 }