[pktools] 140/375: cv_opt must be above 1

Bas Couwenberg sebastic at xs4all.nl
Wed Dec 3 21:54:08 UTC 2014


This is an automated email from the git hooks/post-receive script.

sebastic-guest pushed a commit to branch upstream-master
in repository pktools.

commit bde25e6116cfee4baedf90f4a7894c1dd1f765c5
Author: Pieter Kempeneers <kempenep at gmail.com>
Date:   Mon Nov 18 20:47:41 2013 +0100

    cv_opt must be above 1
---
 ChangeLog                  | 14 ++++++++++++-
 src/apps/pkclassify_nn.cc  | 12 ++++++++---
 src/apps/pkclassify_nn.h   | 50 +++++++++++++++++++++++++++-------------------
 src/apps/pkclassify_svm.cc |  4 ++--
 src/apps/pkfs_svm.cc       |  2 +-
 src/apps/pkopt_svm.cc      |  8 ++++----
 6 files changed, 58 insertions(+), 32 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 1778328..ae3af38 100755
--- a/ChangeLog
+++ b/ChangeLog
@@ -142,4 +142,16 @@ version 2.4.2
 	default maximum distance changed from 3 to 0 (infinity)
  - still todo
 	change all projection options to a_srs
-	rename pkstat to pkstatascii
+ - pkclassify_nn.h
+	support reading ogr files with single feature (one band only: "B" or "Band")
+ - pkclassify_nn.cc
+	option -n for number of neurons
+	cross validation parameter can not be 1
+ - pkfs_nn.cc
+	cross validation parameter can not be 1
+ - pkclassify_svm.cc
+	cross validation parameter can not be 1
+ - pkopt_svm.cc
+	cross validation parameter can not be 1
+ - pkfs_svm.cc
+	cross validation parameter can not be 1
diff --git a/src/apps/pkclassify_nn.cc b/src/apps/pkclassify_nn.cc
index 5922502..e3091f0 100644
--- a/src/apps/pkclassify_nn.cc
+++ b/src/apps/pkclassify_nn.cc
@@ -50,7 +50,7 @@ int main(int argc, char *argv[])
   Optionpk<double> priors_opt("p", "prior", "prior probabilities for each class (e.g., -p 0.3 -p 0.3 -p 0.2 )", 0.0); 
   Optionpk<string> priorimg_opt("pim", "priorimg", "prior probability image (multi-band img with band for each class"); 
   Optionpk<unsigned short> cv_opt("cv", "cv", "n-fold cross validation mode",0);
-  Optionpk<unsigned int> nneuron_opt("\0", "nneuron", "number of neurons in hidden layers in neural network (multiple hidden layers are set by defining multiple number of neurons: -n 15 -n 1, default is one hidden layer with 5 neurons)", 5); 
+  Optionpk<unsigned int> nneuron_opt("n", "nneuron", "number of neurons in hidden layers in neural network (multiple hidden layers are set by defining multiple number of neurons: -n 15 -n 1, default is one hidden layer with 5 neurons)", 5); 
   Optionpk<float> connection_opt("\0", "connection", "connection reate (default: 1.0 for a fully connected network)", 1.0); 
   Optionpk<float> weights_opt("w", "weights", "weights for neural network. Apply to fully connected network only, starting from first input neuron to last output neuron, including the bias neurons (last neuron in each but last layer)", 0.0); 
   Optionpk<float> learning_opt("l", "learning", "learning rate (default: 0.7)", 0.7); 
