Exemple #1
0
        // SVMPREDICT returns a vector of predictions using a trained SVM model
        //(svm_train).
        //
        // pred = SVMPREDICT(model, X) returns a vector of predictions using a
        // trained SVM model (svm_train). X is a mxn matrix where there each
        // example is a row. model is a svm model returned from svm_train.
        // predictions pred is a m x 1 column of predictions of {0, 1} values.
        //
        // Converted to R by: SD Separa (2016/03/18)
        // Converted to C# by: SD Separa (2018/09/29)
        public ManagedArray Predict(ManagedArray input)
        {
            var predictions = new ManagedArray(1, Rows(input));

            if (Trained)
            {
                var x = new ManagedArray(input);

                if (Cols(x) == 1)
                {
                    ManagedMatrix.Transpose(x, input);
                }
                else
                {
                    ManagedOps.Copy2D(x, input, 0, 0);
                }

                var m = Rows(x);

                predictions.Resize(1, m);

                if (Type == KernelType.LINEAR)
                {
                    ManagedMatrix.Multiply(predictions, x, W);
                    ManagedMatrix.Add(predictions, B);
                }
                else if (Type == KernelType.GAUSSIAN || Type == KernelType.RADIAL)
                {
                    // RBF Kernel
                    // This is equivalent to computing the kernel on every pair of examples
                    var pX1 = ManagedMatrix.Pow(x, 2);
                    var pX2 = ManagedMatrix.Pow(ModelX, 2);
                    var rX2 = ManagedMatrix.RowSums(pX2);

                    var X1 = ManagedMatrix.RowSums(pX1);
                    var X2 = ManagedMatrix.Transpose(rX2);
                    var tX = ManagedMatrix.Transpose(ModelX);
                    var tY = ManagedMatrix.Transpose(ModelY);
                    var tA = ManagedMatrix.Transpose(Alpha);

                    var rows = Rows(X1);
                    var cols = Cols(X2);

                    var tempK = new ManagedArray(cols, rows);
                    var temp1 = new ManagedArray(cols, rows);
                    var temp2 = ManagedMatrix.Multiply(x, tX);

                    ManagedMatrix.Multiply(temp2, -2);

                    ManagedMatrix.Expand(X1, cols, 1, tempK);
                    ManagedMatrix.Expand(X2, 1, rows, temp1);

                    ManagedMatrix.Add(tempK, temp1);
                    ManagedMatrix.Add(tempK, temp2);

                    var sigma = KernelParam.Length() > 0 ? KernelParam[0] : 1;

                    if (Type == KernelType.RADIAL)
                    {
                        ManagedMatrix.Sqrt(tempK);
                    }

                    var g = Math.Abs(sigma) > 0 ? Math.Exp(-1 / (2 * sigma * sigma)) : 0;

                    var Kernel = ManagedMatrix.Pow(g, tempK);

                    var tempY = new ManagedArray(Cols(tY), rows);
                    var tempA = new ManagedArray(Cols(tA), rows);

                    ManagedMatrix.Expand(tY, 1, rows, tempY);
                    ManagedMatrix.Expand(tA, 1, rows, tempA);

                    ManagedMatrix.Product(Kernel, tempY);
                    ManagedMatrix.Product(Kernel, tempA);

                    var p = ManagedMatrix.RowSums(Kernel);

                    ManagedOps.Copy2D(predictions, p, 0, 0);
                    ManagedMatrix.Add(predictions, B);

                    ManagedOps.Free(pX1, pX2, rX2, X1, X2, tempK, temp1, temp2, tX, tY, tA, tempY, tempA, Kernel, p);
                }
                else
                {
                    var Xi = new ManagedArray(Cols(x), 1);
                    var Xj = new ManagedArray(Cols(ModelX), 1);

                    for (var i = 0; i < m; i++)
                    {
                        double prediction = 0;

                        ManagedOps.Copy2D(Xi, x, 0, i);

                        for (var j = 0; j < Rows(ModelX); j++)
                        {
                            ManagedOps.Copy2D(Xj, ModelX, 0, j);

                            prediction += Alpha[j] * ModelY[j] * KernelFunction.Run(Type, Xi, Xj, KernelParam);
                        }

                        predictions[i] = prediction + B;
                    }

                    ManagedOps.Free(Xi, Xj);
                }

                ManagedOps.Free(x);
            }

            return(predictions);
        }