/// <summary>
        /// Processes a double array of data of input and a second array of data for ideals
        /// you must input the input and output size.
        /// this typically builds a supervised IMLDatapair, which you must add to a IMLDataset.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="ideal">The ideal.</param>
        /// <param name="_inputWindow">The _input window.</param>
        /// <param name="_predictWindow">The _predict window.</param>
        /// <returns></returns>
        public static IMLDataPair ProcessPairs(double[] data, double[] ideal, int _inputWindow, int _predictWindow)
        {
            IMLDataSet result = new BasicMLDataSet();

            for (int i = 0; i < data.Length; i++)
            {
                IMLData inputData = new BasicMLData(_inputWindow);
                IMLData idealData = new BasicMLData(_predictWindow);
                int     index     = i;
                // handle input window
                for (int j = 0; j < _inputWindow; j++)
                {
                    inputData[j] = data[index++];
                }
                index = 0;
                // handle predict window
                for (int j = 0; j < _predictWindow; j++)
                {
                    idealData[j] = ideal[index++];
                }
                IMLDataPair pair = new BasicMLDataPair(inputData, idealData);
                return(pair);
            }
            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Program entry point.
        /// </summary>
        /// <param name="app">Holds arguments and other info.</param>
        public void Execute(IExampleInterface app)
        {
            IMLDataSet trainingSet = new BasicMLDataSet(XORInput, XORIdeal);

            FlatNetwork network = CreateNetwork();

            Console.WriteLine(@"Starting Weights:");
            DisplayWeights(network);
            Evaluate(network, trainingSet);

            var train = new TrainFlatNetworkResilient(
                network, trainingSet);

            for (int iteration = 1; iteration <= ITERATIONS; iteration++)
            {
                train.Iteration();

                Console.WriteLine();
                Console.WriteLine(@"*** Iteration #" + iteration);
                Console.WriteLine(@"Error: " + train.Error);
                Evaluate(network, trainingSet);

                Console.WriteLine(@"LastGrad:"
                                  + FormatArray(train.LastGradient));
                Console.WriteLine(@"Updates :"
                                  + FormatArray(train.UpdateValues));

                DisplayWeights(network);
            }
        }
        /// <summary>
        /// Processes the specified double serie into an IMLDataset.
        /// To use this method, you must provide a formated double array with the input data and the ideal data in another double array.
        /// The number of points in the input window makes the input array , and the predict window will create the array used in ideal.
        /// This method will use ALL the data inputs and ideals you have provided.
        /// </summary>
        /// <param name="datainput">The datainput.</param>
        /// <param name="ideals">The ideals.</param>
        /// <param name="_inputWindow">The _input window.</param>
        /// <param name="_predictWindow">The _predict window.</param>
        /// <returns></returns>
        public static IMLDataSet ProcessDoubleSerieIntoIMLDataset(List <double> datainput, List <double> ideals, int _inputWindow, int _predictWindow)
        {
            IMLDataSet result = new BasicMLDataSet();
            //int count = 0;
            ////lets check if there is a modulo , if so we move forward in the List of doubles in inputs.This is just a check
            ////as the data of inputs should be able to fill without having .
            //while (datainput.Count % _inputWindow !=0)
            //{
            //    count++;
            //}
            IMLData inputData = new BasicMLData(_inputWindow);
            IMLData idealData = new BasicMLData(_predictWindow);

            foreach (double d in datainput)
            {
                // handle input window
                for (int j = 0; j < _inputWindow; j++)
                {
                    inputData[j] = d;
                }
            }
            foreach (double ideal in ideals)
            {
                // handle predict window
                for (int j = 0; j < _predictWindow; j++)
                {
                    idealData[j] = ideal;
                }
            }
            IMLDataPair pair = new BasicMLDataPair(inputData, idealData);

            result.Add(pair);
            return(result);
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            var network = new BasicNetwork();

            network.AddLayer(new BasicLayer(null, true, 4));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 4));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 5));
            network.Structure.FinalizeStructure();
            network.Reset();
            IMLDataSet trainingSet = new BasicMLDataSet(SensoriInput, AttuatoriOutput);
            IMLTrain   train       = new ResilientPropagation(network, trainingSet);
            int        epoch       = 1;

            do
            {
                /* Avvia la redistribuzione dei pesi */
                train.Iteration();
                Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
                epoch++;
            } while (train.Error > 0.001); /* Itera finche' non viene raggiunto un errore tollerabile */

            /* Test la MLP */
            Console.WriteLine("\r\n+------------------------------------+");
            Console.WriteLine("|Neural Network Results:             |");
            Console.WriteLine("+------------------------------------+");
            foreach (IMLDataPair pair in trainingSet)
            {
                IMLData output = network.Compute(pair.Input);
                Console.WriteLine("Input:" + pair.Input[0] + " - " + pair.Input[1] + " - " + pair.Input[2] + " - " + pair.Input[3]
                                  + "\tactual=" + Math.Round(output[0], 2) + " - " + Math.Round(output[1], 2) + " - " + Math.Round(output[2], 2) + " - " + Math.Round(output[3], 2) + " - " + Math.Round(output[4], 2)
                                  + "\tideal=" + pair.Ideal[0] + " - " + pair.Ideal[1] + " - " + pair.Ideal[2] + " - " + pair.Ideal[3] + " - " + pair.Ideal[4]);
            }
            Console.Read();
        }
Ejemplo n.º 5
0
        public void TestSOM()
        {
            // create the training set
            IMLDataSet training = new BasicMLDataSet(
                SOMInput, null);

            // Create the neural network.
            var network = new SOMNetwork(4, 2)
            {
                Weights = new Matrix(MatrixArray)
            };

            var train = new BasicTrainSOM(network, 0.4,
                                          training, new NeighborhoodSingle())
            {
                ForceWinner = true
            };
            int iteration = 0;

            for (iteration = 0; iteration <= 100; iteration++)
            {
                train.Iteration();
            }

            IMLData data1 = new BasicMLData(
                SOMInput[0]);
            IMLData data2 = new BasicMLData(
                SOMInput[1]);

            int result1 = network.Classify(data1);
            int result2 = network.Classify(data2);

            Assert.IsTrue(result1 != result2);
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            //create a neural network withtout using a factory
            var network = new BasicNetwork();
            network.AddLayer(new BasicLayer(null, true, 2));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 2));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 1));

            network.Structure.FinalizeStructure();
            network.Reset();

            IMLDataSet trainingSet = new BasicMLDataSet(XORInput, XORIdeal);
            IMLTrain train = new ResilientPropagation(network, trainingSet);

            int epoch = 1;
            do
            {
                train.Iteration();
                Console.WriteLine($"Epoch #{epoch} Error: {train.Error}");
                epoch++;
            } while (train.Error > 0.01);
            train.FinishTraining();

            Console.WriteLine("Neural Network Results:");
            foreach (IMLDataPair iPair in trainingSet)
            {
                IMLData output = network.Compute(iPair.Input);
                Console.WriteLine($"{iPair.Input[0]}, {iPair.Input[0]}, actual={output[0]}, ideal={iPair.Ideal[0]}");
            }

            EncogFramework.Instance.Shutdown();

            Console.ReadKey();
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Load a CSV file into a memory dataset.
        /// </summary>
        ///
        /// <param name="format">The CSV format to use.</param>
        /// <param name="filename">The filename to load.</param>
        /// <param name="headers">True if there is a header line.</param>
        /// <param name="inputSize">The input size.  Input always comes first in a file.</param>
        /// <param name="idealSize">The ideal size, 0 for unsupervised.</param>
        /// <returns>A NeuralDataSet that holds the contents of the CSV file.</returns>
        public static IMLDataSet LoadCSVTOMemory(CSVFormat format, String filename,
                                                 bool headers, int inputSize, int idealSize)
        {
            var result = new BasicMLDataSet();
            var csv    = new ReadCSV(filename, headers, format);

            while (csv.Next())
            {
                BasicMLData ideal = null;
                int         index = 0;

                var input = new BasicMLData(inputSize);
                for (int i = 0; i < inputSize; i++)
                {
                    double d = csv.GetDouble(index++);
                    input[i] = d;
                }

                if (idealSize > 0)
                {
                    ideal = new BasicMLData(idealSize);
                    for (int i = 0; i < idealSize; i++)
                    {
                        double d = csv.GetDouble(index++);
                        ideal[i] = d;
                    }
                }

                IMLDataPair pair = new BasicMLDataPair(input, ideal);
                result.Add(pair);
            }

            return(result);
        }
