示例#1
0
        /// <summary>
        /// Generate the network.
        /// </summary>
        /// <returns>The generated network.</returns>
        public BasicNetwork Generate()
        {

            ILayer input, instar, outstar;
            int y = PatternConst.START_Y;

            BasicNetwork network = new BasicNetwork();
            network.AddLayer(input = new BasicLayer(new ActivationLinear(), false, this.inputCount));
            network.AddLayer(instar = new BasicLayer(new ActivationCompetitive(), false, this.instarCount));
            network.AddLayer(outstar = new BasicLayer(new ActivationLinear(), false, this.outstarCount));
            network.Structure.FinalizeStructure();
            network.Reset();

            input.X = PatternConst.START_X;
            input.Y = y;
            y += PatternConst.INC_Y;

            instar.X = PatternConst.START_X;
            instar.Y = y;
            y += PatternConst.INC_Y;

            outstar.X = PatternConst.START_X;
            outstar.Y = y;

            // tag as needed
            network.TagLayer(BasicNetwork.TAG_INPUT, input);
            network.TagLayer(BasicNetwork.TAG_OUTPUT, outstar);
            network.TagLayer(CPNPattern.TAG_INSTAR, instar);
            network.TagLayer(CPNPattern.TAG_OUTSTAR, outstar);

            return network;
        }
        /// <summary>
        /// Generate the Hopfield neural network.
        /// </summary>
        /// <returns>The generated network.</returns>
        public BasicNetwork Generate()
        {
            ILayer layer = new BasicLayer(new ActivationBiPolar(), false,
                    this.neuronCount);

            BasicNetwork result = new BasicNetwork(new HopfieldLogic());
            result.AddLayer(layer);
            layer.AddNext(layer);
            layer.X = PatternConst.START_X;
            layer.Y = PatternConst.START_Y;
            result.Structure.FinalizeStructure();
            result.Reset();
            return result;
        }
示例#3
0
 /// <summary>
 /// Generate the RSOM network.
 /// </summary>
 /// <returns>The neural network.</returns>
 public BasicNetwork Generate()
 {
     ILayer input = new BasicLayer(new ActivationLinear(), false,
             this.inputNeurons);
     ILayer output = new BasicLayer(new ActivationLinear(), false,
             this.outputNeurons);
     int y = PatternConst.START_Y;
     BasicNetwork network = new BasicNetwork();
     network.AddLayer(input);
     network.AddLayer(output);
     input.X = PatternConst.START_X;
     output.X = PatternConst.START_X;
     input.Y = y;
     y += PatternConst.INC_Y;
     output.Y = y;
     network.Logic = new SOMLogic();
     network.Structure.FinalizeStructure();
     network.Reset();
     return network;
 }
        /// <summary>
        /// Generate a Jordan neural network.
        /// </summary>
        /// <returns>A Jordan neural network.</returns>
        public BasicNetwork Generate()
        {
            // construct an Jordan type network
            ILayer input = new BasicLayer(this.activation, false,
                   this.inputNeurons);
            ILayer hidden = new BasicLayer(this.activation, true,
                   this.hiddenNeurons);
            ILayer output = new BasicLayer(this.activation, true,
                   this.outputNeurons);
            ILayer context = new ContextLayer(this.outputNeurons);
            BasicNetwork network = new BasicNetwork();
            network.AddLayer(input);
            network.AddLayer(hidden);
            network.AddLayer(output);

            output.AddNext(context, SynapseType.OneToOne);
            context.AddNext(hidden);

            int y = PatternConst.START_Y;
            input.X = PatternConst.START_X;
            input.Y = y;
            y += PatternConst.INC_Y;
            hidden.X = PatternConst.START_X;
            hidden.Y = y;
            context.X = PatternConst.INDENT_X;
            context.Y = y;
            y += PatternConst.INC_Y;
            output.X = PatternConst.START_X;
            output.Y = y;

            network.Structure.FinalizeStructure();
            network.Reset();
            return network;
        }
