Beispiel #1
0
        /// <summary>
        /// 初期化
        /// </summary>
        /// <param name="svmType">SVMの種類</param>
        /// <param name="kernelType">SVMカーネルの種類</param>
        /// <param name="degree">poly 用</param>
        /// <param name="gamma">poly/rbf/sigmoid 用</param>
        /// <param name="coef0">poly/sigmoid 用</param>
        /// <param name="c">SVMType.CSvc, SVMType.EpsSvr, SVMType.NuSvr 用</param>
        /// <param name="nu">SVMType.NuSvc, SVMType.OneClass, SVMType.NuSvr 用</param>
        /// <param name="p">SVMType.EpsSvr 用</param>
        /// <param name="classWeights">SVMType.CSvc 用</param>
        /// <param name="termCrit">終了条件</param>
#else
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="svmType">Type of SVM</param>
        /// <param name="kernelType">The kernel type</param>
        /// <param name="degree">for poly</param>
        /// <param name="gamma">for poly/rbf/sigmoid</param>
        /// <param name="coef0">for poly/sigmoid</param>
        /// <param name="c">for SVMType.CSvc, SVMType.EpsSvr and SVMType.NuSvr</param>
        /// <param name="nu">for SVMType.NuSvc, SVMType.OneClass and SVMType.NuSvr</param>
        /// <param name="p">for SVMType.EpsSvr</param>
        /// <param name="classWeights">for SVMType.CSvc</param>
        /// <param name="termCrit">Termination criteria</param>
#endif
        public CvSVMParams(SVMType svmType, SVMKernelType kernelType, double degree,
                           double gamma, double coef0, double c, double nu, double p,
                           CvMat classWeights, CvTermCriteria termCrit)
        {
            data = new WCvSVMParams();
            NativeMethods.ml_CvSVMParams_new2(
                ref data, (int)svmType, (int)kernelType, degree, gamma, coef0,
                c, nu, p, Cv2.ToPtr(classWeights), termCrit);
        }
Beispiel #2
0
        public void setSVMType(SVMType type)
        {
            switch (type)
            {
            case SVMType.C:
                _param.SVMType = SVMParam.SVM_TYPE_C_SVC;
                break;

            case SVMType.nu:
                _param.SVMType = SVMParam.SVM_TYPE_NU_SVC;
                break;
            }
        }
Beispiel #3
0
        public void setSVMType(SVMType type)
        {
            switch (type)
            {
            case SVMType.nu:
                _param.SVMType = SVMParam.SVM_TYPE_NU_SVR;
                break;

            case SVMType.epsilon:
                _param.SVMType = SVMParam.SVM_TYPE_EPSILON_SVR;
                break;
            }
        }
