Changeset 11650:ed138a0a0d5c in orange
 Timestamp:
 08/08/13 17:21:29 (9 months ago)
 Branch:
 default
 Location:
 source/orange
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

source/orange/libsvm_interface.cpp
r11649 r11650 22 22 #include <sstream> 23 23 24 #include "libsvm_interface.ppp" 24 #include "libsvm_interface.hpp" 25 #include "symmatrix.hpp" 26 27 #include <algorithm> 28 #include <cmath> 29 25 30 26 31 // Defined in svm.cpp. If new svm or kernel types are added this should be updated. … … 137 142 } 138 143 139 140 #include <algorithm>141 144 142 145 svm_model *svm_load_model_alt(std::istream& stream) … … 351 354 } 352 355 356 353 357 svm_model *svm_load_model_alt(std::string& stream) 354 358 { … … 358 362 359 363 364 std::ostream & svm_node_vector_to_stream(std::ostream & stream, const svm_node * node) { 365 while (node>index != 1) { 366 stream << node>index << ":" << node>value << " "; 367 node++; 368 } 369 stream << node>index << ":" << node>value; 370 return stream; 371 } 372 373 std::ostream & operator << (std::ostream & stream, const svm_problem & problem) { 374 svm_node * node = NULL; 375 for (int i = 0; i < problem.l; i++) { 376 stream << problem.y[i] << " "; 377 svm_node_vector_to_stream(stream, problem.x[i]); 378 stream << endl; 379 } 380 return stream; 381 } 382 360 383 /* 361 384 * Return a formated string representing a svm data instance (svm_node *) 362 385 * (useful for debugging) 363 386 */ 364 st ring svm_node_to_string(svm_node * node) {387 std::string svm_node_to_string(const svm_node * node) { 365 388 std::ostringstream strstream; 366 389 strstream.precision(17); 367 while (node>index != 1) { 368 strstream << node>index << ":" << node>value << " "; 369 node++; 370 } 371 strstream << node>index << ":" << node>value << " "; 390 svm_node_vector_to_stream(strstream, node); 372 391 return strstream.rdbuf()>str(); 373 392 } 374 393 375 394 376 svm_node* example_to_svm(const TExample &ex, svm_node* node, float last=0.0, int type=0){ 377 if(type==0){ 378 int index=1; 379 for(TExample::iterator i=ex.begin(); i!=ex.end(); i++){ 380 if(i>isRegular() && i!=&ex.getClass()){ 381 if(i>varType==TValue::FLOATVAR) 382 node>value=float(*i); 383 else 384 node>value=int(*i); 385 node>index=index++; 386 if(node>value==numeric_limits<float>::signaling_NaN()  387 node>value==numeric_limits<int>::max()  388 node>value == 0) 389 node; 395 #ifdef _MSC_VER 396 #include <float.h> 397 #define isfinite _finite 398 #endif 399 400 /*! 401 * Check if the value is valid (not a special value in 'TValue'). 402 */ 403 404 inline bool is_valid(double value) { 405 return isfinite(value) && value != numeric_limits<int>::max(); 406 } 407 408 409 svm_node* example_to_svm(const TExample &ex, svm_node* node, double last=0.0) { 410 int index = 1; 411 double value = 0.0; 412 TExample::iterator values_end; 413 414 if (ex.domain>classVar) { 415 values_end = ex.end()  1; 416 } else { 417 values_end = ex.end(); 418 } 419 420 for(TExample::iterator iter = ex.begin(); iter != values_end; iter++, index++) { 421 if(iter>isRegular()) { 422 if(iter>varType == TValue::FLOATVAR) { 423 value = iter>floatV; 424 } else if (iter>varType == TValue::INTVAR) { 425 value = iter>intV; 426 } else { 427 continue; 428 } 429 430 // Only add non zero values (speedup due to sparseness) 431 if (value != 0 && is_valid(value)) { 432 node>index = index; 433 node>value = value; 390 434 node++; 391 435 } 392 436 } 393 437 } 394 if(type == 1){ /*one dummy attr so we can pickle the classifier and keep the SV index in the training table*/ 395 node>index=1; 396 node>value=last; 397 node++; 398 } 399 //cout<<(node1)>index<<endl<<(node2)>index<<endl; 400 node>index=1; 401 node>value=last; 438 439 // Sentinel 440 node>index = 1; 441 node>value = last; 402 442 node++; 403 443 return node; … … 406 446 class SVM_NodeSort{ 407 447 public: 408 bool operator() (const svm_node &lhs, const svm_node &rhs) {448 bool operator() (const svm_node &lhs, const svm_node &rhs) { 409 449 return lhs.index < rhs.index; 410 450 } 411 451 }; 412 452 413 svm_node* example_to_svm_sparse(const TExample &ex, svm_node* node, float last=0.0, bool useNonMeta=false){ 414 svm_node *first=node; 415 int j=1; 416 int index=1; 417 if(useNonMeta) 418 for(TExample::iterator i=ex.begin(); i!=ex.end(); i++){ 419 if(i>isRegular() && i!=&ex.getClass()){ 420 if(i>varType==TValue::FLOATVAR) 421 node>value=float(*i); 422 else 423 node>value=int(*i); 424 node>index=index; 425 if(node>value==numeric_limits<float>::signaling_NaN()  426 node>value==numeric_limits<int>::max()  427 node>value == 0) 428 node; 453 svm_node* example_to_svm_sparse(const TExample &ex, svm_node* node, double last=0.0, bool include_regular=false) { 454 svm_node *first = node; 455 int index = 1; 456 double value; 457 458 if (include_regular) { 459 node = example_to_svm(ex, node); 460 // Rewind the sentinel 461 node; 462 assert(node>index == 1); 463 index += ex.domain>variables>size(); 464 } 465 466 for (TMetaValues::const_iterator iter=ex.meta.begin(); iter!=ex.meta.end(); iter++) { 467 if(iter>second.isRegular()) { 468 if(iter>second.varType == TValue::FLOATVAR) { 469 value = iter>second.floatV; 470 } else if (iter>second.varType == TValue::INTVAR) { 471 value = iter>second.intV; 472 } else { 473 continue; 474 } 475 476 if (value != 0 && is_valid(value)) { 477 // add the ( meta_id) to index; meta_ids are negative 478 node>index = index  iter>first; 479 node>value = value; 429 480 node++; 430 481 } 431 index++; 432 } 433 for(TMetaValues::const_iterator i=ex.meta.begin(); i!=ex.meta.end();i++,j++){ 434 if(i>second.isRegular()){ 435 if(i>second.varType==TValue::FLOATVAR) 436 node>value=float(i>second); 437 else 438 node>value=int(i>second); 439 node>index = index  i>first; 440 441 if(node>value==numeric_limits<float>::signaling_NaN()  442 node>value==numeric_limits<int>::max()) 443 node; 444 node++; 445 } 446 } 482 } 483 } 484 485 // sort the nodes by index (metas are not ordered) 447 486 sort(first, node, SVM_NodeSort()); 448 //cout<<first>index<<endl<<(first+1)>index<<endl; 449 node>index=1; 450 node>value=last; 487 488 // Sentinel 489 node>index = 1; 490 node>value = last; 451 491 node++; 452 492 return node; … … 457 497 * Used for prediction when using the PRECOMPUTED kernel. 458 498 */ 459 svm_node* example_to_svm_precomputed(const TExample &ex, PExampleGenerator examples, PKernelFunc kernel, svm_node* node){ 499 svm_node* example_to_svm_precomputed(const TExample &ex, PExampleGenerator examples, PKernelFunc kernel, svm_node* node) { 500 // Required node with index 0 460 501 node>index = 0; 461 502 node>value = 0.0; // Can be any value. … … 464 505 PEITERATE(iter, examples){ 465 506 node>index = ++k; 466 node>value = kernel .getReference()(*iter, ex);507 node>value = kernel>operator()(*iter, ex); 467 508 node++; 468 509 } 469 node>index = 1; // sentry 510 511 // Sentinel 512 node>index = 1; 470 513 node++; 471 514 return node; … … 473 516 474 517 int getNumOfElements(const TExample &ex, bool meta=false, bool useNonMeta=false){ 475 if (!meta)476 return std::max(ex.domain>attributes>size() +1, 2);477 else {478 int count =1; //we need one to indicate the end of a sequence479 if (useNonMeta)480 count +=ex.domain>attributes>size();481 for (TMetaValues::const_iterator i=ex.meta.begin(); i!=ex.meta.end();i++)482 if(i >second.isRegular())518 if (!meta) 519 return std::max(ex.domain>attributes>size() + 1, 2); 520 else { 521 int count = 1; // we need one to indicate the end of a sequence 522 if (useNonMeta) 523 count += ex.domain>attributes>size(); 524 for (TMetaValues::const_iterator iter=ex.meta.begin(); iter!=ex.meta.end();iter++) 525 if(iter>second.isRegular()) 483 526 count++; 484 527 return std::max(count,2); … … 486 529 } 487 530 488 int getNumOfElements(PExampleGenerator &examples, bool meta=false, bool useNonMeta=false) {489 if (!meta)490 return getNumOfElements(*(examples>begin()), meta) *examples>numberOfExamples();491 else {492 int count =0;531 int getNumOfElements(PExampleGenerator &examples, bool meta=false, bool useNonMeta=false) { 532 if (!meta) 533 return getNumOfElements(*(examples>begin()), meta) * examples>numberOfExamples(); 534 else { 535 int count = 0; 493 536 for(TExampleGenerator::iterator ex(examples>begin()); ex!=examples>end(); ++ex){ 494 count +=getNumOfElements(*ex, meta, useNonMeta);537 count += getNumOfElements(*ex, meta, useNonMeta); 495 538 } 496 539 return count; … … 498 541 } 499 542 500 #include "symmatrix.hpp"501 543 svm_node* init_precomputed_problem(svm_problem &problem, PExampleTable examples, TKernelFunc &kernel){ 502 544 int n_examples = examples>numberOfExamples(); … … 506 548 for (j = 0; j <= i; j++){ 507 549 matrix>getref(i, j) = kernel(examples>at(i), examples>at(j)); 508 // cout << i << " " << j << " " << matrix>getitem(i, j) << endl;509 550 } 510 551 svm_node *x_space = Malloc(svm_node, n_examples * (n_examples + 2)); … … 701 742 x_space = init_precomputed_problem(prob, train_data, kernelFunc.getReference()); 702 743 703 if (param.gamma ==0)704 param.gamma =1.0f/(float(numElements)/float(prob.l)1);705 706 const char* error =svm_check_parameter(&prob, ¶m);744 if (param.gamma == 0) 745 param.gamma = 1.0f / (float(numElements) / float(prob.l)  1); 746 747 const char* error = svm_check_parameter(&prob, ¶m); 707 748 if (error){ 708 749 free(x_space); … … 750 791 } 751 792 752 svm_node* TSVMLearner::example_to_svm(const TExample &ex, svm_node* node, float last, int type){753 return ::example_to_svm(ex, node, last , type);793 svm_node* TSVMLearner::example_to_svm(const TExample &ex, svm_node* node, double last){ 794 return ::example_to_svm(ex, node, last); 754 795 } 755 796 756 797 svm_node* TSVMLearner::init_problem(svm_problem &problem, PExampleTable examples, int n_elements){ 757 798 problem.l = examples>numberOfExamples(); 758 problem.y = Malloc(double ,problem.l);799 problem.y = Malloc(double, problem.l); 759 800 problem.x = Malloc(svm_node*, problem.l); 760 801 svm_node *x_space = Malloc(svm_node, n_elements); … … 770 811 problem.y[i] = examples>at(i).getClass().intV; 771 812 } 813 814 // cout << problem << endl; 815 772 816 return x_space; 773 817 } … … 799 843 } 800 844 801 svm_node* TSVMLearnerSparse::example_to_svm(const TExample &ex, svm_node* node, float last, int type){845 svm_node* TSVMLearnerSparse::example_to_svm(const TExample &ex, svm_node* node, double last){ 802 846 return ::example_to_svm_sparse(ex, node, last, useNonMeta); 803 847 } … … 1014 1058 } 1015 1059 1016 svm_node *TSVMClassifier::example_to_svm(const TExample &ex, svm_node *node, float last, int type){1017 return ::example_to_svm(ex, node, last , type);1060 svm_node *TSVMClassifier::example_to_svm(const TExample &ex, svm_node *node, double last){ 1061 return ::example_to_svm(ex, node, last); 1018 1062 } 1019 1063 … … 1021 1065 return ::getNumOfElements(example); 1022 1066 } 1023 svm_node *TSVMClassifierSparse::example_to_svm(const TExample &ex, svm_node *node, float last, int){1067 svm_node *TSVMClassifierSparse::example_to_svm(const TExample &ex, svm_node *node, double last){ 1024 1068 return ::example_to_svm_sparse(ex, node, last, useNonMeta); 1025 1069 } … … 1030 1074 1031 1075 1076 #include "libsvm_interface.ppp" 
source/orange/libsvm_interface.hpp
r11607 r11650 103 103 104 104 protected: 105 virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, float last=0.0, int type=0);105 virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, double last=0.0); 106 106 virtual svm_node* init_problem(svm_problem &problem, PExampleTable examples, int n_elements); 107 107 virtual int getNumOfElements(PExampleGenerator examples); … … 115 115 bool useNonMeta; //P include non meta attributes in the learning process 116 116 protected: 117 virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, float last=0.0, int type=0);117 virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, double last=0.0); 118 118 virtual int getNumOfElements(PExampleGenerator examples); 119 119 virtual TSVMClassifier* createClassifier( … … 155 155 156 156 protected: 157 virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, float last=0.0, int type=0);157 virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, double last=0.0); 158 158 virtual int getNumOfElements(const TExample& example); 159 159 … … 179 179 180 180 protected: 181 virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, float last=0.0, int type=0);181 virtual svm_node* example_to_svm(const TExample &ex, svm_node* node, double last=0.0); 182 182 virtual int getNumOfElements(const TExample& example); 183 183 };
Note: See TracChangeset
for help on using the changeset viewer.