Accord和多标签支持向量机

时间:2016-03-19 23:39:28

标签: c# accord.net

我正在研究多类支持向量机的文档中的示例 - http://accord-framework.net/docs/html/T_Accord_MachineLearning_VectorMachines_MultilabelSupportVectorMachine.htm

尽管如此,我没有得到0错误率,当我尝试计算值时,他们没有给出应该的输出值。这个例子有问题吗?

    static void Main(string[] args)
    {
        // Sample input data
        double[][] inputs =
        {
            new double[] { 0 },
            new double[] { 1 },
            new double[] { 2 },
            new double[] { 3 },
        };

        // Outputs for each of the inputs
        int[][] outputs =
            {
                new[] {1,-1,-1,-1}, 
                new[] {-1,1,-1,-1}, 
                new[] {-1,-1,1,-1}, 
                new[] {-1,-1,-1,1}, 
            };

        // Create a new Linear kernel
        IKernel kernel = new Linear();

        // Create a new Multi-class Support Vector Machine with one input,
        //  using the linear kernel and for four disjoint classes.
        var machine = new MultilabelSupportVectorMachine(1, kernel, 4);

        // Create the Multi-label learning algorithm for the machine
        var teacher = new MultilabelSupportVectorLearning(machine, inputs, outputs);

        // Configure the learning algorithm to use SMO to train the
        //  underlying SVMs in each of the binary class subproblems.
        teacher.Algorithm = (svm, classInputs, classOutputs, i, j) =>
            new SequentialMinimalOptimization(svm, classInputs, classOutputs);

        // Run the learning algorithm
        double error = teacher.Run();

        error = teacher.Run(); // 0.1875 error rate
        var answer = machine.Compute(new double[] {2});  // gives -1,-1,-1,-1, instead of -1,-1,1,-1

如果错误率为零,为什么只有0的输入才能给出正确的输出呢?

1 个答案:

答案 0 :(得分:0)

要回答这个问题,这个特定的例子很可能出现了问题。大多数示例都已更新,以反映去年推出的新的.Learn()API。

现在您可能会看到多标签支持向量机的文档页面也因新API而更改了地址,现在位于

现在它包括这个例子,其中包括:

// Let's say we have the following data to be classified
// into three possible classes. Those are the samples:
// 
double[][] inputs =
{
    //               input         output
    new double[] { 0, 1, 1, 0 }, //  0 
    new double[] { 0, 1, 0, 0 }, //  0
    new double[] { 0, 0, 1, 0 }, //  0
    new double[] { 0, 1, 1, 0 }, //  0
    new double[] { 0, 1, 0, 0 }, //  0
    new double[] { 1, 0, 0, 0 }, //  1
    new double[] { 1, 0, 0, 0 }, //  1
    new double[] { 1, 0, 0, 1 }, //  1
    new double[] { 0, 0, 0, 1 }, //  1
    new double[] { 0, 0, 0, 1 }, //  1
    new double[] { 1, 1, 1, 1 }, //  2
    new double[] { 1, 0, 1, 1 }, //  2
    new double[] { 1, 1, 0, 1 }, //  2
    new double[] { 0, 1, 1, 1 }, //  2
    new double[] { 1, 1, 1, 1 }, //  2
};

int[] outputs = // those are the class labels
{
    0, 0, 0, 0, 0,
    1, 1, 1, 1, 1,
    2, 2, 2, 2, 2,
};

// Create the multi-class learning algorithm for the machine
var teacher = new MulticlassSupportVectorLearning<Gaussian>()
{
    // Configure the learning algorithm to use SMO to train the
    //  underlying SVMs in each of the binary class subproblems.
    Learner = (param) => new SequentialMinimalOptimization<Gaussian>()
    {
        // Estimate a suitable guess for the Gaussian kernel's parameters.
        // This estimate can serve as a starting point for a grid search.
        UseKernelEstimation = true
    }
};

// Configure parallel execution options
teacher.ParallelOptions.MaxDegreeOfParallelism = 1;

// Learn a machine
var machine = teacher.Learn(inputs, outputs);

// Obtain class predictions for each sample
int[] predicted = machine.Decide(inputs);

// Get class scores for each sample
double[] scores = machine.Score(inputs);

// Compute classification error
double error = new ZeroOneLoss(outputs).Loss(predicted);