@@ -412,10 +412,16 @@ int main(int argc, char *argv[])
     const float desired_error = 0.0003;
     const unsigned int iterations_between_reports = (verbose_opt[0])? maxit_opt[0]+1:0;
     if(verbose_opt[0]>=1){
+      cout << "number of features: " << nFeatures << endl;
       cout << "creating artificial neural network with " << nneuron_opt.size() << " hidden layer, having " << endl;
       for(int ilayer=0;ilayer<nneuron_opt.size();++ilayer)
         cout << nneuron_opt[ilayer] << " ";
       cout << "neurons" << endl;
+      //test
+      cout << "connection_opt[0]: " << connection_opt[0] << std::endl;
+      cout << "num_layers: " << num_layers << std::endl;
+      cout << "nFeatures: " << nFeatures << std::endl;
+      cout << "nneuron_opt[0]: " << nneuron_opt[0] << std::endl;
     }
     switch(num_layers){
     case(3):
@@ -461,7 +467,7 @@ int main(int argc, char *argv[])
       net[ibag].print_parameters();
     }
       
-    if(cv_opt[0]){
+    if(cv_opt[0]>1){
       if(verbose_opt[0])
         std::cout << "cross validation" << std::endl;
       vector<unsigned short> referenceVector;
@@ -518,7 +524,7 @@ int main(int argc, char *argv[])
 
     }
   }//for ibag
