Example #1
0
        protected const int MAX_EPOCHS = 1000;          //Limiting the number of iterations in the process.


        /*
         * CONSTRUCTORS
         */
        public NeuralNetwork(DataSetReader dataSet, double[] target, double bias, double eta)
        {
            this.target      = target;
            this.data        = dataSet.data;
            this.featureMask = VectorTools.sequence(dataSet.features);
            this.classMask   = VectorTools.sequence(dataSet.classes);
            this.weight      = VectorTools.ones(this.featureMask.Length + 1);        //+1 for bias.
            this.samples     = dataSet.samples;
            this.eta         = eta;
            this.bias        = bias;
        }
Example #2
0
 public NeuralNetwork(DataSetReader dataSet, double[] target, double[] weight, double eta, double bias)
 {
     this.target      = target;
     this.data        = dataSet.data;
     this.featureMask = VectorTools.sequence(dataSet.features);
     this.classMask   = VectorTools.sequence(dataSet.classes);
     this.samples     = dataSet.samples;
     this.weight      = weight;
     this.eta         = eta;
     this.bias        = bias;
 }
Example #3
0
        public override double classify(double[] input)
        {
            if (input.Length > this.featureMask.Length)
            {
                throw new ArgumentOutOfRangeException("Input features is bigger than features in feature mask");
            }

            input = VectorTools.prepend(input, this.bias); //Add the bias.
            double net = VectorTools.multiply(this.weight, input);

            return(getvalue(net));
        }
Example #4
0
        public override void train(int trainCount)
        {
            int    epochs    = 0;
            bool   moreError = true;
            double minError  = 1E-2;

            double[][] error         = new double[this.classMask.Length][];
            double[]   mse           = new double[MAX_EPOCHS]; //Mean square error.
            bool       weightChanged = true;
            int        classIndex;

            double[] lineData;

            /*REAL WORK*/
            while (weightChanged && epochs < NeuralNetwork.MAX_EPOCHS && moreError)
            {
                weightChanged = false;
                for (int i = 0; i < this.classMask.Length; i++)            //Class index.
                {
                    error[i]   = new double[trainCount];
                    classIndex = (int)classMask[i] - 1;
                    for (int j = 0; j < trainCount; j++)                //Data index.
                    {
                        lineData = VectorTools.trim(this.data[classIndex][j], this.featureMask);
                        lineData = VectorTools.prepend(lineData, this.bias);                         //Prepend the bias.

                        double net = VectorTools.multiply(this.weight, lineData);
                        net = getvalue(net);

                        if (net != this.target[i])
                        {
                            weightChanged = true;
                            lineData      = VectorTools.trim(this.data[classIndex][j], this.featureMask);
                            lineData      = VectorTools.prepend(lineData, this.bias);

                            error[i][j] = this.target[i] - net;
                            double[] mulOut = VectorTools.multiply(lineData, error[i][j] * this.eta);
                            error[i][j] = 0.5 * error[i][j] * error[i][j];
                            this.weight = VectorTools.sum(this.weight, mulOut);
                        }
                    }
                }                 //End of inner for.

                double[] temp = VectorTools.get1D(error);
                mse[epochs] = VectorTools.mean(temp);
                if (mse[epochs] < minError)
                {
                    moreError = false;
                }
                epochs++;
            }             //End of outer while.
        }
Example #5
0
        public NeuralNetwork(DataSetReader dataSet, double[] target, double eta, double bias, double[] featureMask, double[] classMask)
        {
            if (featureMask.Length > dataSet.features || featureMask.Length < 1)
            {
                throw new ArgumentOutOfRangeException("Number of features specified is not applicable");
            }

            this.target      = target;
            this.data        = dataSet.data;
            this.featureMask = featureMask;
            this.classMask   = classMask;
            this.weight      = VectorTools.ones(this.featureMask.Length + 1);        //+1 for bias.
            this.samples     = dataSet.samples;
            this.eta         = eta;
            this.bias        = bias;
        }
Example #6
0
        public void drawLine(NeuralNetwork network)
        {
            //Checks and getting ready:
            double[] classMask = network.getClassMask(),
            featureMask = network.getFeatureMask(),
            weight      = network.getWeight();
            double bias    = network.getBias(),
                   weightX = weight[0],
                   weightY = weight[1];

            if (classMask.Length != 2)
            {
                throw new ArgumentOutOfRangeException("Mask must contain exactly two classes");
            }

            classMask[0]--; classMask[1]--;             //For using as indices.
            int classOne = (int)classMask[0],
                classTwo = (int)classMask[1];

            //Real work:
            System.Collections.Generic.List <double[]>[] set =
                dataSet.normalized ? dataSet.dataNorm : dataSet.data;
            double maxX = VectorTools.max(set[classOne], featureMask),
                   minX = VectorTools.min(set[classOne], featureMask);

            double xOne = (minX + weightX + bias),
                   xTwo = (maxX + weightY + bias),
                   yOne = ((-xOne * weightX) / weightY) - (bias / weightY),                 //Derived from: x*w1 + y*w2 = 0.
                   yTwo = ((-xTwo * weightX) / weightY) - (bias / weightY);

            if (dataSet.normalized)
            {
                double[] newDataOne = dataSet.unNorm(new double[] { xOne, yOne }, featureMask);
                double[] newDataTwo = dataSet.unNorm(new double[] { xTwo, yTwo }, featureMask);
                xOne = newDataOne[0]; yOne = newDataOne[1];
                xTwo = newDataTwo[0]; yTwo = newDataTwo[1];
            }

            Bitmap   bitmap = this.pictureBox.Image as Bitmap;
            Graphics g      = Graphics.FromImage(bitmap);
            Pen      pen    = new Pen(Color.Black);

            g.DrawLine(pen, (float)xOne * factorX, (float)yOne * factorY, (float)xTwo * factorX, (float)yTwo * factorY);
        }
        /// <summary>Train the machine using the perceptron algorithm.</summary>
        /// <param name="trainCount">Number of data set smaples to use in training.</param>
        public override void train(int trainCount)
        {
            int  epochs        = 0;
            bool weightChanged = true;
            int  classIndex;

            double[] lineData;

            /*REAL WORK*/
            while (weightChanged && epochs < NeuralNetwork.MAX_EPOCHS)
            {
                weightChanged = false;
                for (int i = 0; i < this.classMask.Length; i++)            //Class index.
                {
                    classIndex = (int)classMask[i] - 1;
                    for (int j = 0; j < trainCount; j++)                //Data index.
                    {
                        lineData = VectorTools.trim(this.data[classIndex][j], this.featureMask);
                        lineData = VectorTools.prepend(lineData, this.bias);                         //Prepend the bias.

                        double net    = VectorTools.multiply(this.weight, lineData);
                        int    sgnOut = ActivationFunctions.signum(net);

                        if (sgnOut != this.target[i])
                        {
                            weightChanged = true;
                            lineData      = VectorTools.trim(this.data[classIndex][j], this.featureMask);
                            lineData      = VectorTools.prepend(lineData, this.bias);

                            double   error  = this.target[i] - sgnOut;
                            double[] mulOut = VectorTools.multiply(lineData, error * this.eta);
                            this.weight = VectorTools.sum(this.weight, mulOut);
                        }
                    }
                }                 //End of inner for.

                epochs++;
            }             //End of outer while.
        }