Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            // create a neural network, without using a factory
            var svm = new SupportVectorMachine(2, false); // 2 input, & false for classification
            // create training data
            IMLDataSet trainingSet = new BasicMLDataSet(ClassificationInput, ClassificationIdeal);
            // train the SVM
            IMLTrain train = new SVMSearchTrain(svm, trainingSet);
            int      epoch = 1;

            do
            {
                train.Iteration();
                Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
                epoch++;
            } while (train.Error > 0.01);
            // test the SVM
            Console.WriteLine(@"SVM Results:");
            foreach (IMLDataPair pair in trainingSet)
            {
                IMLData output = svm.Compute(pair.Input);
                Console.WriteLine(pair.Input[0]
                                  + @", actual=" + output[0] + @",ideal=" + pair.Ideal[0]);
            }
            Console.WriteLine("Done");
        }
Ejemplo n.º 2
0
        public void Execute(IExampleInterface app)
        {
            // create a neural network, without using a factory
            var svm = new SupportVectorMachine(1, true); // 1 input, & true for regression

            // create training data
            IMLDataSet trainingSet = new BasicMLDataSet(RegressionInput, RegressionIdeal);

            // train the SVM
            IMLTrain train = new SVMSearchTrain(svm, trainingSet);

            int epoch = 1;

            do
            {
                train.Iteration();
                Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
                epoch++;
            } while (train.Error > 0.01);

            // test the SVM
            Console.WriteLine(@"SVM Results:");
            foreach (IMLDataPair pair in trainingSet)
            {
                IMLData output = svm.Compute(pair.Input);
                Console.WriteLine(pair.Input[0]
                                  + @", actual=" + output[0] + @",ideal=" + pair.Ideal[0]);
            }
        }
Ejemplo n.º 3
0
            public static SupportVectorMachine SVMSearch(SupportVectorMachine anetwork, IMLDataSet training)
            {
                SVMSearchTrain       bestsearch = new SVMSearchTrain(anetwork, training);
                StopTrainingStrategy stop       = new StopTrainingStrategy(0.00000000001, 1);

                bestsearch.AddStrategy(stop);
                while (bestsearch.IterationNumber < 30 && !stop.ShouldStop())
                {
                    bestsearch.Iteration();
                    Console.WriteLine("Iteration #" + bestsearch.IterationNumber + " Error :" + bestsearch.Error);
                }

                bestsearch.FinishTraining();

                return(anetwork);
            }
        /// <summary>
        /// Create a SVM trainer.
        /// </summary>
        ///
        /// <param name="method">The method to use.</param>
        /// <param name="training">The training data to use.</param>
        /// <param name="argsStr">The arguments to use.</param>
        /// <returns>The newly created trainer.</returns>
        public IMLTrain Create(IMLMethod method,
                               IMLDataSet training, String argsStr)
        {
            if (!(method is SupportVectorMachine))
            {
                throw new EncogError(
                          "SVM Train training cannot be used on a method of type: "
                          + method.GetType().FullName);
            }

            IDictionary <String, String> args = ArchitectureParse.ParseParams(argsStr);

            new ParamsHolder(args);

            var    holder     = new ParamsHolder(args);
            double gammaStart = holder.GetDouble(
                PropertyGamma1, false,
                SVMSearchTrain.DefaultGammaBegin);
            double cStart = holder.GetDouble(PropertyC1,
                                             false, SVMSearchTrain.DefaultConstBegin);
            double gammaStop = holder.GetDouble(
                PropertyGamma2, false,
                SVMSearchTrain.DefaultGammaEnd);
            double cStop = holder.GetDouble(PropertyC2,
                                            false, SVMSearchTrain.DefaultConstEnd);
            double gammaStep = holder.GetDouble(
                PropertyGammaStep, false,
                SVMSearchTrain.DefaultGammaStep);
            double cStep = holder.GetDouble(PropertyCStep,
                                            false, SVMSearchTrain.DefaultConstStep);

            var result = new SVMSearchTrain((SupportVectorMachine)method, training)
            {
                GammaBegin = gammaStart,
                GammaEnd   = gammaStop,
                GammaStep  = gammaStep,
                ConstBegin = cStart,
                ConstEnd   = cStop,
                ConstStep  = cStep
            };

            return(result);
        }
Ejemplo n.º 5
0
        public void train(double[][] trainingInput, double[][] idealOutput)
        {
            // create training data
            trainingSet = new BasicMLDataSet(trainingInput, idealOutput);

            // train the neural network
            IMLTrain train = new SVMSearchTrain(network, trainingSet);

            int epoch = 1;

            do
            {
                train.Iteration();
                Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
                epoch++;
            } while (train.Error > 0.01);

            train.FinishTraining();
        }
Ejemplo n.º 6
0
        public void Learn()
        {
            var xorInput = Normalize(XorInputOriginal[1][0]);

            var classificationIdeal = Classify(XorIdealOriginal[1][0]);

            var        svm         = new SupportVectorMachine(2, false);
            IMLDataSet trainingSet = new BasicMLDataSet(xorInput, classificationIdeal);
            IMLTrain   train       = new SVMSearchTrain(svm, trainingSet);

            var epoch = 1;

            do
            {
                train.Iteration();
                Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
                epoch++;
            } while (train.Error > 0.01);

            Console.WriteLine(@"SVM Results:");


            var inputString  = new StringBuilder();
            var idealString  = new StringBuilder();
            var outputString = new StringBuilder();


            foreach (var pair in trainingSet)
            {
                var output = svm.Compute(pair.Input);
                outputString.Append(_alphas[(int)output[0]]);
                inputString.Append(Denormalize(pair.Input));
                idealString.Append(_alphas[(int)pair.Ideal[0]]);
            }
            Console.WriteLine("input=" + inputString + @" , " + @" , actual=" + outputString + @" , ideal=" + idealString);

            Console.WriteLine("Done");
        }