Ejemplo n.º 1
0
  /*
   * run cross validation for folds from idxStartFold to idxEndFold
   * @param idxStartFold index of the first fold
   * @param idxEndFold index of the last fold
   */
  void Cvlars::subrun(int idxStartFold,int idxEndFold)
  {
    //create test and control container
    STK::CArrayXX XControl( n_ - sizePartition_[idxStartFold], p_);
    STK::CVectorX yControl( n_ - sizePartition_[idxStartFold] );
    STK::CArrayXX XTest(sizePartition_[idxStartFold], p_);
    STK::CVectorX yTest(sizePartition_[idxStartFold] );
    STK::CVectorX yPred(sizePartition_[idxStartFold] );

    for(int i = idxStartFold ; i <= idxEndFold ; i++)
    {
      //fill the container
      int index = 1;
      int index2 = 1;
      for(int j = 1; j <= n_; j++)
      {
        if(partition_[j-1] != i)
        {
          yControl[index] = (*p_y_)[j];
          XControl.row(index)=p_X_->row(j);
          index++;
        }
        else
        {
          yTest[index2] = (*p_y_)[j];
          XTest.row(index2)=p_X_->row(j);
          index2++;
        }
      }

      //run lars on control data set
      HD::Lars lars(XControl,yControl,maxSteps_,intercept_,eps_);
      lars.run();

      for(int s = 1 ; s <= (int) index_.size(); s++)
      {
        //we compute the prediction of the y associated to XTest
        lars.predict(XTest,index_[s-1], lambdaMode_, yPred);

        //compute the residuals
        residuals_(s,i+1) = (yPred-yTest).square().sum()/sizePartition_[i];
      }
    }
  }
Ejemplo n.º 2
0
void SparseCoding<DictionaryInitializer>::OptimizeCode()
{
  // When using the Cholesky version of LARS, this is correct even if
  // lambda2 > 0.
  arma::mat matGram = trans(dictionary) * dictionary;

  for (size_t i = 0; i < data.n_cols; ++i)
  {
    // Report progress.
    if ((i % 100) == 0)
      Log::Debug << "Optimization at point " << i << "." << std::endl;

    bool useCholesky = true;
    regression::LARS lars(useCholesky, matGram, lambda1, lambda2);

    // Create an alias of the code (using the same memory), and then LARS will
    // place the result directly into that; then we will not need to have an
    // extra copy.
    arma::vec code = codes.unsafe_col(i);
    lars.Regress(dictionary, data.unsafe_col(i), code, false);
  }
}
Ejemplo n.º 3
0
  void Cvlars::run2()
   {
     //search the first and last fold with the same size
     std::vector<int> startIndex(1,0),endIndex(1,k_-1);
     int k = 0;
     for(int i = 1; i < k_; i++)
     {
       if(sizePartition_[i]!= sizePartition_[startIndex[k]])
       {
         startIndex.push_back(i);
         endIndex[k] = i-1;
         endIndex.push_back(k_-1);
         k++;
       }
     }

     //run for each size of fold
     //create test and control container
     #pragma omp parallel
     {
       #pragma omp for schedule(dynamic,1)
       for(int i = 0; i < k_ ; i++)
       {

         STK::CArrayXX XControl( n_ - sizePartition_[i], p_);
         STK::CVectorX yControl( n_ - sizePartition_[i] );
         STK::CArrayXX XTest(sizePartition_[i], p_);
         STK::CVectorX yTest(sizePartition_[i] );
         STK::CVectorX yPred(sizePartition_[i] );

         //fill the container
         int index = 1;
         int index2 = 1;
         for(int j = 1; j <= n_; j++)
         {
           if(partition_[j-1] != i)
           {
             yControl[index] = (*p_y_)[j];
             XControl.row(index)=p_X_->row(j);
             index++;
           }
           else
           {
             yTest[index2] = (*p_y_)[j];
             XTest.row(index2)=p_X_->row(j);
             index2++;
           }
         }

         //run lars on control data set
         HD::Lars lars(XControl,yControl,maxSteps_,intercept_,eps_);
         lars.run();

         for(int s = 1 ; s <= (int) index_.size(); s++)
         {
           //we compute the prediction of the y associated to XTest
           lars.predict(XTest,index_[s-1], lambdaMode_, yPred);

           //compute the residuals
           residuals_(s,i+1) = (yPred-yTest).square().sum()/sizePartition_[i];
         }
       }
     }//end parallel

     // compute mean prediction error for each index
     STK::CVectorX one(k_,1);
     cv_ = (residuals_ * one) / k_;

     // compute mean standard deviation of cv_ for each index
     for(int i = 1; i <= (int) index_.size(); i++)
       residuals_.row(i) -= cv_[i];
     residuals_ = residuals_.square();
     cvError_ = (residuals_ * one)/(k_-1)/k_;
     cvError_ = cvError_.sqrt();

   }