Ejemplo n.º 1
0
        /// <summary>
        ///   Constructs a new Multi-class Kernel Support Vector Machine
        /// </summary>
        /// <param name="kernel">The chosen kernel for the machine.</param>
        /// <param name="inputs">The number of inputs for the machine.</param>
        /// <param name="classes">The number of classes in the classification problem.</param>
        /// <remarks>
        ///   If the number of inputs is zero, this means the machine
        ///   accepts a indefinite number of inputs. This is often the
        ///   case for kernel vector machines using a sequence kernel.
        /// </remarks>
        public MulticlassSupportVectorMachine(int inputs, IKernel kernel, int classes)
        {
            if (classes <= 1)
            {
                throw new ArgumentException("The machine must have at least two classes.", "classes");
            }

            // Create the kernel machines
            machines = new KernelSupportVectorMachine[classes - 1][];
            for (int i = 0; i < classes - 1; i++)
            {
                machines[i] = new KernelSupportVectorMachine[i + 1];
                for (int j = 0; j <= i; j++)
                {
                    machines[i][j] = new KernelSupportVectorMachine(kernel, inputs);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///   Computes the given input to produce the corresponding output.
        /// </summary>
        /// <param name="inputs">An input vector.</param>
        /// <param name="votes">A vector containing the number of votes for each class.</param>
        /// <returns>The output for the given input.</returns>
        public int Compute(double[] inputs, out int[] votes)
        {
            // out variables cannot be passed into delegates,
            // so will be creating a copy for the vote array.
            int[] voting = new int[this.Classes];


            // For each class
            GABIZ.Base.Parallel.For(0, Classes, i =>
            {
                // For each other class
                for (int j = 0; j < i; j++)
                {
                    KernelSupportVectorMachine machine = this[i, j];

                    double answer = machine.Compute(inputs);

                    // Compute the two-class problem
                    if (answer < 0)
                    {
                        voting[i] += 1; // Class i has won
                    }
                    else
                    {
                        voting[j] += 1; // Class j has won
                    }
                }
            });

            // Voting finished.
            votes = voting;

            // Select class which maximum number of votes
            int output; Matrix.Max(votes, out output);

            return(output); // Return as the output.
        }
Ejemplo n.º 3
0
        public static KernelSupportVectorMachine stringToSVM(string s)
        {
            string[] sP = s.Split('~');

            int inputs = Convert.ToInt32(sP[0]); //Read inputs
            int l      = Convert.ToInt32(sP[1]); //Read number of support vectors

            string[] temp = sP[2].Split(' ');    //Read first support vector
            int      n    = temp.Length;

            double[,] supportVectors = new double[l, n];
            for (int i = 0; i < n; i++)
            {
                supportVectors[0, i] = Convert.ToDouble(temp[i]);
            }
            for (int i = 1; i < l; i++)
            {
                temp = sP[2 + i].Split(' ');
                for (int j = 0; j < n; j++)
                {
                    supportVectors[i, j] = Convert.ToDouble(temp[j]);
                }
            }

            double threshold = Convert.ToDouble(sP[2 + l]);

            string[] _weights = sP[3 + l].Split(' '); //Read weight;
            int      wL       = _weights.Length;

            double[] weights = new double[wL];
            for (int i = 0; i < wL; i++)
            {
                weights[i] = Convert.ToDouble(_weights[i]);
            }

            IKernel kernel = new Polynomial(2, 1);

            string kernelString = sP[4 + l];

            switch (kernelString)
            {
            case "Gaussian": kernel = new Statistics.Kernels.Gaussian(Convert.ToDouble(sP[5 + l]));
                break;

            case "Polynomial": kernel = new Polynomial(Convert.ToInt32(sP[5 + l]), Convert.ToDouble(sP[6 + l]));
                break;

            case "Linear": kernel = new Linear(Convert.ToDouble(sP[5 + l]));
                break;

            case "Sigmoid": kernel = new Sigmoid(Convert.ToDouble(sP[5 + l]), Convert.ToDouble(sP[6 + l]));
                break;
            }
            KernelSupportVectorMachine svm = new KernelSupportVectorMachine(kernel, inputs);

            svm.SupportVectors = supportVectors.ToArray();
            svm.Threshold      = threshold;
            svm.Weights        = weights;

            return(svm);
        }
Ejemplo n.º 4
0
        public static string svmToString(KernelSupportVectorMachine svm)
        {
            if (svm == null)
            {
                return(null);
            }

            string s    = "";
            string temp = "";

            s += svm.Inputs + "~"; //Write input
            int l = svm.SupportVectors.Length;

            s += l + "~"; //Length of support vectors
            for (int i = 0; i < l; i++)
            {
                int n = svm.SupportVectors[i].Length;
                temp = "";
                for (int j = 0; j < n; j++)
                {
                    temp += svm.SupportVectors[i][j] + " ";
                }
                temp = temp.Trim();
                s   += temp + "~";
            }

            s += svm.Threshold + "~";

            l = svm.Weights.Length;

            temp = "";
            for (int i = 0; i < l; i++)
            {
                temp += svm.Weights[i] + " ";
            }
            temp = temp.Trim();
            s   += temp + "~";

            string[] _kType = svm.Kernel.ToString().Split('.');
            int      u      = _kType.Length;
            string   kType  = _kType[u - 1];

            switch (kType)
            {
            case "Gaussian": Statistics.Kernels.Gaussian gKernel = svm.Kernel as Statistics.Kernels.Gaussian;
                s += "Gaussian" + "~";
                s += gKernel.Sigma;
                break;

            case "Polynomial": Polynomial pKernel = svm.Kernel as Polynomial;
                s += "Polynomial" + "~";
                s += pKernel.Degree + "~";
                s += pKernel.Constant;
                break;

            case "Linear": Linear lKernel = svm.Kernel as Linear;
                s += "Linear" + "~";
                s += lKernel.Constant;
                break;

            case "Sigmoid": Sigmoid sKernel = svm.Kernel as Sigmoid;
                s += "Sigmoid" + "~";
                s += sKernel.Gamma + "~";
                s += sKernel.Gamma;
                break;
            }
            return(s);
        }