Ejemplo n.º 8
0
        public void Process(String methodName, String methodArchitecture, String trainerName, String trainerArgs,
                            int outputNeurons)
        {
            // first, create the machine learning method
            var       methodFactory = new MLMethodFactory();
            IMLMethod method        = methodFactory.Create(methodName, methodArchitecture, 2, outputNeurons);

            // second, create the data set
            IMLDataSet dataSet = new BasicMLDataSet(XORInput, XORIdeal);

            // third, create the trainer
            var      trainFactory = new MLTrainFactory();
            IMLTrain train        = trainFactory.Create(method, dataSet, trainerName, trainerArgs);

            // reset if improve is less than 1% over 5 cycles
            if (method is IMLResettable && !(train is ManhattanPropagation))
            {
                train.AddStrategy(new RequiredImprovementStrategy(500));
            }

            // fourth, train and evaluate.
            EncogUtility.TrainToError(train, 0.01);
            method = train.Method;
            EncogUtility.Evaluate((IMLRegression)method, dataSet);

            // finally, write out what we did
            Console.WriteLine(@"Machine Learning Type: " + methodName);
            Console.WriteLine(@"Machine Learning Architecture: " + methodArchitecture);

            Console.WriteLine(@"Training Method: " + trainerName);
            Console.WriteLine(@"Training Args: " + trainerArgs);
        }
Ejemplo n.º 9
0
        private static void ZrobKlasyfikacje()
        {
            DaneKlasyfikacja doNauki = new DaneKlasyfikacja();

            doNauki.Wczytaj(sciezkaKlasyfikacjaTreningowe);

            BasicNetwork siec        = UtworzSiecDoKlasyfikacji();
            IMLDataSet   dataTrening = UczSiec(siec, doNauki);

            // testuje siec na danych treningowych
            for (int i = 0; i < dataTrening.Count; i++)
            {
                IMLData wynik = siec.Compute(dataTrening[i].Input);
                doNauki.klasyWy.Add(wynik[0]);
            }

            doNauki.EksportujDoPliku(sciezkaKlasyfikacjaTreningoweWyniki);

            // testuje siec na danych testowych
            DaneKlasyfikacja doTestow = new DaneKlasyfikacja();

            doTestow.Wczytaj(sciezkaKlasyfikacjaTestowe);
            IMLDataSet dataTest = new BasicMLDataSet(doTestow.punkty.ToArray(), doTestow.klasyWej.ToArray());

            for (int i = 0; i < dataTest.Count; i++)
            {
                IMLData wynik = siec.Compute(dataTest[i].Input);
                doTestow.klasyWy.Add(wynik[0]);
            }
            doTestow.EksportujDoPliku(sciezkaKlasyfikacjaTestoweWyniki);
        }
Ejemplo n.º 10
0
        public void TestK2Structure()
        {
            String[] labels = { "available", "not" };

            IMLDataSet      data    = new BasicMLDataSet(DATA, null);
            BayesianNetwork network = new BayesianNetwork();
            BayesianEvent   x1      = network.CreateEvent("x1", labels);
            BayesianEvent   x2      = network.CreateEvent("x2", labels);
            BayesianEvent   x3      = network.CreateEvent("x3", labels);

            network.FinalizeStructure();
            TrainBayesian train = new TrainBayesian(network, data, 10);

            train.InitNetwork = BayesianInit.InitEmpty;
            while (!train.TrainingDone)
            {
                train.Iteration();
            }
            train.Iteration();
            Assert.IsTrue(x1.Parents.Count == 0);
            Assert.IsTrue(x2.Parents.Count == 1);
            Assert.IsTrue(x3.Parents.Count == 1);
            Assert.IsTrue(x2.Parents.Contains(x1));
            Assert.IsTrue(x3.Parents.Contains(x2));
            Assert.AreEqual(0.714, network.GetEvent("x2").Table.FindLine(1, new int[] { 1 }).Probability, 0.001);
        }
        public IMLDataSet GetDataSet(DataTable dt)
        {
            inputNeurons = dt.AsEnumerable().Select(x => new[] {
                (double)x.Field <int>(0),
                (double)x.Field <int>(1),
                bid[x.Field <string>(2)],
                bid[x.Field <string>(3)],
                bid[x.Field <string>(4)],
                rank[x.Field <string>(5)],
                suit[x.Field <string>(6)],
                rank[x.Field <string>(7)],
                suit[x.Field <string>(8)],
                rank[x.Field <string>(9)],
                suit[x.Field <string>(10)],
                rank[x.Field <string>(11)],
                suit[x.Field <string>(12)],
                rank[x.Field <string>(13)],
                suit[x.Field <string>(14)],
                rank[x.Field <string>(15)],
                suit[x.Field <string>(16)]
            }).ToArray();
            outputNeurons = dt.AsEnumerable().Select(x => new[] {
                bid[x.Field <string>(17)],
                suit[x.Field <string>(18)]
            }).ToArray();
            IMLDataSet trainingSet = new BasicMLDataSet(inputNeurons, outputNeurons);

            return(trainingSet);
        }
