예제 #1
0
        protected virtual RBFNetwork CreateNetwork()
        {
            //RBFNetwork network = new RBFNetwork(WindowSize, 1, new IRadialBasisFunction[]{new GaussianFunction()});

            //General setup is the same as before
            double volumeNeuronWidth = 2.0 / numNeuronsPerDimension;

            var pattern = new RadialBasisPattern();

            pattern.InputNeurons  = WindowSize;
            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)System.Math.Pow(numNeuronsPerDimension, WindowSize);

            // int numEdges = (int) (dimensions*Math.Pow(2, dimensions - 1));

            pattern.AddHiddenLayer(numNeurons);

            var network = (RBFNetwork)pattern.Generate();

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

            return(network);
        }
예제 #2
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();
        }