如何创建libsvm的训练数据(作为svm_node结构体) [英] How to create training data for libsvm (as an svm_node struct)
问题描述
我试图用编程方式使用libsvm来训练一个简单的xor问题的svm来理解库的工作原理。问题(我想)似乎是我构造svm_node不正确;也许我有麻烦理解指针事物的整个指针。任何人都可以帮助这个?我首先为xor问题构建一个矩阵,然后尝试将值从矩阵赋给svm_node(这里使用2个步骤,因为我的实际数据将是矩阵格式)。
I am trying to train an svm for a simple xor problem programmatically using libsvm to understand how the library works. The problem (i think) seems to be that i construct svm_node incorrectly; maybe i have trouble understanding the whole pointers to pointers thing. Could anybody help with this? I first construct a matrix for the xor problem then try to assign values from the matrix to svm_node (i am using 2 steps here because my real data will be in matrix format).
在测试模型时,我得到不正确的值(总是-1)。
When testing the model i get incorrect values (always -1).
在上一个问题中,我得到了参数C和gamma的帮助;这些应该是OK现在,因为我得到正确的分类为xor问题使用其他代码。再次感谢Pedrom!
In a previous question i got help with the parameters C and gamma; these should be OK now since i got correct classifications for the xor problem using other code. Thanks again Pedrom!
我在多个地方搜索过答案。 Readme和SvmToy示例中;没有运气。
I have searched in several places for answers, e.g. the Readme and in the SvmToy example; no luck however.
这是输出不正确分类的代码...
Here is the code that outputs incorrect classifications...
/ p>
Thanks in advance!
//Parameters---------------------------------------------------------------------
svm_parameter param;
param.svm_type = C_SVC;
param.kernel_type = RBF;
param.degree = 3;
param.gamma = 0.5;
param.coef0 = 0;
param.nu = 0.5;
param.cache_size = 100;
param.C = 1;
param.eps = 1e-3;
param.p = 0.1;
param.shrinking = 1;
param.probability = 0;
param.nr_weight = 0;
param.weight_label = NULL;
param.weight = NULL;
//Problem definition-------------------------------------------------------------
svm_problem prob;
//Length, 4 examples
prob.l = 4;
//x values matrix of xor values
QVector< QVector<double> >matrix;
QVector<double>row(2);
row[0] = 1;row[1] = 1;
matrix.push_back(row);
row[0] = 1;row[1] = 0;
matrix.push_back(row);
row[0] = 0;row[1] = 1;
matrix.push_back(row);
row[0] = 0;row[1] = 0;
matrix.push_back(row);
//This part i have trouble understanding
svm_node* x_space = new svm_node[3];
svm_node** x = new svm_node *[prob.l];
//Trying to assign from matrix to svm_node training examples
for (int row = 0;row < matrix.size(); row++){
for (int col = 0;col < 2;col++){
x_space[col].index = col;
x_space[col].value = matrix[row][col];
}
x_space[2].index = -1; //Each row of properties should be terminated with a -1 according to the readme
x[row] = x_space;
}
prob.x = x;
//yvalues
prob.y = new double[prob.l];
prob.y[0] = -1;
prob.y[1] = 1;
prob.y[2] = 1;
prob.y[3] = -1;
//Train model---------------------------------------------------------------------
svm_model *model = svm_train(&prob,¶m);
//Test model----------------------------------------------------------------------
svm_node* testnode = new svm_node[3];
testnode[0].index = 0;
testnode[0].value = 1;
testnode[1].index = 1;
testnode[1].value = 0;
testnode[2].index = -1;
//Should return 1 but returns -1
double retval = svm_predict(model,testnode);
qDebug()<<retval;
推荐答案
似乎你一直在试图得到这个例子工作几个星期。我遵循svm-train.c中的libsvm自带的风格。我使用你的值C和伽玛。它是工作。我试过XOR例子中的所有点,它给出正确的结果。
It seems you've been trying to get this example to work for weeks. I followed the style in svm-train.c that comes with libsvm. I used your values for C and gamma. It is working. I tried all points in the XOR example and it is giving correct results.
你所遇到的问题的总结是你不为你训练的4个数据点分配空间,所以你只需要覆盖数据。这是C中的指针的一个典型错误。它可以帮助你在C / C ++中刷指针。
The summary of the problem you're having is that you're not allocating space for the 4 data points you train with, so you just over-write the data. This is a typical mistake with pointers in C. It may help you brushed up on pointers in C/C++.
这是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "svm.h"
#define Malloc(type,n) (type *)malloc((n)*sizeof(type))
struct svm_parameter param; // set by parse_command_line
struct svm_problem prob; // set by read_problem
struct svm_model *model;
struct svm_node *x_space;
int main(int argc, char **argv)
{
char input_file_name[1024];
char model_file_name[1024];
const char *error_msg;
param.svm_type = C_SVC;
param.kernel_type = RBF;
param.degree = 3;
param.gamma = 0.5;
param.coef0 = 0;
param.nu = 0.5;
param.cache_size = 100;
param.C = 1;
param.eps = 1e-3;
param.p = 0.1;
param.shrinking = 1;
param.probability = 0;
param.nr_weight = 0;
param.weight_label = NULL;
param.weight = NULL;
//Problem definition-------------------------------------------------------------
prob.l = 4;
//x values matrix of xor values
double matrix[prob.l][2];
matrix[0][0] = 1;
matrix[0][1] = 1;
matrix[1][0] = 1;
matrix[1][1] = 0;
matrix[2][0] = 0;
matrix[2][1] = 1;
matrix[3][0] = 0;
matrix[3][1] = 0;
//This part i have trouble understanding
svm_node** x = Malloc(svm_node*,prob.l);
//Trying to assign from matrix to svm_node training examples
for (int row = 0;row <prob.l; row++){
svm_node* x_space = Malloc(svm_node,3);
for (int col = 0;col < 2;col++){
x_space[col].index = col;
x_space[col].value = matrix[row][col];
}
x_space[2].index = -1; //Each row of properties should be terminated with a -1 according to the readme
x[row] = x_space;
}
prob.x = x;
//yvalues
prob.y = Malloc(double,prob.l);
prob.y[0] = -1;
prob.y[1] = 1;
prob.y[2] = 1;
prob.y[3] = -1;
//Train model---------------------------------------------------------------------
svm_model *model = svm_train(&prob,¶m);
//Test model----------------------------------------------------------------------
svm_node* testnode = Malloc(svm_node,3);
testnode[0].index = 0;
testnode[0].value = 1;
testnode[1].index = 1;
testnode[1].value = 0;
testnode[2].index = -1;
//This works correctly:
double retval = svm_predict(model,testnode);
printf("retval: %f\n",retval);
svm_destroy_param(¶m);
free(prob.y);
free(prob.x);
free(x_space);
return 0;
}
这篇关于如何创建libsvm的训练数据(作为svm_node结构体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!