Ejemplo n.º 12
0
        private static void ZrobRegresje()
        {
            DaneRegresja doNauki = new DaneRegresja();

            doNauki.Wczytaj(sciezkaRegresjaTreningowe);

            BasicNetwork siec        = UtworzSiecDoRegresji();
            IMLDataSet   dataTrening = UczSiec(siec, doNauki);

            // testuje siec na danych treningowych
            for (int i = 0; i < dataTrening.Count; i++)
            {
                IMLData wynik = siec.Compute(dataTrening[i].Input);
                doNauki.otrzymaneY.Add(wynik[0]);
            }

            doNauki.EksportujDoPliku(sciezkaRegresjaTreningoweWyniki);

            // testuje siec na danych testowych
            DaneRegresja doTestow = new DaneRegresja();

            doTestow.Wczytaj(sciezkaRegresjaTestowe);
            IMLDataSet dataTest = new BasicMLDataSet(doTestow.wejscioweX.ToArray(), doTestow.oczekiwaneY.ToArray());

            for (int i = 0; i < dataTest.Count; i++)
            {
                IMLData wynik = siec.Compute(dataTest[i].Input);
                doTestow.otrzymaneY.Add(wynik[0]);
            }
            doTestow.EksportujDoPliku(sciezkaRegresjaTestoweWyniki);
        }
Ejemplo n.º 13
0
        public static long BenchmarkEncog(double[][] input, double[][] output)
        {
            var network = new BasicNetwork();

            network.AddLayer(new BasicLayer(null, true,
                                            input[0].Length));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), true,
                                            HIDDEN_COUNT));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), false,
                                            output[0].Length));
            network.Structure.FinalizeStructure();
            network.Reset(23); // constant seed for repeatable testing

            IMLDataSet trainingSet = new BasicMLDataSet(input, output);

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

            var sw = new Stopwatch();

            sw.Start();
            // run epoch of learning procedure
            for (int i = 0; i < ITERATIONS; i++)
            {
                train.Iteration();
            }
            sw.Stop();

            return(sw.ElapsedMilliseconds);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Trains a random trainer.
        /// </summary>
        /// <param name="inputs">The inputs.</param>
        /// <param name="predictWindow">The predict window.</param>
        public static void RandomTrainerMethod(int inputs, int predictWindow)
        {
            double[] firstinput = MakeInputs(inputs);
            double[] SecondInput = MakeInputs(inputs);
            double[] ThirdInputs = MakeInputs(inputs);
            double[] FourthInputs = MakeInputs(inputs);
            var pair = SuperUtilsTrainer.ProcessPairs(firstinput, FourthInputs, inputs, predictWindow);
            var pair2 = SuperUtilsTrainer.ProcessPairs(SecondInput, FourthInputs, inputs, predictWindow);
            var pair3 = SuperUtilsTrainer.ProcessPairs(ThirdInputs, FourthInputs, inputs, predictWindow);
            var pair4 = SuperUtilsTrainer.ProcessPairs(FourthInputs, FourthInputs, inputs, predictWindow);
            BasicMLDataSet SuperSet = new BasicMLDataSet();
            SuperSet.Add(pair);
            SuperSet.Add(pair2);
            SuperSet.Add(pair3);
            SuperSet.Add(pair4);

            SupportVectorMachine machine = Create(SuperSet, inputs);
            SVMTrain train = new SVMTrain(machine, SuperSet);

            ///  var network = (BasicNetwork)CreateEval.CreateElmanNetwork(SuperSet.InputSize, SuperSet.IdealSize);
            //double error = CreateEval.TrainNetworks(machine, SuperSet);

            TrainSVM(train, machine);

            //Lets create an evaluation.
            // Console.WriteLine(@"Last error rate on random trainer:" + error);
        }
Ejemplo n.º 15
0
        public static double EvaluateNetworks(BasicNetwork network, BasicMLDataSet set)
        {
            int count = 0;
            int correct = 0;
            foreach (IMLDataPair pair in set)
            {
                IMLData input = pair.Input;
                IMLData actualData = pair.Ideal;
                IMLData predictData = network.Compute(input);

                double actual = actualData[0];
                double predict = predictData[0];
                double diff = Math.Abs(predict - actual);

               Direction  actualDirection = DetermineDirection(actual);
               Direction predictDirection = DetermineDirection(predict);

                if (actualDirection == predictDirection)
                    correct++;
                count++;
                Console.WriteLine(@"Number" + @"count" + @": actual=" + Format.FormatDouble(actual, 4) + @"(" + actualDirection + @")"
                                  + @",predict=" + Format.FormatDouble(predict, 4) + @"(" + predictDirection + @")" + @",diff=" + diff);
               
            }
            double percent = correct / (double)count;
            Console.WriteLine(@"Direction correct:" + correct + @"/" + count);
            Console.WriteLine(@"Directional Accuracy:"
                              + Format.FormatPercent(percent));

            return percent;
        }
Ejemplo n.º 16
0
        public BasicMLDataSet Convert(List <StockQuote> input)
        {
            var dataset = new BasicMLDataSet();
            var list    = input as List <StockQuote>;

            for (var i = 2; i < list?.Count; ++i)
            {
                var    openChange  = (list[i - 1].Open - list[i - 2].Open) / list[i - 2].Open;
                var    highChange  = (list[i - 1].High - list[i - 2].High) / list[i - 2].High;
                var    lowChange   = (list[i - 1].Low - list[i - 2].Low) / list[i - 2].Low;
                var    closeChange = (list[i - 1].Close - list[i - 2].Close) / list[i - 2].Close;
                double avgVolatilityChange;
                if ((list[i - 2].High - list[i - 2].Low) == 0)
                {
                    avgVolatilityChange = 0.0;
                }
                else
                {
                    avgVolatilityChange = ((list[i - 1].High - list[i - 1].Low) - (list[i - 2].High - list[i - 2].Low)) / (list[i - 2].High - list[i - 2].Low);
                }

                //var volChange = list[i - 1].Close - list[i - 2].Close / list[i - 2].Close;
                var inputQuote = new BasicMLData(new[] { openChange, highChange, lowChange, closeChange, avgVolatilityChange });
                var expValue   = (list[i - 1].High - list[i - 1].Low) == 0.0
                    ? 0.0
                    : ((list[i].High - list[i].Low) - (list[i - 1].High - list[i - 1].Low)) /
                                 (list[i - 1].High - list[i - 1].Low);
                var expected = new BasicMLData(new double[] { expValue });
                dataset.Add(inputQuote, expected);
            }

            return(dataset);
        }
Ejemplo n.º 17
0
        private void BtnEntrenar_Click(object sender, EventArgs e)
        {
            this.con.Open();

            var redN = new BasicNetwork();

            redN.AddLayer(new BasicLayer(null, false, imagen0.Height * imagen0.Width));
            redN.AddLayer(new BasicLayer(new ActivationLinear(), false, 2));
            redN.AddLayer(new BasicLayer(new ActivationLinear(), true, 1));
            redN.Structure.FinalizeStructure();
            redN.Reset();

            IMLDataSet Set = new BasicMLDataSet(E, IDEAL);


            IMLTrain train = new ResilientPropagation(redN, Set);


            do
            {
                CicloE++;
                train.Iteration();
                cadena.Add(train.Error);
                cadena2.Add(CicloE);
            }while (train.Error > 0.01);

            timer1.Enabled = true;
            timer1.Start();
            char i1;
            char i2;

            i1 = txtNombre.Text.ToUpper()[0];
            i2 = txtApellido.Text.ToUpper()[0];
            EncogDirectoryPersistence.SaveObject(new FileInfo(i1.ToString() + i2.ToString() + ".txt"), redN);
        }
