gpt4 book ai didi

c++ - LIBSVM 训练数据格式(svm_problem 的 svm_node 中的 x 值)

转载 作者:行者123 更新时间:2023-11-28 03:00:28 26 4
gpt4 key购买 nike

我正在使用 LIBSVM 以编程方式执行简单的 XOR 分类,试图了解函数的工作原理。我已尽可能按照自述文件中的说明设置问题。使用 svm_predict 时我仍然得到错误的输出(总是 1 或 -1)。

在一个相关问题中,有人提出当使用很少的训练示例时可能会出现问题。我尝试将示例数量增加到 20,但这没有帮助。

我怀疑问题出在 prob.x 和/或 prob.y 的定义中,但无法理解在哪里。您能否帮助阐明如何使用 svm_node 定义 prob.x 和 prob.y?

我已经彻底搜索但找不到答案......例如Here, here, here, here, and here.

提前致谢!

这是我的代码:

//Parameters
svm_parameter param;
param.svm_type = C_SVC;
param.kernel_type = RBF;
param.degree = 3;
param.gamma = 0;
param.coef0 = 0;
param.nu = 0.5;
param.cache_size = 100;
param.C = 0.4;
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
prob.l = 4; //number of training examples


//x values

svm_node** x = new svm_node *[prob.l]; //Array of pointers to pointers to arrays

svm_node* x_space1 = new svm_node[3]; //Fist training example
svm_node* x_space2 = new svm_node[3]; //Second training example
svm_node* x_space3 = new svm_node[3]; //Third training example
svm_node* x_space4 = new svm_node[3]; //Fourth training example

x_space1[0].index = 1; //Fist training example
x_space1[0].value = 1;
x_space1[1].index = 2;
x_space1[1].value = 1;
x_space1[2].index = -1;

x_space2[0].index = 1; //Second training example
x_space2[0].value = 1;
x_space2[1].index = 2;
x_space2[1].value = 0;
x_space2[2].index = -1;

x_space3[0].index = 1; //Third training example
x_space3[0].value = 0;
x_space3[1].index = 2;
x_space3[1].value = 1;
x_space3[2].index = -1;

x_space4[0].index = 1; //Fourth training example
x_space4[0].value = 0;
x_space4[1].index = 2;
x_space4[1].value = 0;
x_space4[2].index = -1;

x[0] = x_space1; //Set each training example to x
x[1] = x_space2;
x[2] = x_space3;
x[3] = x_space4;

prob.x = x; //Assign x to the struct field prob.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,&param);


//Test model
svm_node* testnode = new svm_node[3];
testnode[0].index = 1;
testnode[0].value = 1;
testnode[1].index = 2;
testnode[1].value = 0;
testnode[2].index = -1;

double retval = svm_predict(model,testnode);
qDebug()<<retval; //Should return +1 but returns -1

最佳答案

看来你的参数有问题。例如,如果您使用的是 RBF 内核,则 param.gamma 不应为零。

关于c++ - LIBSVM 训练数据格式(svm_problem 的 svm_node 中的 x 值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20982019/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com