示例#5
0
        /// <summary>
        /// This is based off of this article:
        /// http://www.codeproject.com/Articles/54575/An-Introduction-to-Encog-Neural-Networks-for-C
        /// </summary>
        /// <remarks>
        /// Go here for documentation of encog:
        /// http://www.heatonresearch.com/wiki
        ///
        /// Download link:
        /// https://github.com/encog/encog-dotnet-core/releases
        /// </remarks>
        private void btnXOR_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                _trainingData = null;
                _results      = null;

                BasicNetwork network = new BasicNetwork();

                #region Create nodes

                // Create the network's nodes

                //NOTE: Using ActivationSigmoid, because there are no negative values.  If there were negative, use ActivationTANH
                //http://www.heatonresearch.com/wiki/Activation_Function

                //NOTE: ActivationSigmoid (0 to 1) and ActivationTANH (-1 to 1) are pure but slower.  A cruder but faster function is ActivationElliott (0 to 1) and ActivationElliottSymmetric (-1 to 1)
                //http://www.heatonresearch.com/wiki/Elliott_Activation_Function

                network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 2));     // input layer
                network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 6));     // hidden layer
                network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 1));     // output layer
                network.Structure.FinalizeStructure();

                // Randomize the links
                network.Reset();

                #endregion

                #region Training data

                // Neural networks must be trained before they are of any use. To train this neural network, we must provide training
                // data. The training data is the truth table for the XOR operator. The XOR has the following inputs:
                double[][] xor_input = new[]
                {
                    new[] { 0d, 0d },
                    new[] { 1d, 0d },
                    new[] { 0d, 1d },
                    new[] { 1d, 1d },
                };

                // And the expected outputs
                double[][] xor_ideal_output = new[]
                {
                    new[] { 0d },
                    new[] { 1d },
                    new[] { 1d },
                    new[] { 0d },
                };

                _trainingData = GetDrawDataFromTrainingData(xor_input, xor_ideal_output);

                #endregion
                #region Train network

                INeuralDataSet trainingSet = new BasicNeuralDataSet(xor_input, xor_ideal_output);

                // This is a good general purpose training algorithm
                //http://www.heatonresearch.com/wiki/Training
                ITrain train = new ResilientPropagation(network, trainingSet);

                List <double> log = new List <double>();

                int trainingIteration = 1;
                do
                {
                    train.Iteration();

                    log.Add(train.Error);

                    trainingIteration++;
                } while ((trainingIteration < 2000) && (train.Error > 0.001));

                // Paste this into excel and chart it to see the error trend
                string logExcel = string.Join("\r\n", log);

                #endregion

                #region Test

                //NOTE: I initially ran a bunch of tests, but the network always returns exactly the same result when given the same inputs
                //var test = Enumerable.Range(0, 1000).
                //    Select(o => new { In1 = _rand.Next(2), In2 = _rand.Next(2) }).

                var test = xor_input.
                           Select(o => new { In1 = Convert.ToInt32(o[0]), In2 = Convert.ToInt32(o[1]) }).
                           Select(o => new
                {
                    o.In1,
                    o.In2,
                    Expected = XOR(o.In1, o.In2),
                    NN       = CallNN(network, o.In1, o.In2),
                }).
                           Select(o => new { o.In1, o.In2, o.Expected, o.NN, Error = Math.Abs(o.Expected - o.NN) }).
                           OrderByDescending(o => o.Error).
                           ToArray();

                #endregion
                #region Test intermediate values

                // It was only trained with inputs of 0 and 1.  Let's see what it does with values in between

                var intermediates = Enumerable.Range(0, 1000).
                                    Select(o => new { In1 = _rand.NextDouble(), In2 = _rand.NextDouble() }).
                                    Select(o => new
                {
                    o.In1,
                    o.In2,
                    NN = CallNN(network, o.In1, o.In2),
                }).
                                    OrderBy(o => o.In1).
                                    ThenBy(o => o.In2).
                                    //OrderBy(o => o.NN).
                                    ToArray();

                #endregion

                #region Serialize/Deserialize

                // Serialize it
                string   weightDump = network.DumpWeights();
                double[] dumpArray  = weightDump.Split(',').
                                      Select(o => double.Parse(o)).
                                      ToArray();

                //TODO: Shoot through the layers, and store in some custom structure that can be serialized, then walked through to rebuild on deserialize
                //string[] layerDump = network.Structure.Layers.
                //    Select(o => o.ToString()).
                //    ToArray();

                // Create a clone
                BasicNetwork clone = new BasicNetwork();

                clone.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 2));
                clone.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 6));
                clone.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 1));
                clone.Structure.FinalizeStructure();

                clone.DecodeFromArray(dumpArray);

                // Test the clone
                string cloneDump = clone.DumpWeights();

                bool isSame = weightDump == cloneDump;

                var cloneTests = xor_input.
                                 Select(o => new
                {
                    Input = o,
                    NN    = CallNN(clone, o[0], o[1]),
                }).ToArray();

                #endregion

                #region Store results

                double[] matchValues = new[] { 0d, 1d };
                double   matchRange  = .03;     //+- 5% of target value would be considered a match

                _results = intermediates.
                           Select(o => Tuple.Create(new Point(o.In1, o.In2), o.NN, IsMatch(o.NN, matchValues, matchRange))).
                           ToArray();

                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
            finally
            {
                RedrawResults();
            }
        }
        /// <summary>
        /// Generate the network.
        /// </summary>
        /// <returns>The generated network.</returns>
        public BasicNetwork Generate()
        {
            ILayer layer = new BasicLayer(new ActivationBiPolar(), true,
                    this.neuronCount);

            BasicNetwork result = new BasicNetwork(new BoltzmannLogic());
            result.SetProperty(BoltzmannLogic.PROPERTY_ANNEAL_CYCLES, this.annealCycles);
            result.SetProperty(BoltzmannLogic.PROPERTY_RUN_CYCLES, this.runCycles);
            result.SetProperty(BoltzmannLogic.PROPERTY_TEMPERATURE, this.temperature);
            result.AddLayer(layer);
            layer.AddNext(layer);
            layer.X = PatternConst.START_X;
            layer.Y = PatternConst.START_Y;
            result.Structure.FinalizeStructure();
            result.Reset();
            return result;
        }