Ejemplo n.º 18
0
        public static long BenchmarkEncog(double[][] input, double[][] output)
        {
            BasicNetwork network = new BasicNetwork();
            network.AddLayer(new BasicLayer(null, true,
                    input[0].Length));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), true,
                    HIDDEN_COUNT));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), false,
                    output[0].Length));
            network.Structure.FinalizeStructure();
            network.Reset();

            IMLDataSet trainingSet = new BasicMLDataSet(input, output);

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

            Stopwatch sw = new Stopwatch();
            sw.Start();
            // run epoch of learning procedure
            for (int i = 0; i < ITERATIONS; i++)
            {
                train.Iteration();
            }
            sw.Stop();

            return sw.ElapsedMilliseconds;
        }
Ejemplo n.º 19
0
        public void Run()
        {
            //Se crea la red neuronal con sus respectivas capas
            var network = new BasicNetwork();

            network.AddLayer(new BasicLayer(null, true, 2));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 3));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 1));
            network.Structure.FinalizeStructure();
            network.Reset();

            //Crear el conjunto de entrenamiento
            IMLDataSet conjuntoEntrenamiento = new BasicMLDataSet(entradas, salidas);

            //Entrenar
            IMLTrain train = new ResilientPropagation(network, conjuntoEntrenamiento);

            int epoch = 1;

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

            // test the neural network
            Console.WriteLine("Resultados:");
            foreach (IMLDataPair pair in conjuntoEntrenamiento)
            {
                IMLData output = network.Compute(pair.Input);
                Console.WriteLine(pair.Input[0] + @"," + pair.Input[1]
                                  + @", actual=" + output[0] + @",ideal=" + pair.Ideal[0]);
            }
        }
        /// <summary>
        /// Generate a random training set. 
        /// </summary>
        /// <param name="seed">The seed value to use, the same seed value will always produce
        /// the same results.</param>
        /// <param name="count">How many training items to generate.</param>
        /// <param name="inputCount">How many input numbers.</param>
        /// <param name="idealCount">How many ideal numbers.</param>
        /// <param name="min">The minimum random number.</param>
        /// <param name="max">The maximum random number.</param>
        /// <returns>The random training set.</returns>
        public static BasicMLDataSet Generate(long seed,
            int count, int inputCount,
            int idealCount, double min, double max)
        {
            var rand =
                new LinearCongruentialGenerator(seed);

            var result = new BasicMLDataSet();
            for (int i = 0; i < count; i++)
            {
                var inputData = new BasicMLData(inputCount);

                for (int j = 0; j < inputCount; j++)
                {
                    inputData[j] = rand.Range(min, max);
                }

                var idealData = new BasicMLData(idealCount);

                for (int j = 0; j < idealCount; j++)
                {
                    idealData[j] = rand.Range(min, max);
                }

                var pair = new BasicMLDataPair(inputData,
                                               idealData);
                result.Add(pair);
            }
            return result;
        }
Ejemplo n.º 21
0
        public void Execute(IExampleInterface app)
        {
            this.app = app;

            // Create the neural network.
            BasicLayer hopfield;
            var        network = new HopfieldNetwork(4);

            // This pattern will be trained
            bool[] pattern1 = { true, true, false, false };
            // This pattern will be presented
            bool[]  pattern2 = { true, false, false, false };
            IMLData result;

            var data1 = new BiPolarMLData(pattern1);
            var data2 = new BiPolarMLData(pattern2);
            var set   = new BasicMLDataSet();

            set.Add(data1);

            // train the neural network with pattern1
            app.WriteLine("Training Hopfield network with: "
                          + FormatBoolean(data1));

            network.AddPattern(data1);
            // present pattern1 and see it recognized
            result = network.Compute(data1);
            app.WriteLine("Presenting pattern:" + FormatBoolean(data1)
                          + ", and got " + FormatBoolean(result));
            // Present pattern2, which is similar to pattern 1. Pattern 1
            // should be recalled.
            result = network.Compute(data2);
            app.WriteLine("Presenting pattern:" + FormatBoolean(data2)
                          + ", and got " + FormatBoolean(result));
        }
Ejemplo n.º 22
0
        public void TestSOM()
        {
            // create the training set
            IMLDataSet training = new BasicMLDataSet(
                SOMInput, null);

            // Create the neural network.
            var network = new SOMNetwork(4, 2) {Weights = new Matrix(MatrixArray)};

            var train = new BasicTrainSOM(network, 0.4,
                                          training, new NeighborhoodSingle()) {ForceWinner = true};

            int iteration = 0;

            for (iteration = 0; iteration <= 100; iteration++)
            {
                train.Iteration();
            }

            IMLData data1 = new BasicMLData(
                SOMInput[0]);
            IMLData data2 = new BasicMLData(
                SOMInput[1]);

            int result1 = network.Classify(data1);
            int result2 = network.Classify(data2);

            Console.WriteLine(result1 + " result 2 :"+result2);
            Assert.IsTrue(result1 != result2);
        }
Ejemplo n.º 23
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.º 24
0
        public void Execute(IExampleInterface app)
        {
            this.app = app;

            // Create the neural network.
            BasicLayer hopfield;
            var network = new HopfieldNetwork(4);

            // This pattern will be trained
            bool[] pattern1 = {true, true, false, false};
            // This pattern will be presented
            bool[] pattern2 = {true, false, false, false};
            IMLData result;

            var data1 = new BiPolarMLData(pattern1);
            var data2 = new BiPolarMLData(pattern2);
            var set = new BasicMLDataSet();
            set.Add(data1);

            // train the neural network with pattern1
            app.WriteLine("Training Hopfield network with: "
                          + FormatBoolean(data1));

            network.AddPattern(data1);
            // present pattern1 and see it recognized
            result = network.Compute(data1);
            app.WriteLine("Presenting pattern:" + FormatBoolean(data1)
                          + ", and got " + FormatBoolean(result));
            // Present pattern2, which is similar to pattern 1. Pattern 1
            // should be recalled.
            result = network.Compute(data2);
            app.WriteLine("Presenting pattern:" + FormatBoolean(data2)
                          + ", and got " + FormatBoolean(result));
        }
Ejemplo n.º 25
0
        public static IMLDataSet GenerateTraining()
        {
            var result = new BasicMLDataSet();

            for (int i = 0; i < DIGITS.Length; i++)
            {
                var ideal = new BasicMLData(DIGITS.Length);

                // setup input
                IMLData input = Image2data(DIGITS[i]);

                // setup ideal
                for (int j = 0; j < DIGITS.Length; j++)
                {
                    if (j == i)
                    {
                        ideal[j] = 1;
                    }
                    else
                    {
                        ideal[j] = -1;
                    }
                }

                // add training element
                result.Add(input, ideal);
            }
            return(result);
        }