Beispiel #4
0
 /// <summary>
 /// Construct the object.
 /// </summary>
 public SVMPattern()
 {
     Regression = true;
     _kernelType = KernelType.RadialBasisFunction;
     _svmType = SVMType.EpsilonSupportVectorRegression;
 }
        /// <summary>
        /// Construct a SVM network.
        /// </summary>
        ///
        /// <param name="theInputCount">The input count.</param>
        /// <param name="svmType">The type of SVM.</param>
        /// <param name="kernelType">The SVM kernal type.</param>
        public SupportVectorMachine(int theInputCount, SVMType svmType,
                                    KernelType kernelType)
        {
            _inputCount = theInputCount;

            _paras = new svm_parameter();

            switch (svmType)
            {
            case SVMType.SupportVectorClassification:
                _paras.svm_type = svm_parameter.C_SVC;
                break;

            case SVMType.NewSupportVectorClassification:
                _paras.svm_type = svm_parameter.NU_SVC;
                break;

            case SVMType.SupportVectorOneClass:
                _paras.svm_type = svm_parameter.ONE_CLASS;
                break;

            case SVMType.EpsilonSupportVectorRegression:
                _paras.svm_type = svm_parameter.EPSILON_SVR;
                break;

            case SVMType.NewSupportVectorRegression:
                _paras.svm_type = svm_parameter.NU_SVR;
                break;

            default:
                throw new NeuralNetworkError("Invalid svm type");
            }

            switch (kernelType)
            {
            case KernelType.Linear:
                _paras.kernel_type = svm_parameter.LINEAR;
                break;

            case KernelType.Poly:
                _paras.kernel_type = svm_parameter.POLY;
                break;

            case KernelType.RadialBasisFunction:
                _paras.kernel_type = svm_parameter.RBF;
                break;

            case KernelType.Sigmoid:
                _paras.kernel_type = svm_parameter.SIGMOID;
                break;

            /*case Encog.ML.SVM.KernelType.Precomputed:
             * this.paras.kernel_type = Encog.MathUtil.LIBSVM.svm_parameter.PRECOMPUTED;
             *          break;*/
            default:
                throw new NeuralNetworkError("Invalid kernel type");
            }

            // params[i].kernel_type = svm_parameter.RBF;
            _paras.degree       = DefaultDegree;
            _paras.coef0        = 0;
            _paras.nu           = DefaultNu;
            _paras.cache_size   = DefaultCacheSize;
            _paras.C            = 1;
            _paras.eps          = DefaultEps;
            _paras.p            = DefaultP;
            _paras.shrinking    = 1;
            _paras.probability  = 0;
            _paras.nr_weight    = 0;
            _paras.weight_label = new int[0];
            _paras.weight       = new double[0];
            _paras.gamma        = 1.0d / _inputCount;
        }
        /// <summary>
        /// Construct a SVM network.
        /// </summary>
        ///
        /// <param name="theInputCount">The input count.</param>
        /// <param name="svmType">The type of SVM.</param>
        /// <param name="kernelType">The SVM kernal type.</param>
        public SupportVectorMachine(int theInputCount, SVMType svmType,
            KernelType kernelType)
        {
            _inputCount = theInputCount;

            _paras = new svm_parameter();

            switch (svmType)
            {
                case SVMType.SupportVectorClassification:
                    _paras.svm_type = svm_parameter.C_SVC;
                    break;
                case SVMType.NewSupportVectorClassification:
                    _paras.svm_type = svm_parameter.NU_SVC;
                    break;
                case SVMType.SupportVectorOneClass:
                    _paras.svm_type = svm_parameter.ONE_CLASS;
                    break;
                case SVMType.EpsilonSupportVectorRegression:
                    _paras.svm_type = svm_parameter.EPSILON_SVR;
                    break;
                case SVMType.NewSupportVectorRegression:
                    _paras.svm_type = svm_parameter.NU_SVR;
                    break;
                default:
                    throw new NeuralNetworkError("Invalid svm type");
            }

            switch (kernelType)
            {
                case KernelType.Linear:
                    _paras.kernel_type = svm_parameter.LINEAR;
                    break;
                case KernelType.Poly:
                    _paras.kernel_type = svm_parameter.POLY;
                    break;
                case KernelType.RadialBasisFunction:
                    _paras.kernel_type = svm_parameter.RBF;
                    break;
                case KernelType.Sigmoid:
                    _paras.kernel_type = svm_parameter.SIGMOID;
                    break;
                    /*case Encog.ML.SVM.KernelType.Precomputed:
                this.paras.kernel_type = Encog.MathUtil.LIBSVM.svm_parameter.PRECOMPUTED;
                break;*/
                default:
                    throw new NeuralNetworkError("Invalid kernel type");
            }

            // params[i].kernel_type = svm_parameter.RBF;
            _paras.degree = DefaultDegree;
            _paras.coef0 = 0;
            _paras.nu = DefaultNu;
            _paras.cache_size = DefaultCacheSize;
            _paras.C = 1;
            _paras.eps = DefaultEps;
            _paras.p = DefaultP;
            _paras.shrinking = 1;
            _paras.probability = 0;
            _paras.nr_weight = 0;
            _paras.weight_label = new int[0];
            _paras.weight = new double[0];
            _paras.gamma = 1.0d/_inputCount;
        }
Beispiel #7
0
 /// <summary>
 /// Construct the object.
 /// </summary>
 public SVMPattern()
 {
     Regression  = true;
     _kernelType = KernelType.RadialBasisFunction;
     _svmType    = SVMType.EpsilonSupportVectorRegression;
 }