示例#7
0
        public void Execute(IExampleInterface app)
        {
            //Specify the number of dimensions and the number of neurons per dimension
            int dimensions             = 2;
            int numNeuronsPerDimension = 7;

            //Set the standard RBF neuron width.
            //Literature seems to suggest this is a good default value.
            double volumeNeuronWidth = 2.0 / numNeuronsPerDimension;

            //RBF can struggle when it comes to flats at the edge of the sample space.
            //We have added the ability to include wider neurons on the sample space boundary which greatly
            //improves fitting to flats
            bool includeEdgeRBFs = true;

            #region Setup
            //General setup is the same as before
            RadialBasisPattern pattern = new RadialBasisPattern();
            pattern.InputNeurons  = dimensions;
            pattern.OutputNeurons = 1;

            //Total number of neurons required.
            //Total number of Edges is calculated possibly for future use but not used any further here
            int numNeurons = (int)Math.Pow(numNeuronsPerDimension, dimensions);
            int numEdges   = (int)(dimensions * Math.Pow(2, dimensions - 1));

            pattern.AddHiddenLayer(numNeurons);

            BasicNetwork             network  = pattern.Generate();
            RadialBasisFunctionLayer rbfLayer = (RadialBasisFunctionLayer)network.GetLayer(RadialBasisPattern.RBF_LAYER);
            network.Reset();

            //Position the multidimensional RBF neurons, with equal spacing, within the provided sample space from 0 to 1.
            rbfLayer.SetRBFCentersAndWidthsEqualSpacing(0, 1, RBFEnum.Gaussian, dimensions, volumeNeuronWidth, includeEdgeRBFs);

            #endregion

            //Create some training data that can not easily be represented by gaussians
            //There are other training examples for both 1D and 2D
            //Degenerate training data only provides outputs as 1 or 0 (averaging over all outputs for a given set of inputs would produce something approaching the smooth training data).
            //Smooth training data provides true values for the provided input dimensions.
            Create2DSmoothTainingDataGit();

            //Create the training set and train.
            INeuralDataSet trainingSet = new BasicNeuralDataSet(INPUT, IDEAL);
            ITrain         train       = new SVDTraining(network, trainingSet);

            //SVD is a single step solve
            int epoch = 1;
            do
            {
                train.Iteration();
                Console.WriteLine("Epoch #" + epoch + " Error:" + train.Error);
                epoch++;
            } while ((epoch < 1) && (train.Error > 0.001));

            // test the neural network
            Console.WriteLine("Neural Network Results:");

            //Create a testing array which may be to a higher resoltion than the original training data
            Set2DTestingArrays(100);
            trainingSet = new BasicNeuralDataSet(INPUT, IDEAL);

            //Write out the results data
            using (var sw = new System.IO.StreamWriter("results.csv", false))
            {
                foreach (INeuralDataPair pair in trainingSet)
                {
                    INeuralData output = network.Compute(pair.Input);
                    //1D//sw.WriteLine(InverseScale(pair.Input[0]) + ", " + Chop(InverseScale(output[0])));// + ", " + pair.Ideal[0]);
                    sw.WriteLine(InverseScale(pair.Input[0]) + ", " + InverseScale(pair.Input[1]) + ", " + Chop(InverseScale(output[0])));// + ", " + pair.Ideal[0]);// + ",ideal=" + pair.Ideal[0]);
                    //3D//sw.WriteLine(InverseScale(pair.Input[0]) + ", " + InverseScale(pair.Input[1]) + ", " + InverseScale(pair.Input[2]) + ", " + Chop(InverseScale(output[0])));// + ", " + pair.Ideal[0]);// + ",ideal=" + pair.Ideal[0]);
                    //Console.WriteLine(pair.Input[0] + ", actual=" + output[0] + ",ideal=" + pair.Ideal[0]);
                }
            }

            Console.WriteLine("\nFit output saved to results.csv");
            Console.WriteLine("\nComplete - Please press the 'any' key to close.");
            Console.ReadKey();
        }
 /// <summary>
 /// Generate the RBF network.
 /// </summary>
 /// <returns>The neural network.</returns>
 public BasicNetwork Generate()
 {
     ILayer input = new BasicLayer(new ActivationLinear(), false,
             this.inputNeurons);
     ILayer output = new BasicLayer(new ActivationLinear(), false, this.outputNeurons);
     BasicNetwork network = new BasicNetwork();
     RadialBasisFunctionLayer rbfLayer = new RadialBasisFunctionLayer(
            this.hiddenNeurons);
     network.AddLayer(input);
     network.AddLayer(rbfLayer, SynapseType.Direct);
     network.AddLayer(output);
     network.Structure.FinalizeStructure();
     network.Reset();
     network.TagLayer(RBF_LAYER, rbfLayer);
     rbfLayer.RandomizeRBFCentersAndWidths(this.inputNeurons, -1, 1, RBFEnum.Gaussian);
     int y = PatternConst.START_Y;
     input.X = PatternConst.START_X;
     input.Y = y;
     y += PatternConst.INC_Y;
     rbfLayer.X = PatternConst.START_X;
     rbfLayer.Y = y;
     y += PatternConst.INC_Y;
     output.X = PatternConst.START_X;
     output.Y = y;
     return network;
 }