Ejemplo n.º 26
0
        static void Main(string[] args)
        {
            var network = new BasicNetwork();
            network.AddLayer(new BasicLayer(null, true, 2));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 3));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 1));
            network.Structure.FinalizeStructure();
            network.Reset();

            var trainingSet = new BasicMLDataSet(XORInput, XORIdeal);
            var train = new ResilientPropagation(network, trainingSet);
            var epoch = 1;
            do
            {
                train.Iteration();

            } while (train.Error > 0.01);

            train.FinishTraining();

            foreach (var pair in trainingSet)
            {
                var output = network.Compute(pair.Input);
                Console.WriteLine(pair.Input[0] + @", " + pair.Input[1] + @" , actual=" + output[0] + @", ideal=" + pair.Ideal[0]);
            }

            EncogFramework.Instance.Shutdown();
            Console.ReadLine();
        }
Ejemplo n.º 27
0
        public static void TrainElmhanNetwork(ref IExampleInterface app)
        {
            BasicMLDataSet set = CreateEval.CreateEvaluationSetAndLoad(app.Args[1], CONFIG.STARTING_YEAR,
                                                                       CONFIG.TRAIN_END,
                                                                       CONFIG.INPUT_WINDOW,
                                                                       CONFIG.PREDICT_WINDOW);

            //create our network.
            BasicNetwork network =
                (BasicNetwork)CreateEval.CreateElmanNetwork(CONFIG.INPUT_WINDOW, CONFIG.PREDICT_WINDOW);

            //Train it..

            double LastError = CreateEval.TrainNetworks(network, set);

            Console.WriteLine("NetWork Trained to :" + LastError);
            SuperUtils.SaveTraining(CONFIG.DIRECTORY, CONFIG.TRAINING_FILE, set);
            SuperUtils.SaveNetwork(CONFIG.DIRECTORY, CONFIG.NETWORK_FILE, network);
            Console.WriteLine("Network Saved to :" + CONFIG.DIRECTORY + " File Named :" +
                              CONFIG.NETWORK_FILE);

            Console.WriteLine("Training Saved to :" + CONFIG.DIRECTORY + " File Named :" +
                              CONFIG.TRAINING_FILE);
            MakeAPause();
        }
Ejemplo n.º 28
0
        public void TestSingleOutput()
        {
            BasicNetwork network = new BasicNetwork();

            network.AddLayer(new BasicLayer(null, true, 2));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 2));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 1));
            network.Structure.FinalizeStructure();

            (new ConsistentRandomizer(-1, 1)).Randomize(network);

            IMLDataSet trainingData = new BasicMLDataSet(XOR.XORInput, XOR.XORIdeal);

            HessianFD testFD = new HessianFD();

            testFD.Init(network, trainingData);
            testFD.Compute();

            HessianCR testCR = new HessianCR();

            testCR.Init(network, trainingData);
            testCR.Compute();

            //dump(testFD, "FD");
            //dump(testCR, "CR");
            Assert.IsTrue(testCR.HessianMatrix.equals(testFD.HessianMatrix, 4));
        }
Ejemplo n.º 29
0
        /// <summary>
        /// Program entry point.
        /// </summary>
        /// <param name="app">Holds arguments and other info.</param>
        public void Execute(IExampleInterface app)
        {
            var network = new FlatNetwork(2, 4, 0, 1, false);
            network.Randomize();

            IMLDataSet trainingSet = new BasicMLDataSet(XORInput, XORIdeal);


            var train = new TrainFlatNetworkResilient(network, trainingSet);

            int epoch = 1;

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

            var output = new double[1];
            // test the neural network
            Console.WriteLine(@"Neural Network Results:");
            foreach (IMLDataPair pair in trainingSet)
            {
                double[] input = pair.Input.Data;
                network.Compute(input, output);
                Console.WriteLine(input[0] + @"," + input[1] + @":" + output[0]);
            }
        }
        public static IMLDataSet LoadCSVToDataSet(FileInfo fileInfo, int inputCount, int outputCount, bool randomize = true, bool headers = true)
        {
            BasicMLDataSet result = new BasicMLDataSet();
            CultureInfo CSVformat = new CultureInfo("en");

            using (TextFieldParser parser = new TextFieldParser(fileInfo.FullName))
            {
                parser.TextFieldType = FieldType.Delimited;
                parser.SetDelimiters(",");
                if (headers)
                    parser.ReadFields();
                while (!parser.EndOfData)
                {
                    //Processing row
                    string[] fields = parser.ReadFields();
                    var input = new BasicMLData(inputCount);
                    for (int i = 0; i < inputCount; i++)
                        input[i] = double.Parse(fields[i], CSVformat);
                    var ideal = new BasicMLData(outputCount);
                    for (int i = 0; i < outputCount; i++)
                        ideal[i] = double.Parse(fields[i + inputCount], CSVformat);
                    result.Add(input, ideal);
                }
            }
            var rand = new Random(DateTime.Now.Millisecond);

            return (randomize ? new BasicMLDataSet(result.OrderBy(r => rand.Next()).ToList()) : new BasicMLDataSet(result));
        }
Ejemplo n.º 31
0
        /// <summary>
        /// Processes the specified double serie into an IMLDataset.
        /// To use this method, you must provide a formated double array.
        /// The number of points in the input window makes the input array , and the predict window will create the array used in ideal.
        /// Example you have an array with 1, 2, 3 , 4 , 5.
        /// You can use this method to make an IMLDataset 4 inputs and 1 ideal (5).
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="_inputWindow">The _input window.</param>
        /// <param name="_predictWindow">The _predict window.</param>
        /// <returns></returns>
        public static IMLDataSet ProcessDoubleSerieIntoIMLDataset(double[] data, int _inputWindow, int _predictWindow)
        {
            IMLDataSet result          = new BasicMLDataSet();
            int        totalWindowSize = _inputWindow + _predictWindow;
            int        stopPoint       = data.Length - totalWindowSize;

            for (int i = 0; i < stopPoint; i++)
            {
                IMLData inputData = new BasicMLData(_inputWindow);
                IMLData idealData = new BasicMLData(_predictWindow);
                int     index     = i;
                // handle input window
                for (int j = 0; j < _inputWindow; j++)
                {
                    inputData[j] = data[index++];
                }

                // handle predict window
                for (int j = 0; j < _predictWindow; j++)
                {
                    idealData[j] = data[index++];
                }
                IMLDataPair pair = new BasicMLDataPair(inputData, idealData);
                result.Add(pair);
            }

            return(result);
        }
Ejemplo n.º 32
0
        /// <summary>
        /// Load a CSV file into a memory dataset.  
        /// </summary>
        ///
        /// <param name="format">The CSV format to use.</param>
        /// <param name="filename">The filename to load.</param>
        /// <param name="headers">True if there is a header line.</param>
        /// <param name="inputSize">The input size.  Input always comes first in a file.</param>
        /// <param name="idealSize">The ideal size, 0 for unsupervised.</param>
        /// <returns>A NeuralDataSet that holds the contents of the CSV file.</returns>
        public static IMLDataSet LoadCSVTOMemory(CSVFormat format, String filename,
                                                bool headers, int inputSize, int idealSize)
        {
            var result = new BasicMLDataSet();
            var csv = new ReadCSV(filename, headers, format);
            while (csv.Next())
            {
                BasicMLData ideal = null;
                int index = 0;

                var input = new BasicMLData(inputSize);
                for (int i = 0; i < inputSize; i++)
                {
                    double d = csv.GetDouble(index++);
                    input[i] = d;
                }

                if (idealSize > 0)
                {
                    ideal = new BasicMLData(idealSize);
                    for (int i = 0; i < idealSize; i++)
                    {
                        double d = csv.GetDouble(index++);
                        ideal[i] = d;
                    }
                }

                IMLDataPair pair = new BasicMLDataPair(input, ideal);
                result.Add(pair);
            }

            return result;
        }
