我需要使用Liblinear实现多类分类器。Accord.net机器学习框架提供了Liblinear的所有特性,但不包括Crammer和Singer用于多类分类的公式。这是具体的过程。
回答:
通常学习多类机器的方法是使用MulticlassSupportVectorLearning类。这个类可以训练一对一的机器,然后使用投票或淘汰策略进行查询。
因此,这里是一个如何对多个类进行线性训练的示例:
// 假设我们有以下数据需要分类// 分为三个可能的类。这些是样本:// double[][] inputs ={ // 输入 输出 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 = // 这些是类标签{ 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,};// 创建一个一对一的多类SVM学习算法 var teacher = new MulticlassSupportVectorLearning<Linear>(){ // 使用LIBLINEAR的L2损失SVC双重方法进行每个SVM的训练 Learner = (p) => new LinearDualCoordinateDescent() { Loss = Loss.L2 }};// 学习一个机器var machine = teacher.Learn(inputs, outputs);// 获取每个样本的类预测int[] predicted = machine.Decide(inputs);// 计算分类准确率double acc = new GeneralConfusionMatrix(expected: outputs, predicted: predicted).Accuracy;
你也可以尝试使用一对多的策略来解决多类决策问题。在这种情况下,你可以使用MultilabelSupportVectorLearning教学算法来代替上面展示的多类算法。