示例#9
0
        public static String TrainModel(MegasenaListResult dbl, int deepness)
        {
            var deep    = deepness;
            var network = new BasicNetwork();

            network.AddLayer(new BasicLayer(null, true, 6 * deep));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 5 * 6 * deep));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 5 * 6 * deep));
            network.AddLayer(new BasicLayer(new ActivationLinear(), true, 6));

            network.Structure.FinalizeStructure();

            var learningInput = new double[deep][];

            for (int i = 0; i < deep; ++i)
            {
                learningInput[i] = new double[deep * 6];

                for (int j = 0, k = 0; j < deep; ++j)
                {
                    var idx  = 2 * deep - i - j;
                    var data = dbl[idx];
                    learningInput[i][k++] = (double)data.V1;
                    learningInput[i][k++] = (double)data.V2;
                    learningInput[i][k++] = (double)data.V3;
                    learningInput[i][k++] = (double)data.V4;
                    learningInput[i][k++] = (double)data.V5;
                    learningInput[i][k++] = (double)data.V6;
                }
            }

            var learningOutput = new double[deep][];

            for (int i = 0; i < deep; ++i)
            {
                var idx  = deep - 1 - i;
                var data = dbl[idx];

                learningOutput[i] = new double[6]
                {
                    (double)data.V1,
                    (double)data.V2,
                    (double)data.V3,
                    (double)data.V4,
                    (double)data.V5,
                    (double)data.V6
                };
            }

            var trainingSet = new BasicMLDataSet(learningInput, learningOutput);
            var train       = new ResilientPropagation(network, trainingSet);

            train.NumThreads = Environment.ProcessorCount;