Ejemplo n.º 33
0
        public void JustDoIt()
        {
            var network = new BasicNetwork();

            network.AddLayer(new BasicLayer(null, false, 2));
            network.AddLayer(new BasicLayer(new ActivationLinear(), false, 1));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 1));
            network.Structure.FinalizeStructure();
            network.Reset();

            IMLDataSet trainDataSet = new BasicMLDataSet(XORInput, XORIdeal);

            IMLTrain train = new ResilientPropagation(network, trainDataSet);

            int epoch = 1;

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

            train.FinishTraining();

            Console.WriteLine(@"Neural Network Results:");
            foreach (IMLDataPair pair in trainDataSet)
            {
                IMLData output = network.Compute(pair.Input);
                Console.WriteLine(pair.Input[0] + @"," + pair.Input[1]
                                  + @", actual=" + output[0] + @",ideal=" + pair.Ideal[0]);
            }
            EncogFramework.Instance.Shutdown();
            Console.ReadKey();
        }
Ejemplo n.º 34
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");
        }
        /// <summary>
        /// Evaluate memory.
        /// </summary>
        private void EvalMemory()
        {
            BasicMLDataSet training = RandomTrainingFactory.Generate(
                1000, 10000, 10, 10, -1, 1);

            const long stop   = (10 * Evaluate.Milis);
            int        record = 0;

            IMLDataPair pair = BasicMLDataPair.CreatePair(10, 10);

            int iterations = 0;
            var watch      = new Stopwatch();

            watch.Start();
            while (watch.ElapsedMilliseconds < stop)
            {
                iterations++;
                training.GetRecord(record++, pair);
                if (record >= training.Count)
                {
                    record = 0;
                }
            }

            iterations /= 100000;

            _report.Report(Steps, Step2,
                           "Memory dataset, result: " + Format.FormatInteger(iterations));

            _memoryScore = iterations;
        }
Ejemplo n.º 36
0
        /// <summary>
        /// Generate a random training set.
        /// </summary>
        /// <param name="seed">The seed value to use, the same seed value will always produce
        /// the same results.</param>
        /// <param name="count">How many training items to generate.</param>
        /// <param name="inputCount">How many input numbers.</param>
        /// <param name="idealCount">How many ideal numbers.</param>
        /// <param name="min">The minimum random number.</param>
        /// <param name="max">The maximum random number.</param>
        /// <returns>The random training set.</returns>
        public static BasicMLDataSet Generate(long seed,
                                              int count, int inputCount,
                                              int idealCount, double min, double max)
        {
            var rand =
                new LinearCongruentialGenerator(seed);

            var result = new BasicMLDataSet();

            for (int i = 0; i < count; i++)
            {
                var inputData = new BasicMLData(inputCount);

                for (int j = 0; j < inputCount; j++)
                {
                    inputData[j] = rand.Range(min, max);
                }

                var idealData = new BasicMLData(idealCount);

                for (int j = 0; j < idealCount; j++)
                {
                    idealData[j] = rand.Range(min, max);
                }

                var pair = new BasicMLDataPair(inputData,
                                               idealData);
                result.Add(pair);
            }
            return(result);
        }
Ejemplo n.º 37
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.º 38
0
        protected ProbabilitySupportVectorMachine TrainSVM(double C, double gamma, List <ISample> trainingSamples, Func <ISample, double> idealFunction)
        {
            // duplicate the training dataset for better cross validation by LIBSVM probability generator (see LIBSVM documentation)
            List <double[]> inputSamples = trainingSamples.Select(sample => sample.GetDimensions()).ToList();

            inputSamples.AddRange(trainingSamples.Select(sample => sample.GetDimensions()));

            // account for imposter samples (identifier not in identifierMap)
            List <double[]> outputSamples = trainingSamples.Select(sample => new double[] { idealFunction.Invoke(sample) }).ToList();

            outputSamples.AddRange(trainingSamples.Select(sample => new double[] { idealFunction.Invoke(sample) }));

            double[][] INPUT = inputSamples.ToArray();
            double[][] IDEAL = outputSamples.ToArray();

            // train the SVM classifier with the provided data
            IMLDataSet trainingData = new BasicMLDataSet(INPUT, IDEAL);

            ProbabilitySupportVectorMachine svmNetwork = new ProbabilitySupportVectorMachine(trainingSamples[0].GetDimensionCount(), false, 0.00000001);

            // train the SVM classifier with the provided C and gamma
            SVMTrain trainedSVM = new SVMTrain(svmNetwork, trainingData)
            {
                Fold  = 0,
                Gamma = gamma,
                C     = C
            };

            trainedSVM.Iteration();

            Console.WriteLine("SVM training error: " + trainedSVM.Error);
            return(svmNetwork);
        }
        public static BasicMLDataSet CreateEvaluationSetAndLoad(string @fileName, int startLine, int HowMany, int WindowSize, int outputsize)
        {
            List <double> Opens = QuickCSVUtils.QuickParseCSV(fileName, "Open", startLine, HowMany);
            List <double> High  = QuickCSVUtils.QuickParseCSV(fileName, "High", startLine, HowMany);
            // List<double> Low = QuickCSVUtils.QuickParseCSV(fileName, "Low", startLine, HowMany);
            List <double> Close  = QuickCSVUtils.QuickParseCSV(fileName, "Close", startLine, HowMany);
            List <double> Volume = QuickCSVUtils.QuickParseCSV(fileName, 5, startLine, HowMany);

            double[]           Ranges      = NetworkUtility.CalculateRanges(Opens.ToArray(), Close.ToArray());
            IMLDataPair        aPairInput  = TrainerHelper.ProcessPairs(NetworkUtility.CalculatePercents(Opens.ToArray()), NetworkUtility.CalculatePercents(Opens.ToArray()), WindowSize, outputsize);
            IMLDataPair        aPairInput3 = TrainerHelper.ProcessPairs(NetworkUtility.CalculatePercents(Close.ToArray()), NetworkUtility.CalculatePercents(Close.ToArray()), WindowSize, outputsize);
            IMLDataPair        aPairInput2 = TrainerHelper.ProcessPairs(NetworkUtility.CalculatePercents(High.ToArray()), NetworkUtility.CalculatePercents(High.ToArray()), WindowSize, outputsize);
            IMLDataPair        aPairInput4 = TrainerHelper.ProcessPairs(NetworkUtility.CalculatePercents(Volume.ToArray()), NetworkUtility.CalculatePercents(Volume.ToArray()), WindowSize, outputsize);
            IMLDataPair        aPairInput5 = TrainerHelper.ProcessPairs(NetworkUtility.CalculatePercents(Ranges.ToArray()), NetworkUtility.CalculatePercents(Ranges.ToArray()), WindowSize, outputsize);
            List <IMLDataPair> listData    = new List <IMLDataPair>();

            listData.Add(aPairInput);
            listData.Add(aPairInput2);
            listData.Add(aPairInput3);
            listData.Add(aPairInput4);
            listData.Add((aPairInput5));


            var minitrainning = new BasicMLDataSet(listData);

            return(minitrainning);
        }
        public static double EvaluateNetworks(BasicNetwork network, BasicMLDataSet set)
        {
            int count   = 0;
            int correct = 0;

            foreach (IMLDataPair pair in set)
            {
                IMLData input       = pair.Input;
                IMLData actualData  = pair.Ideal;
                IMLData predictData = network.Compute(input);

                double actual  = actualData[0];
                double predict = predictData[0];
                double diff    = Math.Abs(predict - actual);

                Direction actualDirection  = DetermineDirection(actual);
                Direction predictDirection = DetermineDirection(predict);

                if (actualDirection == predictDirection)
                {
                    correct++;
                }
                count++;
                Console.WriteLine(@"Number" + @"count" + @": actual=" + Format.FormatDouble(actual, 4) + @"(" + actualDirection + @")"
                                  + @",predict=" + Format.FormatDouble(predict, 4) + @"(" + predictDirection + @")" + @",diff=" + diff);
            }
            double percent = correct / (double)count;

            Console.WriteLine(@"Direction correct:" + correct + @"/" + count);
            Console.WriteLine(@"Directional Accuracy:"
                              + Format.FormatPercent(percent));

            return(percent);
        }
        /// <summary>
        /// Process the array.
        /// </summary>
        ///
        /// <param name="data">The array to process.</param>
        /// <returns>A neural data set that contains the time-series.</returns>
        public IMLDataSet Process(double[] data)
        {
            var result = new BasicMLDataSet();

            int totalWindowSize = _inputWindow + _predictWindow;
            int stopPoint       = data.Length - totalWindowSize;

            for (int i = 0; i < stopPoint; i++)
            {
                var inputData = new BasicMLData(_inputWindow);
                var idealData = new BasicMLData(_predictWindow);

                int index = i;

                // handle input window
                for (int j = 0; j < _inputWindow; j++)
                {
                    inputData[j] = data[index++];
                }

                // handle predict window
                for (int j = 0; j < _predictWindow; j++)
                {
                    idealData[j] = data[index++];
                }

                var pair = new BasicMLDataPair(inputData, idealData);
                result.Add(pair);
            }

            return(result);
        }