Beispiel #8
0
        /// <summary>
        /// Create the SVM.
        /// </summary>
        ///
        /// <param name="architecture">The architecture string.</param>
        /// <param name="input">The input count.</param>
        /// <param name="output">The output count.</param>
        /// <returns>The newly created SVM.</returns>
        public IMLMethod Create(String architecture, int input,
                                int output)
        {
            IList <String> layers = ArchitectureParse.ParseLayers(architecture);

            if (layers.Count != MAX_LAYERS)
            {
                throw new EncogError(
                          "SVM's must have exactly three elements, separated by ->.");
            }

            ArchitectureLayer inputLayer = ArchitectureParse.ParseLayer(
                layers[0], input);
            ArchitectureLayer paramsLayer = ArchitectureParse.ParseLayer(
                layers[1], input);
            ArchitectureLayer outputLayer = ArchitectureParse.ParseLayer(
                layers[2], output);

            String name       = paramsLayer.Name;
            String kernelStr  = paramsLayer.Params.ContainsKey("KERNEL") ? paramsLayer.Params["KERNEL"] : null;
            String svmTypeStr = paramsLayer.Params.ContainsKey("TYPE") ? paramsLayer.Params["TYPE"] : null;

            SVMType    svmType    = SVMType.NewSupportVectorClassification;
            KernelType kernelType = KernelType.RadialBasisFunction;

            bool useNew = true;

            if (svmTypeStr == null)
            {
                useNew = true;
            }
            else if (svmTypeStr.Equals("NEW", StringComparison.InvariantCultureIgnoreCase))
            {
                useNew = true;
            }
            else if (svmTypeStr.Equals("OLD", StringComparison.InvariantCultureIgnoreCase))
            {
                useNew = false;
            }
            else
            {
                throw new EncogError("Unsupported type: " + svmTypeStr
                                     + ", must be NEW or OLD.");
            }

            if (name.Equals("C", StringComparison.InvariantCultureIgnoreCase))
            {
                if (useNew)
                {
                    svmType = SVMType.NewSupportVectorClassification;
                }
                else
                {
                    svmType = SVMType.SupportVectorClassification;
                }
            }
            else if (name.Equals("R", StringComparison.InvariantCultureIgnoreCase))
            {
                if (useNew)
                {
                    svmType = SVMType.NewSupportVectorRegression;
                }
                else
                {
                    svmType = SVMType.EpsilonSupportVectorRegression;
                }
            }
            else
            {
                throw new EncogError("Unsupported mode: " + name
                                     + ", must be C for classify or R for regression.");
            }

            if (kernelStr == null)
            {
                kernelType = KernelType.RadialBasisFunction;
            }
            else if ("linear".Equals(kernelStr, StringComparison.InvariantCultureIgnoreCase))
            {
                kernelType = KernelType.Linear;
            }
            else if ("poly".Equals(kernelStr, StringComparison.InvariantCultureIgnoreCase))
            {
                kernelType = KernelType.Poly;
            }
            else if ("precomputed".Equals(kernelStr, StringComparison.InvariantCultureIgnoreCase))
            {
                kernelType = KernelType.Precomputed;
            }
            else if ("rbf".Equals(kernelStr, StringComparison.InvariantCultureIgnoreCase))
            {
                kernelType = KernelType.RadialBasisFunction;
            }
            else if ("sigmoid".Equals(kernelStr, StringComparison.InvariantCultureIgnoreCase))
            {
                kernelType = KernelType.Sigmoid;
            }
            else
            {
                throw new EncogError("Unsupported kernel: " + kernelStr
                                     + ", must be linear,poly,precomputed,rbf or sigmoid.");
            }

            int inputCount  = inputLayer.Count;
            int outputCount = outputLayer.Count;

            if (outputCount != 1)
            {
                throw new EncogError("SVM can only have an output size of 1.");
            }

            var result = new SupportVectorMachine(inputCount, svmType, kernelType);

            return(result);
        }
        /// <summary>
        /// Construct a SVM network. 
        /// </summary>
        /// <param name="inputCount">The input count.</param>
        /// <param name="outputCount">The output count.</param>
        /// <param name="svmType">The type of SVM.</param>
        /// <param name="kernelType">The SVM kernal type.</param>
        public SVMNetwork(int inputCount, int outputCount, SVMType svmType,
                KernelType kernelType)
        {
            this.inputCount = inputCount;
            this.outputCount = outputCount;
            this.kernelType = kernelType;
            this.svmType = svmType;

            models = new svm_model[outputCount];
            parameters = new svm_parameter[outputCount];

            for (int i = 0; i < outputCount; i++)
            {
                parameters[i] = new svm_parameter();

                switch (svmType)
                {
                    case SVMType.SupportVectorClassification:
                        parameters[i].svm_type = svm_parameter.C_SVC;
                        break;
                    case SVMType.NewSupportVectorClassification:
                        parameters[i].svm_type = svm_parameter.NU_SVC;
                        break;
                    case SVMType.SupportVectorOneClass:
                        parameters[i].svm_type = svm_parameter.ONE_CLASS;
                        break;
                    case SVMType.EpsilonSupportVectorRegression:
                        parameters[i].svm_type = svm_parameter.EPSILON_SVR;
                        break;
                    case SVMType.NewSupportVectorRegression:
                        parameters[i].svm_type = svm_parameter.NU_SVR;
                        break;
                }

                switch (kernelType)
                {
                    case KernelType.Linear:
                        parameters[i].kernel_type = svm_parameter.LINEAR;
                        break;
                    case KernelType.Poly:
                        parameters[i].kernel_type = svm_parameter.POLY;
                        break;
                    case KernelType.RadialBasisFunction:
                        parameters[i].kernel_type = svm_parameter.RBF;
                        break;
                    case KernelType.Sigmoid:
                        parameters[i].kernel_type = svm_parameter.SIGMOID;
                        break;
                }

                parameters[i].kernel_type = svm_parameter.RBF;
                parameters[i].degree = 3;
                parameters[i].coef0 = 0;
                parameters[i].nu = 0.5;
                parameters[i].cache_size = 100;
                parameters[i].C = 1;
                parameters[i].eps = 1e-3;
                parameters[i].p = 0.1;
                parameters[i].shrinking = 1;
                parameters[i].probability = 0;
                parameters[i].nr_weight = 0;
                parameters[i].weight_label = new int[0];
                parameters[i].weight = new double[0];
                parameters[i].gamma = 1.0 / inputCount;
            }
        }