START:
            network.Reset();

RETRY:
            var step = 0;

            do
            {
                train.Iteration();
                Console.WriteLine("Train Error: {0}", train.Error);
                ++step;
            }while (train.Error > 0.001 && step < 20);

            var passedCount = 0;

            for (var i = 0; i < deep; ++i)
            {
                var should = new MegasenaResult(learningOutput[i]);
                var inputn = new BasicMLData(6 * deep);

                Array.Copy(learningInput[i], inputn.Data, inputn.Data.Length);

                var comput = new MegasenaResult(((BasicMLData)network.Compute(inputn)).Data);
                var passed = should.ToString() == comput.ToString();

                if (passed)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    ++passedCount;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                }

                Console.WriteLine("{0} {1} {2} {3}",
                                  should.ToString().PadLeft(17, ' '),
                                  passed ? "==" : "!=",
                                  comput.ToString().PadRight(17, ' '),
                                  passed ? "PASS" : "FAIL");
                Console.ResetColor();
            }

            var input = new BasicMLData(6 * deep);

            for (int i = 0, k = 0; i < deep; ++i)
            {
                var idx  = deep - 1 - i;
                var data = dbl[idx];
                input.Data[k++] = (double)data.V1;
                input.Data[k++] = (double)data.V2;
                input.Data[k++] = (double)data.V3;
                input.Data[k++] = (double)data.V4;
                input.Data[k++] = (double)data.V5;
                input.Data[k++] = (double)data.V6;
            }

            var perfect = dbl[0];
            var predict = new MegasenaResult(((BasicMLData)network.Compute(input)).Data);

            Console.ForegroundColor = ConsoleColor.Yellow;
            //Console.WriteLine("Predict: {0}", predict);
            Console.ResetColor();

            if (predict.IsOut())
            {
                goto START;
            }
            if ((double)passedCount < (deep * (double)9 / (double)10) ||
                !predict.IsValid())
            {
                goto RETRY;
            }

            //Console.WriteLine("Press any key for close...");
            //Console.ReadKey(true);

            return(ReturnOrderedPredictResult(predict.ToString()));
        }
        /// <summary>
        /// Generate the Elman neural network.
        /// </summary>
        /// <returns>The Elman neural network.</returns>
        public BasicNetwork Generate()
        {
            int y = PatternConst.START_Y;
            ILayer input = new BasicLayer(this.activation, false,
                   this.inputNeurons);

            BasicNetwork result = new BasicNetwork();
            result.AddLayer(input);

            input.X = PatternConst.START_X;
            input.Y = y;
            y += PatternConst.INC_Y;

            foreach (int count in this.hidden)
            {

                ILayer hidden = new BasicLayer(
                       this.activation, true, count);

                result.AddLayer(hidden);
                hidden.X = PatternConst.START_X;
                hidden.Y = y;
                y += PatternConst.INC_Y;
            }

            ILayer output = new BasicLayer(this.activation, true,
                   this.outputNeurons);
            result.AddLayer(output);
            output.X = PatternConst.START_X;
            output.Y = y;
            y += PatternConst.INC_Y;

            result.Structure.FinalizeStructure();
            result.Reset();

            return result;
        }