Ejemplo n.º 42
0
        static void Main(string[] args)
        {
            // this form of ANN uses genetic algorithm to produce
            // hidden layer of neurons
            // A NEAT network starts with only an
            // input layer and output layer. The rest is evolved as the training progresses.
            // Connections inside of a NEAT neural network can be feedforward, recurrent,
            // or self - connected.All of these connection types will be tried by NEAT as it
            // attempts to evolve a neural network capable of the given task.
            IMLDataSet     trainingSet = new BasicMLDataSet(XORInput, XORIdeal);
            NEATPopulation pop         = new NEATPopulation(2, 1, 1000);

            pop.Reset();
            pop.InitialConnectionDensity = 1.0; // not required, but speeds processing.
            ICalculateScore score = new TrainingSetScore(trainingSet);
            // train the neural network
            TrainEA train = NEATUtil.ConstructNEATTrainer(pop, score);

            EncogUtility.TrainToError(train, 0.01);

            NEATNetwork network = (NEATNetwork)train.CODEC.Decode(train.BestGenome);

            // TODO no persistance? no means to peek structure?

            // test the neural network
            Console.WriteLine(@"Neural Network Results:");
            EncogUtility.Evaluate(network, trainingSet);
        }
Ejemplo n.º 43
0
        /// <summary>
        /// Program entry point.
        /// </summary>
        /// <param name="app">Holds arguments and other info.</param>
        public void Execute(IExampleInterface app)
        {
            // create a neural network, without using a factory
            var network = new BasicNetwork();

            network.AddLayer(new BasicLayer(null, true, 2));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 3));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 1));
            network.Structure.FinalizeStructure();
            network.Reset();

            // create training data
            IMLDataSet trainingSet = new BasicMLDataSet(XORInput, XORIdeal);

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

            int epoch = 1;

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

            // test the neural network
            Console.WriteLine(@"Neural Network Results:");
            foreach (IMLDataPair pair in trainingSet)
            {
                IMLData output = network.Compute(pair.Input);
                Console.WriteLine(pair.Input[0] + @"," + pair.Input[1]
                                  + @", actual=" + output[0] + @",ideal=" + pair.Ideal[0]);
            }
        }
 private static SupportVectorMachine Create(IMLDataSet theset, int inputs)
 {
     IMLDataSet training = new BasicMLDataSet(theset);
     SupportVectorMachine result = new SupportVectorMachine(inputs, SVMType.EpsilonSupportVectorRegression, KernelType.Sigmoid);
     SVMTrain train = new SVMTrain(result, training);
     train.Iteration();
     return result;
 }
Ejemplo n.º 45
0
        public void TestManhattan()
        {
            IMLDataSet trainingData = new BasicMLDataSet(XOR.XORInput, XOR.XORIdeal);

            BasicNetwork network = NetworkUtil.CreateXORNetworkUntrained();
            IMLTrain bprop = new ManhattanPropagation(network, trainingData, 0.01);
            NetworkUtil.TestTraining(bprop, 0.01);
        }
Ejemplo n.º 46
0
        public void TestRPROP()
        {
            IMLDataSet trainingData = new BasicMLDataSet(XOR.XORInput, XOR.XORIdeal);

            BasicNetwork network = NetworkUtil.CreateXORNetworkUntrained();
            IMLTrain rprop = new ResilientPropagation(network, trainingData);
            NetworkUtil.TestTraining(rprop, 0.03);
        }
Ejemplo n.º 47
0
        public void TestLMA()
        {
            IMLDataSet trainingData = new BasicMLDataSet(XOR.XORInput, XOR.XORIdeal);

            BasicNetwork network = NetworkUtil.CreateXORNetworkUntrained();
            IMLTrain rprop = new LevenbergMarquardtTraining(network, trainingData);
            NetworkUtil.TestTraining(rprop, 0.03);
        }
Ejemplo n.º 48
0
 public void TestAnneal()
 {
     IMLDataSet trainingData = new BasicMLDataSet(XOR.XORInput, XOR.XORIdeal);
     BasicNetwork network = NetworkUtil.CreateXORNetworkUntrained();
     ICalculateScore score = new TrainingSetScore(trainingData);
     NeuralSimulatedAnnealing anneal = new NeuralSimulatedAnnealing(network, score, 10, 2, 100);
     NetworkUtil.TestTraining(anneal, 0.01);
 }
