Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encog C# RBF network, how to start?

I went through whole documantation and didnt find how to set RBF network. I found some RBF example in ConsoleExmpales/Examples/Radial, but it looks like it doesnt work anymore, beceause some methods have been changed in Encog.

So far I am stuck on this:

    public static double[][] XORInput = {
        new[] {0.0, 0.0},
        new[] {1.0, 0.0},
        new[] {0.0, 1.0},
        new[] {1.0, 1.0}
    };

    public static double[][] XORIdeal = {
        new[] {0.0},
        new[] {1.0},
        new[] {1.0},
        new[] {0.0}
    };

        int dimension = 8;
        int numNeuronsPerDimension = 64;
        double volumeNeuronWidth = 2.0 / numNeuronsPerDimension;
        bool includeEdgeRBFs = true;

        RBFNetwork n = new RBFNetwork(dimension, numNeuronsPerDimension, 1, RBFEnum.Gaussian);
        n.SetRBFCentersAndWidthsEqualSpacing(0, 1, RBFEnum.Gaussian, volumeNeuronWidth, includeEdgeRBFs);
        //n.RandomizeRBFCentersAndWidths(0, 1, RBFEnum.Gaussian);

        INeuralDataSet trainingSet = new BasicNeuralDataSet(XORInput, XORIdeal);
        SVDTraining train = new SVDTraining(n, trainingSet);

        int epoch = 1;
        do
        {
            train.Iteration();
            Console.WriteLine("Epoch #" + epoch + " Error:" + train.Error);
            epoch++;
        } while ((epoch < 1) && (train.Error > 0.001));

When I run this, I get error "Total number of RBF neurons must be some integer to the power of 'dimensions'." on SetRBFCentersAndWidthsEqualSpacing. It works if I change this method for RandomizeRBFCentersAndWidths until train.iteration() is reached, where i get " Index was outside the bounds of the array".

I understand how RBF network works, but I am confused from all parameters in SetRBFCentersAndWidthsEqualSpacing method, can someone explain it more detail?.

like image 304
EdWood Avatar asked Aug 21 '16 22:08

EdWood


1 Answers

Very good question.

  1. SetRBFCentersAndWidthsEqualSpacing and here is relatively new method of training neural networks and Jeff Heaton decided to implement it.
  2. Looks like there is a difference between Java version and C# version at lines 230 - 240 and IMHO bug sits in Java version.

  3. I've modified your code in order for it to be workable with additional comments:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Encog.MathUtil.RBF;
    using Encog.Neural.Data.Basic;
    using Encog.Neural.NeuralData;
    using Encog.Neural.Rbf.Training;
    using Encog.Neural.RBF;
    
    namespace TestRBF
    {
        class Program
        {
            public static double[][] XORInput = {
            new[] {0.0, 0.0},
            new[] {1.0, 0.0},
            new[] {0.0, 1.0},
            new[] {1.0, 1.0}
        };
    
            public static double[][] XORIdeal = {
            new[] {0.0},
            new[] {1.0},
            new[] {1.0},
            new[] {0.0}
        };
    
            static void Main(string[] args)
            {
                int dimension = 2; // XORInput provides two-dimensional inputs. Not 8. 
                /*
                If XORInput is  8 dimensional  it should be like this:
    
                public static double[][] XORInput = {
                new[] {0.0, 0.0,0.0, 0.0,0.0, 0.0,0.0, 0.0}, 
                .
                .   
                .*/
                int numNeuronsPerDimension = 4; // could be also 16, 64, 256. I suppose it should accept 8, 32 but it needs additional investigation
                double volumeNeuronWidth = 2.0 / numNeuronsPerDimension;
                bool includeEdgeRBFs = true;
    
                RBFNetwork n = new RBFNetwork(dimension, numNeuronsPerDimension, 1, RBFEnum.Gaussian);
                n.SetRBFCentersAndWidthsEqualSpacing(0, 1, RBFEnum.Gaussian, volumeNeuronWidth, includeEdgeRBFs);
                //n.RandomizeRBFCentersAndWidths(0, 1, RBFEnum.Gaussian);
    
                INeuralDataSet trainingSet = new BasicNeuralDataSet(XORInput, XORIdeal);
                SVDTraining train = new SVDTraining(n, trainingSet);
    
                int epoch = 1;
                do
                {
                    train.Iteration();
                    Console.WriteLine("Epoch #" + epoch + " Error:" + train.Error);
                    epoch++;
                } while ((epoch < 1) && (train.Error > 0.001));
    
            }
        }
    }
    
like image 80
Yuriy Zaletskyy Avatar answered Oct 02 '22 04:10

Yuriy Zaletskyy