示例#11
0
        /// <summary>
        ///     Run the example.
        /// </summary>
        public void Process()
        {
            // read the iris data from the resources
            var assembly = Assembly.GetExecutingAssembly();
            var res      = assembly.GetManifestResourceStream("AIFH_Vol3.Resources.auto-mpg.data.csv");

            // did we fail to read the resouce
            if (res == null)
            {
                Console.WriteLine("Can't read auto MPG data from embedded resources.");
                return;
            }

            // load the data
            var istream = new StreamReader(res);
            var ds      = DataSet.Load(istream);

            istream.Close();

            // The following ranges are setup for the Auto MPG data set.  If you wish to normalize other files you will
            // need to modify the below function calls other files.

            // First remove some columns that we will not use:
            ds.DeleteColumn(8); // Car name
            ds.DeleteColumn(7); // Car origin
            ds.DeleteColumn(6); // Year
            ds.DeleteUnknowns();

            ds.NormalizeZScore(1);
            ds.NormalizeZScore(2);
            ds.NormalizeZScore(3);
            ds.NormalizeZScore(4);
            ds.NormalizeZScore(5);

            var trainingData = ds.ExtractSupervised(1, 4, 0, 1);

            var splitList = DataUtil.Split(trainingData, 0.75);

            trainingData = splitList[0];
            var validationData = splitList[1];

            Console.WriteLine("Size of dataset: " + ds.Count);
            Console.WriteLine("Size of training set: " + trainingData.Count);
            Console.WriteLine("Size of validation set: " + validationData.Count);

            var inputCount = trainingData[0].Input.Length;

            var network = new BasicNetwork();

            network.AddLayer(new BasicLayer(null, true, inputCount));
            network.AddLayer(new BasicLayer(new ActivationReLU(), true, 50));
            network.AddLayer(new BasicLayer(new ActivationReLU(), true, 25));
            network.AddLayer(new BasicLayer(new ActivationReLU(), true, 5));
            network.AddLayer(new BasicLayer(new ActivationLinear(), false, 1));
            network.FinalizeStructure();
            network.Reset();

            var train = new BackPropagation(network, trainingData, 0.000001, 0.9);

            PerformIterationsEarlyStop(train, network, validationData, 20, new ErrorCalculationMSE());
            Query(network, validationData);
        }
示例#12
0
        public static void Run()
        {
            FileInfo networkFile = new FileInfo(@"D:\Imagery\network\network.eg");

            var network = new BasicNetwork();

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

            if (System.IO.File.Exists(@"D:\Imagery\network\network.eg"))
            {
                network = (BasicNetwork)(Encog.Persist.EncogDirectoryPersistence.LoadObject(networkFile));
            }



            Encog.ML.Data.Image.ImageMLDataSet trainingSet = new Encog.ML.Data.Image.ImageMLDataSet(new RGBDownsample(), false, 1, -1);

            Random rnd = new Random();
            //take 1000,, take 5000 from nothing and scramble them
            List <string> fileEntries = Directory.GetFiles(@"D:\Imagery\_Vege").OrderBy(x => rnd.Next()).Take(1000).ToList();

            fileEntries.AddRange(Directory.GetFiles(@"D:\Imagery\_Nothing").OrderBy(x => rnd.Next()).Take(5000).ToArray());
            fileEntries = fileEntries.OrderBy(x => rnd.Next()).Take(6000).ToList();
            foreach (var file in fileEntries)
            {
                var         bitmap = new System.Drawing.Bitmap(file);
                ImageMLData data   = new ImageMLData(bitmap);
                if (file.Contains("_Nothing"))
                {
                    BasicMLData ideal = new BasicMLData(Nothing);
                    trainingSet.Add(data, ideal);
                }
                else
                {
                    BasicMLData ideal = new BasicMLData(Vegetation);
                    trainingSet.Add(data, ideal);
                }
            }
            trainingSet.Downsample(SIZE, SIZE);

            IMLTrain train = new Backpropagation(network, trainingSet, .001, 0.02)
            {
            };


            int epoch = 1;

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

            Encog.Persist.EncogDirectoryPersistence.SaveObject(networkFile, (BasicNetwork)network);
            Encog.ML.Data.Image.ImageMLDataSet testingSet = new Encog.ML.Data.Image.ImageMLDataSet(new RGBDownsample(), false, 1, -1);
            fileEntries = Directory.GetFiles(@"D:\Imagery\_VegeTest").ToList();
            foreach (var file in fileEntries)
            {
                ImageMLData data  = new ImageMLData(new System.Drawing.Bitmap(file));
                BasicMLData ideal = new BasicMLData(Vegetation);
                testingSet.Add(data, ideal);
            }
            fileEntries = Directory.GetFiles(@"D:\Imagery\_NothingTest").ToList();
            foreach (var file in fileEntries)
            {
                ImageMLData data  = new ImageMLData(new System.Drawing.Bitmap(file));
                BasicMLData ideal = new BasicMLData(Nothing);
                testingSet.Add(data, ideal);
            }
            testingSet.Downsample(SIZE, SIZE);

            Console.WriteLine(@"Neural Network Results:");
            foreach (IMLDataPair pair in testingSet)
            {
                IMLData output = network.Compute(pair.Input);
                Console.WriteLine(@", actual (" + output[0] + @"," + output[1] + @"),ideal (" + pair.Ideal[0] + @"," + pair.Ideal[1] + ")");
            }

            EncogFramework.Instance.Shutdown();
        }