Ejemplo n.º 49
0
 private SupportVectorMachine Create()
 {
     IMLDataSet training = new BasicMLDataSet(XOR.XORInput, XOR.XORIdeal);
     SupportVectorMachine result = new SupportVectorMachine(2, SVMType.EpsilonSupportVectorRegression, KernelType.RadialBasisFunction);
     SVMTrain train = new SVMTrain(result, training);
     train.Iteration();
     return result;
 }
 public void EvaluateNetwork(BasicNetwork trainedNetwork, BasicMLDataSet trainingData)
 {
     foreach (var trainingItem in trainingData)
     {
         var output = trainedNetwork.Compute(trainingItem.Input);
         Console.WriteLine("Input:{0}, {1}  Ideal: {2}  Actual : {3}", trainingItem.Input[0], trainingItem.Input[1], trainingItem.Ideal, output[0]);
     }
     Console.ReadKey();
 }
        /// <summary>
        /// Create a dataset from the clustered data. 
        /// </summary>
        /// <returns>The dataset.</returns>
        public IMLDataSet CreateDataSet()
        {
            var result = new BasicMLDataSet();

            foreach (IMLData dataItem in _data)
            {
                result.Add(dataItem);
            }

            return result;
        }
        static void Main(string[] args)
        {
            var network = CreateNetwork();
            var trainingData = new BasicMLDataSet(XORStatics.XorInputMatrix,XORStatics.XorIdealMatrix);

            var functions = new BasicNeuralNetFunctions();

            var trainedNetwork = functions.TrainNetwork(network, trainingData);

            functions.EvaluateNetwork(network,trainingData);
        }
Ejemplo n.º 53
0
        public void TestClassifyPNN()
        {
            PNNOutputMode mode = PNNOutputMode.Classification;
            BasicPNN network = new BasicPNN(PNNKernelType.Gaussian, mode, 2, 2);

            IMLDataSet trainingData = new BasicMLDataSet(XOR.XORInput, XOR.XORIdeal);

            TrainBasicPNN train = new TrainBasicPNN(network, trainingData);
            train.Iteration();

            XOR.VerifyXOR(network, 0.01);
        }
Ejemplo n.º 54
0
 public void Execute(IExampleInterface app)
 {
     this.app = app;
     IMLDataSet trainingSet = new BasicMLDataSet(XOR_INPUT, XOR_IDEAL);
     BasicNetwork network = EncogUtility.SimpleFeedForward(2, 6, 0, 1, false);
     EncogUtility.TrainToError(network, trainingSet, 0.01);
     double error = network.CalculateError(trainingSet);
     EncogDirectoryPersistence.SaveObject(new FileInfo(FILENAME), network);
     double error2 = network.CalculateError(trainingSet);
     app.WriteLine("Error before save to EG: " + Format.FormatPercent(error));
     app.WriteLine("Error before after to EG: " + Format.FormatPercent(error2));
 }
        public ResilientPropagation TrainNetwork(BasicNetwork network, BasicMLDataSet trainingData)
        {
            var trainedNetwork = new ResilientPropagation(network, trainingData);
            var epoch = 0;
            do
            {
                trainedNetwork.Iteration();
                epoch++;
                Console.WriteLine("Epoch:{0}, Error{1}", epoch, trainedNetwork.Error);
            } while (trainedNetwork.Error > 0.01);

            return trainedNetwork;
        }
Ejemplo n.º 56
0
 public static void ExportToExcel(BasicMLDataSet data, DataSetEditMode mode)
 {
     object obj2;
     int num;
     int num2;
     int num3;
     int num4;
     int num5;
     int num6;
     bool flag;
     Application application2 = (Application) Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("00024500-0000-0000-C000-000000000046")));
     goto Label_0FE4;
     Label_0028:
     num2++;
     using (IEnumerator<IMLDataPair> enumerator = data.GetEnumerator())
     {
         IMLDataPair pair;
         goto Label_0040;
     Label_0039:
         num2++;
     Label_0040:
         flag = enumerator.MoveNext();
         if (flag)
         {
             goto Label_0796;
         }
         if (((uint) flag) < 0)
         {
             goto Label_01D4;
         }
         return;
     Label_006A:
         num++;
         num4++;
     Label_0075:
         flag = num4 <= pair.ErrorArray.Length;
     Label_0087:
         if (flag)
         {
             goto Label_01DC;
         }
         if (((uint) num4) >= 0)
         {
             goto Label_07CA;
         }
         goto Label_01FF;
     Label_00A8:;
         <ExportToExcel>o__SiteContainer1.<>p__Site17 = CallSite<Func<CallSite, object, double, object>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.SetMember(CSharpBinderFlags.None, "Value2", typeof(ExcelDataExport), new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null) }));
     Label_00E9:
         if (<ExportToExcel>o__SiteContainer1.<>p__Site18 == null)
         {
 public void Execute(IExampleInterface app)
 {
     this.app = app;
     this.app = app;
     IMLDataSet trainingSet = new BasicMLDataSet(XOR_INPUT, XOR_IDEAL);
     BasicNetwork network = EncogUtility.SimpleFeedForward(2, 6, 0, 1, false);
     EncogUtility.TrainToError(network, trainingSet, 0.01);
     double error = network.CalculateError(trainingSet);
     SerializeObject.Save("encog.ser", network);
     network = (BasicNetwork) SerializeObject.Load("encog.ser");
     double error2 = network.CalculateError(trainingSet);
     app.WriteLine("Error before save to ser: " + Format.FormatPercent(error));
     app.WriteLine("Error before after to ser: " + Format.FormatPercent(error2));
 }
        private NEATPopulation Generate()
        {
            IMLDataSet trainingSet = new BasicMLDataSet(XOR.XORInput, XOR.XORIdeal);

            ICalculateScore score = new TrainingSetScore(trainingSet);
            // train the neural network
            ActivationStep step = new ActivationStep();
            step.Center = 0.5;

            IEvolutionaryAlgorithm train = NEATUtil.ConstructNEATTrainer(
                    score, 2, 1, 10);

            return (NEATPopulation)train.Population;
        }
        public BasicPNN create()
        {
            PNNOutputMode mode = PNNOutputMode.Regression;

            BasicPNN network = new BasicPNN(PNNKernelType.Gaussian, mode, 2, 1);

            BasicMLDataSet trainingSet = new BasicMLDataSet(XOR.XORInput,
                    XOR.XORIdeal);

            TrainBasicPNN train = new TrainBasicPNN(network, trainingSet);
            train.Iteration();
            XOR.VerifyXOR(network, 0.001);
            return network;
        }
        private NEATPopulation Generate()
        {
            IMLDataSet trainingSet = new BasicMLDataSet(XOR.XORInput, XOR.XORIdeal);

            ICalculateScore score = new TrainingSetScore(trainingSet);
            // train the neural network
            ActivationStep step = new ActivationStep();
            step.Center = 0.5;

            NEATTraining train = new NEATTraining(
                    score, 2, 1, 10);

            return (NEATPopulation)train.Population;
        }