Beispiel #10
0
        /// <summary>
	    /// 初期化
	    /// </summary>
	    /// <param name="_svm_type">SVMの種類</param>
	    /// <param name="_kernel_type">SVMカーネルの種類</param>
	    /// <param name="_degree">poly 用</param>
	    /// <param name="_gamma">poly/rbf/sigmoid 用</param>
	    /// <param name="_coef0">poly/sigmoid 用</param>
	    /// <param name="_C">SVMType.CSvc, SVMType.EpsSvr, SVMType.NuSvr 用</param>
	    /// <param name="_nu">SVMType.NuSvc, SVMType.OneClass, SVMType.NuSvr 用</param>
	    /// <param name="p">SVMType.EpsSvr 用</param>
	    /// <param name="_class_weights">SVMType.CSvc 用</param>
	    /// <param name="_term_crit">終了条件</param>
#else
		/// <summary>
	    /// Constructor
	    /// </summary>
	    /// <param name="_svm_type">Type of SVM</param>
	    /// <param name="_kernel_type">The kernel type</param>
	    /// <param name="_degree">for poly</param>
	    /// <param name="_gamma">for poly/rbf/sigmoid</param>
	    /// <param name="_coef0">for poly/sigmoid</param>
	    /// <param name="_C">for SVMType.CSvc, SVMType.EpsSvr and SVMType.NuSvr</param>
	    /// <param name="_nu">for SVMType.NuSvc, SVMType.OneClass and SVMType.NuSvr</param>
	    /// <param name="_p">for SVMType.EpsSvr</param>
	    /// <param name="_class_weights">for SVMType.CSvc</param>
	    /// <param name="_term_crit">Termination criteria</param>
#endif
		public CvSVMParams(SVMType _svm_type, SVMKernelType _kernel_type, double _degree, double _gamma, double _coef0, 
            double _C, double _nu, double _p, CvMat _class_weights, CvTermCriteria _term_crit )
	    {
            _data = new WCvSVMParams();
            IntPtr _class_weights_ptr = (_class_weights == null) ? IntPtr.Zero : _class_weights.CvPtr;
            MLInvoke.CvSVMParams_construct(ref _data, (int)_svm_type, (int)_kernel_type, _degree, _gamma, _coef0, _C, _nu, _p, _class_weights_ptr, _term_crit);
	    }