示例#13
0
        /// <summary>
        /// Generate the RSOM network.
        /// </summary>
        /// <returns>The neural network.</returns>
        public BasicNetwork Generate()
        {
            ILayer output = new BasicLayer(new ActivationLinear(), false,
                    this.outputNeurons);
            ILayer input = new BasicLayer(new ActivationLinear(), false,
                    this.inputNeurons);

            BasicNetwork network = new BasicNetwork();
            ILayer context = new ContextLayer(this.outputNeurons);
            network.AddLayer(input);
            network.AddLayer(output);

            output.AddNext(context, SynapseType.OneToOne);
            context.AddNext(input);

            int y = PatternConst.START_Y;
            input.X = PatternConst.START_X;
            input.Y = y;

            context.X = PatternConst.INDENT_X;
            context.Y = y;

            y += PatternConst.INC_Y;

            output.X = PatternConst.START_X;
            output.Y = y;

            network.Structure.FinalizeStructure();
            network.Reset();
            return network;
        }
示例#14
0
        static void Main(string[] args)
        {
            /// LIGHT DIODE ARRAY INFO
            double[][] dataIn =
            {
                new double[3]
                {
                    0, 0.5, 0.75
                }
            };

            /// ACTUAL VOLUME
            double[][] dataOut =
            {
                new double[1]
                {
                    0
                }
            };

            /// SETUP: MAKE PUBLIC
            IMLDataSet   mLDataSet = new BasicNeuralDataSet(dataIn, dataOut);
            IMLDataSet   mLInput;
            IMLTrain     mLTrain;
            BasicNetwork basicNetwork = new BasicNetwork();

            /// NEURAL NETWORK 3 > 5 > 5 > 5 > 1
            basicNetwork.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 3));
            basicNetwork.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 5));
            basicNetwork.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 5));
            basicNetwork.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 5));
            basicNetwork.AddLayer(new BasicLayer(new ActivationSigmoid(), true, 1));
            basicNetwork.Structure.FinalizeStructure();
            basicNetwork.Reset();

            mLTrain = new Backpropagation(basicNetwork, mLDataSet);

            /// TRAINING
            float numEpochs = 100;

            for (float i = 0; i <= numEpochs; i++)
            {
                /// PROGRESSBAR
                Console.Clear();
                for (int pi = 0; pi <= (i / numEpochs) * 50; pi++)
                {
                    Console.Write("-");
                }
                Console.WriteLine("> {0}%", (float)((i / numEpochs) * 100), numEpochs);

                /// ITERATION
                mLTrain.Iteration();
            }

            /// INPUT
            while (true)
            {
                var in1 = Console.ReadLine();
                var in2 = Console.ReadLine();
                var in3 = Console.ReadLine();

                double[][] inputIn =
                {
                    new double[3]
                    {
                        double.Parse(in1), double.Parse(in2), double.Parse(in3)
                    }
                };

                mLInput = new BasicNeuralDataSet(inputIn, dataOut);
                IMLData output = basicNetwork.Compute(mLInput[0].Input);

                Console.WriteLine((float)output[0]);
            }
        }
