@@ -46,11 +46,12 @@ void svmModel::fit(const svmProblem &problem, const SVMParam ¶m) {
4646 probParam.C = 1.0 ;
4747 svmModel model;
4848 model.fit (subProblem, probParam);
49- vector<float_point * > decValues;
49+ vector<vector< float_point> > decValues;
5050 // todo predict with cross validation
5151 model.predictValues (subProblem.v_vSamples , decValues);
5252 // binary model has only one sub-model
53- sigmoidTrain (decValues.front (), subProblem.getNumOfSamples (), subProblem.v_nLabels , probA[k], probB[k]);
53+ sigmoidTrain (decValues.front ().data (), subProblem.getNumOfSamples (), subProblem.v_nLabels , probA[k],
54+ probB[k]);
5455 probability = true ;
5556 }
5657 svm_model binaryModel = trainBinarySVM (subProblem, param);
@@ -182,19 +183,20 @@ void svmModel::addBinaryModel(const svmProblem &problem, const svm_model &bModel
182183}
183184
184185void
185- svmModel::predictValues (const vector<vector<float_point> > &v_vSamples, vector<float_point *> &decisionValues) const {
186+ svmModel::predictValues (const vector<vector<float_point> > &v_vSamples,
187+ vector<vector<float_point> > &decisionValues) const {
186188 decisionValues.clear ();
187189 for (int k = 0 ; k < cnr2; ++k) {
188- float_point * kernelValues = new float_point[ v_vSamples.size () * supportVectors[k].size ()] ;
190+ vector< float_point> kernelValues ( v_vSamples.size () * supportVectors[k].size ()) ;
189191 computeKernelValuesOnFly (v_vSamples, supportVectors[k], kernelValues);
190- decisionValues. push_back (
191- predictLabels (kernelValues, ( int ) v_vSamples. size () , k)); // TODO not return local pointer in function
192- delete[] kernelValues ;
192+ vector<float_point> decValues41BinaryModel (v_vSamples. size () * supportVectors[k]. size ());
193+ predictLabels (kernelValues, decValues41BinaryModel , k);
194+ decisionValues. push_back (decValues41BinaryModel) ;
193195 }
194196}
195197
196198vector<int > svmModel::predict (const vector<vector<float_point> > &v_vSamples, bool probability) const {
197- vector<float_point * > decisionValues;
199+ vector<vector< float_point> > decisionValues;
198200 predictValues (v_vSamples, decisionValues);
199201 vector<int > labels;
200202 if (!probability) {
@@ -233,7 +235,7 @@ vector<int> svmModel::predict(const vector<vector<float_point> > &v_vSamples, bo
233235 return labels;
234236}
235237
236- float_point svmModel::sigmoid_predict (float_point decValue, float_point A, float_point B) const {
238+ float_point svmModel::sigmoidPredict (float_point decValue, float_point A, float_point B) const {
237239 double fApB = decValue * A + B;
238240 // 1-p used later; avoid catastrophic cancellation
239241 if (fApB >= 0 )
@@ -242,7 +244,7 @@ float_point svmModel::sigmoid_predict(float_point decValue, float_point A, float
242244 return 1.0 / (1 + exp (fApB ));
243245}
244246
245- void svmModel::multiclass_probability (const vector<vector<float_point> > &r, vector<float_point> &p) const {
247+ void svmModel::multiClassProbability (const vector<vector<float_point> > &r, vector<float_point> &p) const {
246248 int t, j;
247249 int iter = 0 , max_iter = max (100 , nrClass);
248250 double **Q = (double **) malloc (sizeof (double *) * nrClass);
@@ -301,7 +303,7 @@ void svmModel::multiclass_probability(const vector<vector<float_point> > &r, vec
301303
302304vector<vector<float_point> > svmModel::predictProbability (const vector<vector<float_point> > &v_vSamples) const {
303305 vector<vector<float_point> > result;
304- vector<float_point * > decValues;
306+ vector<vector< float_point> > decValues;
305307 predictValues (v_vSamples, decValues);
306308 for (int l = 0 ; l < v_vSamples.size (); ++l) {
307309 vector<vector<float_point> > r (nrClass, vector<float_point>(nrClass));
@@ -310,21 +312,20 @@ vector<vector<float_point> > svmModel::predictProbability(const vector<vector<fl
310312 for (int i = 0 ; i < nrClass; i++)
311313 for (int j = i + 1 ; j < nrClass; j++) {
312314 r[i][j] = min (
313- max (sigmoid_predict (decValues[k][l], probA[k], probB[k]), min_prob), 1 - min_prob);
315+ max (sigmoidPredict (decValues[k][l], probA[k], probB[k]), min_prob), 1 - min_prob);
314316 r[j][i] = 1 - r[i][j];
315317 k++;
316318 }
317319 vector<float_point> p (nrClass);
318- multiclass_probability (r, p);
320+ multiClassProbability (r, p);
319321 result.push_back (p);
320322 }
321323 return result;
322324}
323325
324- void
325- svmModel::computeKernelValuesOnFly (const vector<vector<float_point> > &samples,
326- const vector<vector<float_point> > &supportVectors,
327- float_point *kernelValues) const {
326+ void svmModel::computeKernelValuesOnFly (const vector<vector<float_point> > &samples,
327+ const vector<vector<float_point> > &supportVectors,
328+ vector<float_point> &kernelValues) const {
328329 for (int i = 0 ; i < samples.size (); ++i) {
329330 for (int j = 0 ; j < supportVectors.size (); ++j) {
330331 // rbf kernel
@@ -338,7 +339,8 @@ svmModel::computeKernelValuesOnFly(const vector<vector<float_point> > &samples,
338339 }
339340}
340341
341- float_point *svmModel::predictLabels (const float_point *kernelValues, int nNumofTestSamples, int k) const {
342+ void svmModel::predictLabels (const vector<float_point> &kernelValues, vector<float_point> &classificationResult,
343+ int k) const {
342344 // get infomation from SVM model
343345 int nNumofSVs = (int ) supportVectors[k].size ();
344346// int nNumofSVs = GetNumSV(pModel);
@@ -360,21 +362,21 @@ float_point *svmModel::predictLabels(const float_point *kernelValues, int nNumof
360362
361363 StorageManager *manager = StorageManager::getManager ();
362364 long long int nMaxNumofFloatPoint = manager->GetFreeGPUMem ();
363- long long int nNumofPart = Ceil (nNumofSVs * nNumofTestSamples , nMaxNumofFloatPoint);
365+ long long int nNumofPart = Ceil (nNumofSVs * kernelValues. size () , nMaxNumofFloatPoint);
364366
365367// cout << "cache size is: " << nMaxNumofFloatPoint << " v.s.. " << nNumofSVs * nNumofTestSamples << endl;
366368// cout << "perform classification in " << nNumofPart << " time(s)" << endl;
367369
368370 // allocate memory for storing classification result
369- float_point *pfClassificaitonResult = new float_point[nNumofTestSamples ];
371+ // float_point *pfClassificaitonResult = new float_point[kernelValues.size() ];
370372 // initialise the size of each part
371373 int *pSizeofPart = new int [nNumofPart];
372- int nAverageSize = (int ) (nNumofTestSamples / nNumofPart);
374+ int nAverageSize = (int ) (kernelValues. size () / nNumofPart);
373375 for (int i = 0 ; i < nNumofPart; i++) {
374376 if (i != nNumofPart - 1 ) {
375377 pSizeofPart[i] = nAverageSize;
376378 } else {
377- pSizeofPart[i] = nNumofTestSamples - nAverageSize * i;
379+ pSizeofPart[i] = kernelValues. size () - nAverageSize * i;
378380 }
379381 }
380382
@@ -388,7 +390,7 @@ float_point *svmModel::predictLabels(const float_point *kernelValues, int nNumof
388390 checkCudaErrors (cudaMemset (pfDevSVsYiAlpha, 0 , sizeof (float_point) * nNumofSVs));
389391// checkCudaErrors(cudaMemset(pnDevSVsLabel, 0, sizeof(int) * nNumofSVs));
390392
391- checkCudaErrors (cudaMemcpy (pfDevSVYiAlphaHessian, kernelValues + i * nAverageSize * nNumofSVs,
393+ checkCudaErrors (cudaMemcpy (pfDevSVYiAlphaHessian, kernelValues. data () + i * nAverageSize * nNumofSVs,
392394 sizeof (float_point) * nNumofSVs * pSizeofPart[i], cudaMemcpyHostToDevice));
393395 checkCudaErrors (
394396 cudaMemcpy (pfDevSVsYiAlpha, pfSVsYiAlpha, sizeof (float_point) * nNumofSVs, cudaMemcpyHostToDevice));
@@ -403,12 +405,7 @@ float_point *svmModel::predictLabels(const float_point *kernelValues, int nNumof
403405
404406 // perform classification
405407 ComputeClassLabel (pSizeofPart[i], pfDevSVYiAlphaHessian,
406- nNumofSVs, fBias , pfClassificaitonResult + i * nAverageSize);
407-
408- if (pfClassificaitonResult == NULL ) {
409- cerr << " error in ComputeClassLabel" << endl;
410- exit (-1 );
411- }
408+ nNumofSVs, fBias , classificationResult.data () + i * nAverageSize);
412409
413410
414411 // free memory
@@ -417,8 +414,6 @@ float_point *svmModel::predictLabels(const float_point *kernelValues, int nNumof
417414 checkCudaErrors (cudaFree (pfDevSVsYiAlpha));
418415// checkCudaErrors(cudaFree(pnDevSVsLabel));
419416 }
420-
421- return pfClassificaitonResult;
422417}
423418
424419/*
0 commit comments