Beispiel #11
0
        /// <summary>
        /// Construct a SVM network.
        /// </summary>
        /// <param name="inputCount">The input count.</param>
        /// <param name="outputCount">The output count.</param>
        /// <param name="svmType">The type of SVM.</param>
        /// <param name="kernelType">The SVM kernal type.</param>
        public SVMNetwork(int inputCount, int outputCount, SVMType svmType,
                          KernelType kernelType)
        {
            this.inputCount  = inputCount;
            this.outputCount = outputCount;
            this.kernelType  = kernelType;
            this.svmType     = svmType;

            models     = new svm_model[outputCount];
            parameters = new svm_parameter[outputCount];

            for (int i = 0; i < outputCount; i++)
            {
                parameters[i] = new svm_parameter();

                switch (svmType)
                {
                case SVMType.SupportVectorClassification:
                    parameters[i].svm_type = svm_parameter.C_SVC;
                    break;

                case SVMType.NewSupportVectorClassification:
                    parameters[i].svm_type = svm_parameter.NU_SVC;
                    break;

                case SVMType.SupportVectorOneClass:
                    parameters[i].svm_type = svm_parameter.ONE_CLASS;
                    break;

                case SVMType.EpsilonSupportVectorRegression:
                    parameters[i].svm_type = svm_parameter.EPSILON_SVR;
                    break;

                case SVMType.NewSupportVectorRegression:
                    parameters[i].svm_type = svm_parameter.NU_SVR;
                    break;
                }

                switch (kernelType)
                {
                case KernelType.Linear:
                    parameters[i].kernel_type = svm_parameter.LINEAR;
                    break;

                case KernelType.Poly:
                    parameters[i].kernel_type = svm_parameter.POLY;
                    break;

                case KernelType.RadialBasisFunction:
                    parameters[i].kernel_type = svm_parameter.RBF;
                    break;

                case KernelType.Sigmoid:
                    parameters[i].kernel_type = svm_parameter.SIGMOID;
                    break;
                }

                parameters[i].kernel_type  = svm_parameter.RBF;
                parameters[i].degree       = 3;
                parameters[i].coef0        = 0;
                parameters[i].nu           = 0.5;
                parameters[i].cache_size   = 100;
                parameters[i].C            = 1;
                parameters[i].eps          = 1e-3;
                parameters[i].p            = 0.1;
                parameters[i].shrinking    = 1;
                parameters[i].probability  = 0;
                parameters[i].nr_weight    = 0;
                parameters[i].weight_label = new int[0];
                parameters[i].weight       = new double[0];
                parameters[i].gamma        = 1.0 / inputCount;
            }
        }
Beispiel #12
0
        /// <summary>
	    /// 初期化
	    /// </summary>
        /// <param name="svmType">SVMの種類</param>
        /// <param name="kernelType">SVMカーネルの種類</param>
        /// <param name="degree">poly 用</param>
        /// <param name="gamma">poly/rbf/sigmoid 用</param>
        /// <param name="coef0">poly/sigmoid 用</param>
	    /// <param name="c">SVMType.CSvc, SVMType.EpsSvr, SVMType.NuSvr 用</param>
	    /// <param name="nu">SVMType.NuSvc, SVMType.OneClass, SVMType.NuSvr 用</param>
	    /// <param name="p">SVMType.EpsSvr 用</param>
        /// <param name="classWeights">SVMType.CSvc 用</param>
        /// <param name="termCrit">終了条件</param>
#else
		/// <summary>
	    /// Constructor
	    /// </summary>
	    /// <param name="svmType">Type of SVM</param>
	    /// <param name="kernelType">The kernel type</param>
	    /// <param name="degree">for poly</param>
	    /// <param name="gamma">for poly/rbf/sigmoid</param>
	    /// <param name="coef0">for poly/sigmoid</param>
	    /// <param name="c">for SVMType.CSvc, SVMType.EpsSvr and SVMType.NuSvr</param>
	    /// <param name="nu">for SVMType.NuSvc, SVMType.OneClass and SVMType.NuSvr</param>
	    /// <param name="p">for SVMType.EpsSvr</param>
	    /// <param name="classWeights">for SVMType.CSvc</param>
	    /// <param name="termCrit">Termination criteria</param>
#endif
		public CvSVMParams(SVMType svmType, SVMKernelType kernelType, double degree, 
            double gamma, double coef0, double c, double nu, double p, 
            CvMat classWeights, CvTermCriteria termCrit )
	    {
            data = new WCvSVMParams();
            NativeMethods.ml_CvSVMParams_new2(
                ref data, (int)svmType, (int)kernelType, degree, gamma, coef0,
                c, nu, p, Cv2.ToPtr(classWeights), termCrit);
	    }