示例#15
0
        public LotteryModel ByMachineLearningExecute()
        {
            try
            {
                if (this.lotteryRule.LotteryType != Enums.LotteryType.TheFiveNumberDraw)
                {
                    return(null);                                                                     //TODO: Make this for 6 and custom lottery draw
                }
                LotteryModel lm  = new LotteryModel(lotteryRule);
                var          dbl = new List <LotteryResult>();
                foreach (LotteryModel lotteryModel in this.lotteryCollection)
                {
                    var res = new LotteryResult(
                        lotteryModel.Numbers[0],
                        lotteryModel.Numbers[1],
                        lotteryModel.Numbers[2],
                        lotteryModel.Numbers[3],
                        lotteryModel.Numbers[4]
                        );

                    dbl.Add(res);
                }

                dbl.Reverse();
                var deep    = 20;
                var network = new BasicNetwork();
                network.AddLayer(
                    new BasicLayer(null, true, 5 * deep));
                network.AddLayer(
                    new BasicLayer(
                        new ActivationSigmoid(), true, 4 * 5 * deep));
                network.AddLayer(
                    new BasicLayer(
                        new ActivationSigmoid(), true, 4 * 5 * deep));
                network.AddLayer(
                    new BasicLayer(
                        new ActivationLinear(), true, 5));
                network.Structure.FinalizeStructure();
                var learningInput = new double[deep][];
                for (int i = 0; i < deep; ++i)
                {
                    learningInput[i] = new double[deep * 5];
                    for (int j = 0, k = 0; j < deep; ++j)
                    {
                        var           idx  = 2 * deep - i - j;
                        LotteryResult data = dbl[idx];
                        learningInput[i][k++] = (double)data.V1;
                        learningInput[i][k++] = (double)data.V2;
                        learningInput[i][k++] = (double)data.V3;
                        learningInput[i][k++] = (double)data.V4;
                        learningInput[i][k++] = (double)data.V5;
                    }
                }
                var learningOutput = new double[deep][];
                for (int i = 0; i < deep; ++i)
                {
                    var idx  = deep - 1 - i;
                    var data = dbl[idx];
                    learningOutput[i] = new double[5]
                    {
                        (double)data.V1,
                        (double)data.V2,
                        (double)data.V3,
                        (double)data.V4,
                        (double)data.V5
                    };
                }
                var trainingSet = new BasicMLDataSet(
                    learningInput,
                    learningOutput);

                var train = new ResilientPropagation(
                    network, trainingSet);
                train.NumThreads = Environment.ProcessorCount;
START:
                network.Reset();
RETRY:
                var step = 0;
                do
                {
                    train.Iteration();
                    Console.WriteLine("Train Error: {0}", train.Error);
                    ++step;
                }while (train.Error > 0.001 && step < 20);
                var passedCount = 0;
                for (var i = 0; i < deep; ++i)
                {
                    var should =
                        new LotteryResult(learningOutput[i]);
                    var inputn = new BasicMLData(5 * deep);
                    Array.Copy(
                        learningInput[i],
                        inputn.Data,
                        inputn.Data.Length);
                    var comput =
                        new LotteryResult(
                            ((BasicMLData)network.
                             Compute(inputn)).Data);
                    var passed = should.ToString() == comput.ToString();
                    if (passed)
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                        ++passedCount;
                    }
                }

                var input = new BasicMLData(5 * deep);
                for (int i = 0, k = 0; i < deep; ++i)
                {
                    var idx  = deep - 1 - i;
                    var data = dbl[idx];
                    input.Data[k++] = (double)data.V1;
                    input.Data[k++] = (double)data.V2;
                    input.Data[k++] = (double)data.V3;
                    input.Data[k++] = (double)data.V4;
                    input.Data[k++] = (double)data.V5;
                }
                var           perfect = dbl[0];
                LotteryResult predict = new LotteryResult(
                    ((BasicMLData)network.Compute(input)).Data);
                Console.WriteLine("Predict: {0}", predict);

                if (predict.IsOut())
                {
                    goto START;
                }

                var t       = passedCount < (deep * (double)9 / (double)10);
                var isvalid = predict.IsValid();

                if (t ||
                    !isvalid)
                {
                    goto RETRY;
                }

                lm.AddNumber(predict.V1);
                lm.AddNumber(predict.V2);
                lm.AddNumber(predict.V3);
                lm.AddNumber(predict.V4);
                lm.AddNumber(predict.V5);

                return(lm);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
                return(null);
            }
        }