/** 
 * Train the SVM using several cross validation iterations
 * @param pNorm Normalization object
 */
void CrossValidation::train(Normalizer * pNorm) {

  if (VERB > 0) {
    cerr << "---Training with Cpos";
    if (selectedCpos_ > 0) {
      cerr << "=" << selectedCpos_;
    } else {
      cerr << " selected by cross validation";
    }
    cerr << ", Cneg";
    if (selectedCneg_ > 0) {
      cerr << "=" << selectedCneg_;
    } else {
      cerr << " selected by cross validation";
    }
    cerr << ", fdr=" << selectionFdr_ << endl;
  }
  
  // iterate
  int foundPositivesOldOld = 0, foundPositivesOld = 0, foundPositives = 0; 
  for (unsigned int i = 0; i < niter_; i++) {
    if (VERB > 1) {
      cerr << "Iteration " << i + 1 << " :\t";
    }
    
    bool updateDOC = true;
    foundPositives = doStep(updateDOC);
    
    if (VERB > 1) {
      cerr << "After the iteration step, " << foundPositives
          << " target PSMs with q<" << testFdr_
          << " were estimated by cross validation" << endl;
    }
    if (VERB > 2) {
      cerr << "Obtained weights" << endl;
      printAllWeights(cerr, pNorm);
    }
    if (foundPositives > 0 && foundPositivesOldOld > 0 && quickValidation_ &&
           (static_cast<double>(foundPositives - foundPositivesOldOld) <= 
           foundPositivesOldOld * requiredIncreaseOver2Iterations_)) {
      if (VERB > 1) {
        std::cerr << "Performance increase over the last two iterations " <<
            "indicate that the algorithm has converged\n" <<
            "(" << foundPositives << " vs " << foundPositivesOldOld << ")" << 
            std::endl;
      }
      break;
    }
    foundPositivesOldOld = foundPositivesOld;    
    foundPositivesOld = foundPositives;
  }
  if (VERB == 2) {
    cerr
    << "Obtained weights (only showing weights of first cross validation set)"
    << endl;
    printSetWeights(cerr, 0, pNorm);
  }
  foundPositives = 0;
  for (size_t set = 0; set < numFolds_; ++set) {
    if (DataSet::getCalcDoc()) {
      testScores_[set].getDOC().copyDOCparameters(trainScores_[set].getDOC());
      testScores_[set].setDOCFeatures();
    }
    foundPositives += testScores_[set].calcScores(w_[set], testFdr_);
  }
  if (VERB > 0) {
    std::cerr << "After all training done, " << foundPositives << 
                 " target PSMs with q<" << testFdr_ << 
                 " were found when measuring on the test set" << std::endl;
  }  
}
Esempio n. 2
0
/** 
 * Train the SVM using several cross validation iterations
 * @param w list of normal vectors
 */
void CrossValidation::train(Normalizer * pNorm) {

  if (VERB > 0) {
    cerr << "---Training with Cpos";
    if (selectedCpos > 0) {
      cerr << "=" << selectedCpos;
    } else {
      cerr << " selected by cross validation";
    }
    cerr << ", Cneg";
    if (selectedCneg > 0) {
      cerr << "=" << selectedCneg;
    } else {
      cerr << " selected by cross validation";
    }
    cerr << ", fdr=" << selectionFdr << endl;
  }
  
  // iterate
  int foundPositivesOldOld=0, foundPositivesOld=0, foundPositives=0; 
  for (unsigned int i = 0; i < niter; i++) {
    if (VERB > 1) {
      cerr << "Iteration " << i + 1 << " :\t";
    }
    
    foundPositives = xv_step(true);
    
    if (VERB > 1) {
      cerr << "After the iteration step, " << foundPositives
          << " target PSMs with q<" << selectionFdr
          << " were estimated by cross validation" << endl;
    }
    if (VERB > 2) {
      cerr << "Obtained weights" << endl;
      printAllWeights(cerr, pNorm);
    }
    if (foundPositives>0 && foundPositivesOldOld>0 && quickValidation) {
      if ((double)(foundPositives-foundPositivesOldOld)<=(foundPositivesOldOld*requiredIncreaseOver2Iterations)) {
        if (VERB > 1) {
          cerr << "Performance increase over the last two iterations indicate that the algorithm has converged" << endl;
          cerr << "(" << foundPositives << " vs " << foundPositivesOldOld << ")" << endl;
        }
        break;
      }
    }    
    foundPositivesOldOld=foundPositivesOld;    
    foundPositivesOld=foundPositives;
  }
  if (VERB == 2) {
    cerr
    << "Obtained weights (only showing weights of first cross validation set)"
    << endl;
    printSetWeights(cerr, 0, pNorm);
  }
  foundPositives = 0;
  for (size_t set = 0; set < xval_fold; ++set) {
    if (DataSet::getCalcDoc()) {
      xv_test[set].getDOC().copyDOCparameters(xv_train[set].getDOC());
      xv_test[set].setDOCFeatures();
    }
    foundPositives += xv_test[set].calcScores(w[set], testFdr);
  }
  if (VERB > 0) {
    cerr << "After all training done, " << foundPositives << " target PSMs with q<"
        << testFdr << " were found when measuring on the test set"
        << endl;
  }  
}