-  if(cv_opt[0]>0){
+  if(cv_opt[0]>1){
     assert(cm.nReference());
     std::cout << cm << std::endl;
     cout << "class #samples userAcc prodAcc" << endl;
diff --git a/src/apps/pkclassify_nn.h b/src/apps/pkclassify_nn.h
index b7bad1f..28ed4ec 100644
--- a/src/apps/pkclassify_nn.h
+++ b/src/apps/pkclassify_nn.h
@@ -66,21 +66,25 @@ template<typename T> unsigned int readDataImageShape(const string &filename,
       if(verbose)
         cout << *fit << " ";
       // size_t pos=(*fit).find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ ");
-      if(((*fit).substr(0,1)=="B")&&((*fit).substr(1).find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ ")!=string::npos)){
-        int theBand=atoi((*fit).substr(1).c_str());
-        if(bands.size()){
-          bool validBand=false;
-          for(int iband=0;iband<bands.size();++iband){
-            if(theBand==bands[iband])
-              validBand=true;
-          }
-          if(validBand)
-            ++fit;
-          else
-            fields.erase(fit);
-        }
-        else
-          ++fit;
+      if((*fit).substr(0,1)=="B"){
+	if((*fit).substr(1).find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ ")!=string::npos){
+	  int theBand=atoi((*fit).substr(1).c_str());
+	  if(bands.size()){
+	    bool validBand=false;
+	    for(int iband=0;iband<bands.size();++iband){
+	      if(theBand==bands[iband])
+		validBand=true;
+	    }
+	    if(validBand)
+	      ++fit;
+	    else
+	      fields.erase(fit);
+	  }
+	  else
+	    ++fit;
+	}
+	else if((*fit)=="B" || (*fit)=="Band")//B is only band
+	  ++fit;
       }
       else
         fields.erase(fit);
@@ -143,12 +147,16 @@ template<typename T> unsigned int readDataImageShape(const string &filename,
       if(verbose)
         cout << *fit << " ";
       // size_t pos=(*fit).find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ ");
-      if(((*fit).substr(0,1)=="B")&&((*fit).substr(1).find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ ")!=string::npos)){
-        int iband=atoi((*fit).substr(1).c_str());
-        if((start||end)&&(iband<start||iband>end))
-          fields.erase(fit);
-        else
-          ++fit;
+      if((*fit).substr(0,1)=="B"){
+	if((*fit).substr(1).find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ ")!=string::npos){
+	  int iband=atoi((*fit).substr(1).c_str());
+	  if((start||end)&&(iband<start||iband>end))
+	    fields.erase(fit);
+	  else
+	    ++fit;
+	}
+	else if(*fit=="B" || *fit=="Band")
+	  ++fit;
       }
       else
         fields.erase(fit);
diff --git a/src/apps/pkclassify_svm.cc b/src/apps/pkclassify_svm.cc
index d38a12e..ea68208 100644
--- a/src/apps/pkclassify_svm.cc
+++ b/src/apps/pkclassify_svm.cc
@@ -504,7 +504,7 @@ int main(int argc, char *argv[])
     if(verbose_opt[0])
       std::cout << "parameters ok, training" << std::endl;
     svm[ibag]=svm_train(&prob[ibag],&param[ibag]);
-    if(cv_opt[0]>0){
+    if(cv_opt[0]>1){
       double *target = Malloc(double,prob[ibag].l);
       svm_cross_validation(&prob[ibag],&param[ibag],cv_opt[0],target);
       assert(param[ibag].svm_type != EPSILON_SVR&&param[ibag].svm_type != NU_SVR);//only for regression
@@ -525,7 +525,7 @@ int main(int argc, char *argv[])
     // not free the memory used by svm_problem if you are still using the
     // svm_model produced by svm_train(). 
   }//for ibag
-  if(cv_opt[0]>0){
+  if(cv_opt[0]>1){
     assert(cm.nReference());
     std::cout << cm << std::endl;
     cout << "class #samples userAcc prodAcc" << endl;
diff --git a/src/apps/pkfs_svm.cc b/src/apps/pkfs_svm.cc
index a169763..b8b69de 100644
--- a/src/apps/pkfs_svm.cc
+++ b/src/apps/pkfs_svm.cc
@@ -154,7 +154,7 @@ double getCost(const vector<Vector2d<float> > &trainingFeatures)
     else if(cm.getClassIndex(type2string<short>(classValueMap[nameVector[iname]]))<0)
       cm.pushBackClassName(type2string<short>(classValueMap[nameVector[iname]]));
   }
-  if(cv_opt[0]>0){
+  if(cv_opt[0]>1){
     double *target = Malloc(double,prob.l);
     svm_cross_validation(&prob,&param,cv_opt[0],target);
     assert(param.svm_type != EPSILON_SVR&&param.svm_type != NU_SVR);//only for regression
diff --git a/src/apps/pkopt_svm.cc b/src/apps/pkopt_svm.cc
index 47f767a..cda45d4 100644
--- a/src/apps/pkopt_svm.cc
+++ b/src/apps/pkopt_svm.cc
@@ -163,7 +163,7 @@ double objFunction(const std::vector<double> &x, std::vector<double> &grad, void
     else if(cm.getClassIndex(type2string<short>(classValueMap[nameVector[iname]]))<0)
       cm.pushBackClassName(type2string<short>(classValueMap[nameVector[iname]]));
   }
-  if(cv_opt[0]>0){
+  if(cv_opt[0]>1){
     double *target = Malloc(double,prob.l);
     svm_cross_validation(&prob,&param,cv_opt[0],target);
     assert(param.svm_type != EPSILON_SVR&&param.svm_type != NU_SVR);//only for regression
@@ -219,7 +219,7 @@ double objFunction(const std::vector<double> &x, std::vector<double> &grad, void
     std::cout << "kappa: " << kappa << std::endl;
   }
   double cost=(costfunction_opt[0])? oa : kappa;
-  if(cost)
+  if(cost>0)
     error=1.0/cost;
   return(error);
 }
@@ -565,10 +565,10 @@ int main(int argc, char *argv[])
 
   assert(ccost_opt.size()>1);//must have boundaries at least (initial value is optional)
   if(ccost_opt.size()<3)//create initial value
-    ccost_opt.push_back(0.5*(ccost_opt[0]+ccost_opt[1]));
+    ccost_opt.push_back(sqrt(ccost_opt[0]*ccost_opt[1]));
   assert(gamma_opt.size()>1);//must have boundaries at least (initial value is optional)
   if(gamma_opt.size()<3)//create initial value
-    gamma_opt.push_back(0);//will be translated to 1.0/nFeatures
+    gamma_opt.push_back(sqrt(gamma_opt[0]*gamma_opt[1]));//will be translated to 1.0/nFeatures
   assert(ccost_opt.size()==3);//min, init, max
   assert(gamma_opt.size()==3);//min, init, max
   assert(gamma_opt[0]<gamma_opt[1]);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-grass/pktools.git



More information about the Pkg-grass-devel mailing list