- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 Android 应用程序 (NDK) 上运行 LibSVM。我已经在适用于所有特征向量大小的 Mac 应用程序上实现了类似的代码。当我给出一个包含 408 个特征的 vector 时,我在进行多类分类时没有任何问题。不过,任何 409 或更高的值(我最终将输入 16800)似乎都在这里失败:
0-16 23:28:41.084 30997-31028/? A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0xaf000000 in tid 31028 (GLThread 17147)
10-16 23:28:41.190 27393-27393/? I/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
10-16 23:28:41.191 27393-27393/? I/DEBUG: Build fingerprint: 'google/hammerhead/hammerhead:5.1.1/LMY48M/2167285:user/release-keys'
10-16 23:28:41.191 27393-27393/? I/DEBUG: Revision: '11'
10-16 23:28:41.191 27393-27393/? I/DEBUG: ABI: 'arm'
10-16 23:28:41.191 27393-27393/? I/DEBUG: pid: 30997, tid: 31028, name: GLThread 17147 >>> cc.openframeworks.androidEmptyExample <<<
10-16 23:28:41.191 27393-27393/? I/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xaf000000
10-16 23:28:41.202 27393-27393/? I/DEBUG: r0 aef3e000 r1 aef5ed10 r2 00000001 r3 af000000
10-16 23:28:41.202 27393-27393/? I/DEBUG: r4 aec29eb8 r5 00000001 r6 b4b2c608 r7 12d090c0
10-16 23:28:41.202 27393-27393/? I/DEBUG: r8 12d15660 r9 b4a39400 sl 00000000 fp af37d824
10-16 23:28:41.202 27393-27393/? I/DEBUG: ip b6e417dc sp af37d810 lr a301ff78 pc a301ff04 cpsr 000f0010
10-16 23:28:41.202 27393-27393/? I/DEBUG: #00 pc 00167f04 /data/app/cc.openframeworks.androidEmptyExample-1/lib/arm/libOFAndroidApp.so (Kernel::dot(svm_node const*, svm_node const*)+192)
这是我正在学习的相关代码:
ofxSvm mSvm;
void ofApp::update()
{ //Runs in loop
for(int i =0; i<8400; ++i)
{
HandDataVector.push_back((double)tempValue[i]);//tempValue is incoming data from a serial port (8400 doubles per packet)
}
//If I exclude this I get segfaults:
HandDataVector.resize(150);
if(learningToggleBoxTicked)
{
mSvm.addData(HandDataVector,label)
mSvm.train();
} else {
ofLogNotice("Classified As")<< mSvm.classify();
}
}
int ofApp::classify()
{
return mSvm.predict(HandDataVector);
}
这是我正在使用的 ofxSvm 库
int ofxSvm::addData(int label, vector<double>& vec)
{
checkDimension(vec.size());
mData.insert(make_pair(label, vec));
mDimension = vec.size();
stringstream ss;
for (const auto v : vec) ss << v << " ";
ss << "EOS";
ofLogNotice(LOG_MODULE, "add data, label: " + ofToString(label) + " size: "+ofToString(vec.size())+" vec: " + ss.str());
return mData.size();
}
void ofxSvm::train()
{
svm_problem prob;
prob.l = mData.size();
prob.y = new double[prob.l];
{
data_type::iterator it = mData.begin();
int i = 0;
while (it != mData.end())
{
prob.y[i] = it->first;
++it; ++i;
}
}
if(mParam.gamma == 0)
{
mParam.gamma = 1.0 / mDimension;
}
int nodeLength = mDimension + 1;
svm_node* node = new svm_node[prob.l * nodeLength];
prob.x = new svm_node*[prob.l];
{
data_type::iterator it = mData.begin();
int i = 0;
while (it != mData.end())
{
prob.x[i] = node + i * nodeLength;
for (int j = 0; j < mDimension; ++j)
{
prob.x[i][j].index = j + 1;
prob.x[i][j].value = it->second[j];
}
prob.x[i][mDimension].index = -1; // delimiter
++it; ++i;
}
}
ofLogNotice(LOG_MODULE, "Start train...");
mModel = svm_train(&prob, &mParam);
ofLogNotice(LOG_MODULE, "Finished train!");
int x = mModel->nr_class;
ofLogNotice("TRAINED MODEL LABELS: " + ofToString(x));
delete[] node;
delete[] prob.x;
delete[] prob.y;
}
int ofxSvm::predict(vector<double>& testVec)
{
if (mModel == NULL)
{
ofLogNotice(LOG_MODULE, "null model, before do train or load model file");
return -5;
}
if (testVec.size() != mDimension)
{
ofLogNotice(LOG_MODULE, "different dimension");
return -6;
}
svm_node* node = new svm_node[mDimension + 1];
for (int i = 0; i < mDimension; ++i)
{
node[i].index = i + 1;
node[i].value = testVec[i];
ofLogNotice("node") << node[i].value <<"-" << i;
}
node[mDimension].index = -1;
int res = static_cast<int>(svm_predict(mModel, node));
stringstream ss;
for (const auto v : testVec) ss << v << " ";
ss << "EOS";
ofLogNotice(LOG_MODULE, "add data, label: size: "+ofToString(testVec.size())+" vec: " + ss.str());
ofLogNotice("ANSWER")<< res;
delete[] node;
return res;
}
这是发生错误的 LibSVM 库中的函数:
double Kernel::dot(const svm_node *px, const svm_node *py)
{
double sum = 0;
while(px->index != -1 && py->index != -1)
{
if(px->index == py->index)
{
sum += px->value * py->value;
++px;
++py;
}
else
{
if(px->index > py->index)
++py;
else
++px;
}
}
return sum;
}
编辑:这里是调用点函数的地方(svm_predict_values 中的 k_function)
double svm_predict_values(const svm_model *model, const svm_node *x, double* dec_values)
{
int i;
if(model->param.svm_type == ONE_CLASS ||
model->param.svm_type == EPSILON_SVR ||
model->param.svm_type == NU_SVR)
{
double *sv_coef = model->sv_coef[0];
double sum = 0;
for(i=0;i<model->l;i++)
sum += sv_coef[i] * Kernel::k_function(x,model->SV[i],model->param);
sum -= model->rho[0];
*dec_values = sum;
if(model->param.svm_type == ONE_CLASS)
return (sum>0)?1:-1;
else
return sum;
}
else
{
int nr_class = model->nr_class;
int l = model->l;
double *kvalue = Malloc(double,l);
for(i=0;i<l;i++)
kvalue[i] = Kernel::k_function(x,model->SV[i],model->param);
int *start = Malloc(int,nr_class);
start[0] = 0;
for(i=1;i<nr_class;i++)
start[i] = start[i-1]+model->nSV[i-1];
int *vote = Malloc(int,nr_class);
for(i=0;i<nr_class;i++)
vote[i] = 0;
int p=0;
for(i=0;i<nr_class;i++)
for(int j=i+1;j<nr_class;j++)
{
double sum = 0;
int si = start[i];
int sj = start[j];
int ci = model->nSV[i];
int cj = model->nSV[j];
int k;
double *coef1 = model->sv_coef[j-1];
double *coef2 = model->sv_coef[i];
for(k=0;k<ci;k++)
sum += coef1[si+k] * kvalue[si+k];
for(k=0;k<cj;k++)
sum += coef2[sj+k] * kvalue[sj+k];
sum -= model->rho[p];
dec_values[p] = sum;
if(dec_values[p] > 0)
++vote[i];
else
++vote[j];
p++;
}
int vote_max_idx = 0;
for(i=1;i<nr_class;i++)
if(vote[i] > vote[vote_max_idx])
vote_max_idx = i;
free(kvalue);
free(start);
free(vote);
return model->label[vote_max_idx];
}
}
double Kernel::k_function(const svm_node *x, const svm_node *y,
const svm_parameter& param)
{
switch(param.kernel_type)
{
case LINEAR:
return dot(x,y);
case POLY:
return powi(param.gamma*dot(x,y)+param.coef0,param.degree);
case RBF:
{
double sum = 0;
while(x->index != -1 && y->index !=-1)
{
if(x->index == y->index)
{
double d = x->value - y->value;
sum += d*d;
++x;
++y;
}
else
{
if(x->index > y->index)
{
sum += y->value * y->value;
++y;
}
else
{
sum += x->value * x->value;
++x;
}
}
}
while(x->index != -1)
{
sum += x->value * x->value;
++x;
}
while(y->index != -1)
{
sum += y->value * y->value;
++y;
}
return exp(-param.gamma*sum);
}
case SIGMOID:
return tanh(param.gamma*dot(x,y)+param.coef0);
case PRECOMPUTED: //x: test (validation), y: SV
return x[(int)(y->value)].value;
default:
return 0; // Unreachable
}
}
double kernel_linear(int i, int j) const
{
return dot(x[i],x[j]);
}
最佳答案
简介
我很难在 C++ 中找到有关 libSVM 的任何文档。所以我的回答主要基于您向我们展示的代码,没有真正的引用文档。
如果您可以发布指向您的文档的链接,那将非常有帮助:)
潜在问题
您展示了初始化 svm_node*
的代码在你的训练代码中:
prob.x[i][mDimension].index = -1;
您正在初始化一个 svm_problem
实例。显然在你的 dot()
您希望看到带有负索引的此类节点的函数,以标记 svm_node
的结尾列表。
但是当这样调用时你的代码失败了:
Kernel::k_function(x,model->SV[i],model->param);
在这里,model
是一个 svm_model
,不是 svm_problem
,所以我猜这是在您训练模型后 libSVM 返回的。你确定svm_model
遵循使用 node->index = -1
的约定标记节点列表的结尾?
所以也许标记节点不存在而你超出了范围。
为什么409条突然断了
您收到的信号是一个 SIGSEGV,它表示您试图访问未在您的进程中映射的页面中的一个字节。
越界访问项目时,通常不会收到 SIGSEGV,因为先前分配的项目位于内存页的中间,并且页中它后面有足够的字节。可能恰好第 409 项就在当前分配的页面之后,触发了信号。
关于c++ - 具有大特征向量 LibSVM 的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33182523/
我已经使用 vue-cli 两个星期了,直到今天一切正常。我在本地建立这个项目。 https://drive.google.com/open?id=0BwGw1zyyKjW7S3RYWXRaX24tQ
您好,我正在尝试使用 python 库 pytesseract 从图像中提取文本。请找到代码: from PIL import Image from pytesseract import image_
我的错误 /usr/bin/ld: errno: TLS definition in /lib/libc.so.6 section .tbss mismatches non-TLS reference
我已经训练了一个模型,我正在尝试使用 predict函数但它返回以下错误。 Error in contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]])
根据Microsoft DataConnectors的信息我想通过 this ODBC driver 创建一个从 PowerBi 到 PostgreSQL 的连接器使用直接查询。我重用了 Micros
我已经为 SoundManagement 创建了一个包,其中有一个扩展 MediaPlayer 的类。我希望全局控制这个变量。这是我的代码: package soundmanagement; impo
我在Heroku上部署了一个应用程序。我正在使用免费服务。 我经常收到以下错误消息。 PG::Error: ERROR: out of memory 如果刷新浏览器,就可以了。但是随后,它又随机发生
我正在运行 LAMP 服务器,这个 .htaccess 给我一个 500 错误。其作用是过滤关键字并重定向到相应的域名。 Options +FollowSymLinks RewriteEngine
我有两个驱动器 A 和 B。使用 python 脚本,我在“A”驱动器中创建一些文件,并运行 powerscript,该脚本以 1 秒的间隔将驱动器 A 中的所有文件复制到驱动器 B。 我在 powe
下面的函数一直返回这个错误信息。我认为可能是 double_precision 字段类型导致了这种情况,我尝试使用 CAST,但要么不是这样,要么我没有做对...帮助? 这是错误: ERROR: i
这个问题已经有答案了: Syntax error due to using a reserved word as a table or column name in MySQL (1 个回答) 已关闭
我的数据库有这个小问题。 我创建了一个表“articoli”,其中包含商品的品牌、型号和价格。 每篇文章都由一个 id (ID_ARTICOLO)` 定义,它是一个自动递增字段。 好吧,现在当我尝试插
我是新来的。我目前正在 DeVry 在线学习中级 C++ 编程。我们正在使用 C++ Primer Plus 这本书,到目前为止我一直做得很好。我的老师最近向我们扔了一个曲线球。我目前的任务是这样的:
这个问题在这里已经有了答案: What is an undefined reference/unresolved external symbol error and how do I fix it?
我的网站中有一段代码有问题;此错误仅发生在 Internet Explorer 7 中。 我没有在这里发布我所有的 HTML/CSS 标记,而是发布了网站的一个版本 here . 如您所见,我在列中有
如果尝试在 USB 设备上构建 node.js 应用程序时在我的树莓派上使用 npm 时遇到一些问题。 package.json 看起来像这样: { "name" : "node-todo",
在 Python 中,您有 None单例,在某些情况下表现得很奇怪: >>> a = None >>> type(a) >>> isinstance(a,None) Traceback (most
这是我的 build.gradle (Module:app) 文件: apply plugin: 'com.android.application' android { compileSdkV
我是 android 的新手,我的项目刚才编译和运行正常,但在我尝试实现抽屉导航后,它给了我这个错误 FAILURE: Build failed with an exception. What wen
谁能解释一下?我想我正在做一些非常愚蠢的事情,并且急切地等待着启蒙。 我得到这个输出: phpversion() == 7.2.25-1+0~20191128.32+debian8~1.gbp108
我是一名优秀的程序员,十分优秀!