【问题标题】:I want to make multi Class SVM using Dynamic time warping kernel我想使用动态时间规整内核制作多类 SVM
【发布时间】:2017-01-30 19:56:02
【问题描述】:

我正在使用accord.net。以下示例工作正常,但我想制作多类分类器。 我尝试使用 MulticlassSupportVectorMachine() 函数,但它为动态时间扭曲类内核训练了 0.6 错误的数据,它没有为某些输入提供正确的输出。

    // Suppose you have sequences of multivariate observations, and that
// those sequences could be of arbitrary length. On the other hand, 
// each observation have a fixed, delimited number of dimensions.

// In this example, we have sequences of 3-dimensional observations. 
// Each sequence can have an arbitrary length, but each observation
// will always have length 3:   
 double[][][] sequences ={
  new double[][] // first sequence
{
    new double[] { 1, 1, 1 }, // first observation of the first sequence
    new double[] { 1, 2, 1 }, // second observation of the first sequence
    new double[] { 1, 4, 2 }, // third observation of the first sequence
    new double[] { 2, 2, 2 }, // fourth observation of the first sequence
},

new double[][] // second sequence (note that this sequence has a different length)
{
    new double[] { 1, 1, 1 }, // first observation of the second sequence
    new double[] { 1, 5, 6 }, // second observation of the second sequence
    new double[] { 2, 7, 1 }, // third observation of the second sequence
},

new double[][] // third sequence 
{
    new double[] { 8, 2, 1 }, // first observation of the third sequence
},

new double[][] // fourth sequence 
{
    new double[] { 8, 2, 5 }, // first observation of the fourth sequence
    new double[] { 1, 5, 4 }, // second observation of the fourth sequence
}
};
// Now, we will also have different class labels associated which each 
// sequence. We will assign -1 to sequences whose observations start 
// with { 1, 1, 1 } and +1 to those that do not:

int[] outputs =
{
  -1,-1,  // First two sequences are of class -1 (those start with {1,1,1})
    1, 1,  // Last two sequences are of class +1  (don't start with {1,1,1})
};

// At this point, we will have to "flat" out the input sequences from             double[][][]
    // to a double[][] so they can be properly understood by the SVMs. The      problem is 
// that, normally, SVMs usually expect the data to be comprised of fixed-length 
// input vectors and associated class labels. But in this case, we will be feeding
// them arbitrary-length sequences of input vectors and class labels associated with
// each sequence, instead of each vector.

double[][] inputs = new double[sequences.Length][];
for (int i = 0; i < sequences.Length; i++)
inputs[i] = Matrix.Concatenate(sequences[i]);


// Now we have to setup the Dynamic Time Warping kernel. We will have to
// inform the length of the fixed-length observations contained in each
// arbitrary-length sequence:
// 
DynamicTimeWarping kernel = new DynamicTimeWarping(length: 3);

// Now we can create the machine. When using variable-length
/    / kernels, we will need to pass zero as the input length:
var svm = new KernelSupportVectorMachine(kernel, inputs: 0);


// Create the Sequential Minimal Optimization learning algorithm
var smo = new SequentialMinimalOptimization(svm, inputs, outputs)
{
Complexity = 1.5
};

// And start learning it!
double error = smo.Run(); // error will be 0.0


// At this point, we should have obtained an useful machine. Let's
// see if it can understand a few examples it hasn't seem before:

double[][] a = 
{ 
new double[] { 1, 1, 1 },
new double[] { 7, 2, 5 },
new double[] { 2, 5, 1 },
};

double[][] b =
{
new double[] { 7, 5, 2 },
new double[] { 4, 2, 5 },
new double[] { 1, 1, 1 },
};

// Following the aforementioned logic, sequence (a) should be
// classified as -1, and sequence (b) should be classified as +1.

int resultA = System.Math.Sign(svm.Compute(Matrix.Concatenate(a))); // -1
int resultB = System.Math.Sign(svm.Compute(Matrix.Concatenate(b))); // +1

我需要帮助来使用 MulticlassSupportVectorMachine() 来实现多类 SVM 分类器,该分类器针对两​​种以上类型的输入训练机器,并为每种类型的输入提供输出标签。 P.S:如果 MulticlassSupportVectorMachine() 函数不支持动态时间扭曲内核。请告诉我如何在上面的动态时间扭曲内核中使用一对一的多类 svm 技术,并使用一对一的技术制作多分类器。 您的帮助将不胜感激。 提前致谢。

【问题讨论】:

    标签: c# machine-learning svm accord.net


    【解决方案1】:

    此代码适用于我

    var smo = new MulticlassSupportVectorLearning<DynamicTimeWarping, double[][]>()
                {
                    // Set the parameters of the kernel
                    Kernel = new DynamicTimeWarping(alpha: 1, degree: 1)
                };
                // And use it to learn a machine!
                var svm = smo.Learn(words, labels);
                // Create the multi-class learning algorithm for the machine
                var calibration = new MulticlassSupportVectorLearning<DynamicTimeWarping, double[][]>()
                {
                    Model = svm, // We will start with an existing machine
    
                    // Configure the learning algorithm to use SMO to train the
                    //  underlying SVMs in each of the binary class subproblems.
                    Learner = (param) => new ProbabilisticOutputCalibration<DynamicTimeWarping, double[][]>()
                    {
                        Model = param.Model // Start with an existing machine
                    }
                };
                // Configure parallel execution options
                calibration.ParallelOptions.MaxDegreeOfParallelism = 1;
                // Learn a machine
                calibration.Learn(words, labels);
                // Obtain class predictions for each sample
                int[] predicted = svm.Decide(words);
                int[] expected = new int[words.Length];
    
    
                double correct = 0;
                for (int i = 0; i < words.Length; i++)
                {
                    expected[i] = labels[i];
                    predicted[i] = svm.Decide(words[i]);
                    if (svm.Decide(words[i]) == labels[i])
                    {
                        correct++;
                    }
    
                }
                string Accurecy = "SMO Accurecy = " + (correct / predicted.Length).ToString() + Environment.NewLine; // ori
    

    【讨论】:

    • 欢迎来到 StackOverflow!感谢您提供答案,但如果您在代码中添加一些解释会更有帮助。
    猜你喜欢
    • 2015-07-05
    • 2022-12-29
    • 2019-10-02
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 2012-07-16
    • 1970-01-01
    相关资源
    最近更新 更多