コード例 #1
0
ファイル: gmath.cpp プロジェクト: xufango/contrib_bk
GURLS_EXPORT void svd(const gMat2D<float>& A, gMat2D<float>& U, gVec<float>& W, gMat2D<float>& Vt) {

    char jobu = 'S', jobvt = 'S';
    int m = A.rows();
    int n = A.cols();
    int k = std::min<int>(m, n);

    if ((int)W.getSize() < k) {
        throw gException("The length of vector W must be at least equal to the minimum dimension of the input matrix A");
    }
    if ((int)U.rows() < m || (int)U.cols() < k) {
        throw gException("Please check the dimensions of the matrix U where to store the singular vectors");
    }
    if ((int)Vt.rows() < k || (int)Vt.cols() < n) {
        throw gException("Please check the dimensions of the matrix Vt where to store the rigth singular vectors");
    }

    int lda = A.cols();
    int ldu = U.cols();
    int ldvt = Vt.cols();
    int info, lwork = std::max<int>(3*k+std::max<int>(m,n), 5*k);
    float* work = new float[lwork];
    float* copy = new float[m*n];
    A.asarray(copy, m*n);
    sgesvd_(&jobu, &jobvt, &n, &m, copy, &lda, W.getData(), Vt.getData(), &ldvt, U.getData(), &ldu, work, &lwork, &info);
    delete[] work;
    delete[] copy;
}
コード例 #2
0
ファイル: gmath.cpp プロジェクト: xufango/contrib_bk
GURLS_EXPORT void dot(const gMat2D<float>& A, const gMat2D<float>& B, gMat2D<float>& C) {

    dot(A.getData(), B.getData(), C.getData(),
        A.rows(), A.cols(),
        B.rows(), B.cols(),
        C.rows(), C.cols(),
        CblasNoTrans, CblasNoTrans, CblasRowMajor);
}
コード例 #3
0
ファイル: gmath.cpp プロジェクト: elen4/GURLS
GURLS_EXPORT void dot(const gMat2D<double>& A, const gMat2D<double>& B, gMat2D<double>& C)
{

    dot(A.getData(), B.getData(), C.getData(),
        A.rows(), A.cols(),
        B.rows(), B.cols(),
        C.rows(), C.cols(),
        CblasNoTrans, CblasNoTrans, CblasColMajor);
}
コード例 #4
0
ファイル: bigarray.hpp プロジェクト: elen4/GURLS
void BigArray<T>::getMatrix(unsigned long startingRow, unsigned long startingCol, gMat2D<T>&result) const
{
    if(startingRow >= this->numrows || startingCol >= this->numcols)
        throw gException(Exception_Index_Out_of_Bound);

    if(startingRow+result.rows() > this->numrows || startingCol+result.cols() > this->numcols)
        throw gException(Exception_Index_Out_of_Bound);

    getMatrix(startingRow, startingCol, result.rows(), result.cols(), result.getData());
}
コード例 #5
0
ファイル: gmath.cpp プロジェクト: xufango/contrib_bk
GURLS_EXPORT void lu(gMat2D<float>& A, gVec<int>& pv) {

    unsigned int k = std::min<unsigned int>(A.cols(), A.rows());
    if (pv.getSize() != k) {
        throw gException("The lenghth of pv must be equal to the minimun dimension of A");
    }
    int info;
    int m = A.rows();
    int n = A.cols();
    int lda = A.cols();
    sgetrf_(&m, &n, A.getData(), &lda, pv.getData(), &info);

}
コード例 #6
0
ファイル: gmath.cpp プロジェクト: elen4/GURLS
GURLS_EXPORT void svd(const gMat2D<float>& A, gMat2D<float>& U, gVec<float>& W, gMat2D<float>& Vt)
{
    float* Ubuf;
    float* Sbuf;
    float* Vtbuf;

    int Urows, Ucols;
    int Slen;
    int Vtrows, Vtcols;

    gurls::svd(A.getData(), Ubuf, Sbuf, Vtbuf,
             A.rows(), A.cols(),
             Urows, Ucols, Slen, Vtrows, Vtcols);


    U.resize(Urows, Ucols);
    copy(U.getData(), Ubuf, U.getSize());

    W.resize(Slen);
    copy(W.getData(), Sbuf, Slen);

    Vt.resize(Vtrows, Vtcols);
    copy(Vt.getData(), Vtbuf, Vt.getSize());

    delete [] Ubuf;
    delete [] Sbuf;
    delete [] Vtbuf;
}
コード例 #7
0
ファイル: gmath.cpp プロジェクト: elen4/GURLS
GURLS_EXPORT void eig(const gMat2D<float>& A, gVec<float>& Wr, gVec<float>& Wi)
{
    if (A.cols() != A.rows())
        throw gException("The input matrix A must be squared");

    float* Atmp = new float[A.getSize()];
    copy(Atmp, A.getData(), A.getSize());

    char jobvl = 'N', jobvr = 'N';
    int n = A.cols(), lda = A.cols(), ldvl = 1, ldvr = 1;
    int info, lwork = 4*n;
    float* work = new float[lwork];

    sgeev_(&jobvl, &jobvr, &n, Atmp, &lda, Wr.getData(), Wi.getData(), NULL, &ldvl, NULL, &ldvr, work, &lwork, &info);

    delete[] Atmp;
    delete[] work;

    if(info != 0)
    {
        std::stringstream str;
        str << "Eigenvalues/eigenVectors computation failed, error code " << info << ";" << std::endl;
        throw gException(str.str());
    }
}
コード例 #8
0
float RLSlinear::predictModel(gMat2D<float> &X, bool newGURLS)
{
    
    if (modelLinearRLS==NULL)
    {
        cout << "Error: train model first!" << endl;
        return 0.0;
    }

    if (newGURLS==false)
    {
        gMat2D<float> empty;
        RLS.run(X,empty,*modelLinearRLS,"test");
    }
    else
    {
        unsigned long nTest = X.rows();
        unsigned long d = X.cols();

        gMat2D<float> empty(nTest, 1);
        unsigned long t = empty.cols();

        float* perfBuffer = new float[empty.cols()];
        float* predBuffer = new float[empty.cols()*nTest];

        test(*modelLinearRLS, X.getData(), empty.getData(), predBuffer, perfBuffer, nTest, d, t, "auto");
    }

    // Retrieve prediction
    gMat2D<float>& pred = modelLinearRLS->getOptValue<OptMatrix<gMat2D<float> > >("pred");
    
    return pred(0,0);
}
コード例 #9
0
ファイル: gmath.cpp プロジェクト: xufango/contrib_bk
GURLS_EXPORT void inv(const gMat2D<float>& A, gMat2D<float>& Ainv, InversionAlgorithm alg){
    Ainv = A;
    int k = std::min<int>(Ainv.cols(), Ainv.rows());
    int info;
    int* ipiv = new int[k];
    int m = Ainv.rows();
    int n = Ainv.cols();
    int lda = Ainv.cols();
    float* work = new float[n];

    sgetrf_(&m, &n, Ainv.getData(), &lda, ipiv, &info);

    sgetri_(&m, Ainv.getData(), &lda, ipiv, work, &n, &info);
    delete[] ipiv;
    delete[] work;
}
コード例 #10
0
ファイル: bigarray.hpp プロジェクト: elen4/GURLS
BigArray<T>::BigArray(std::wstring fileName, const gMat2D<T>& mat)
{
    std::string fName = std::string(fileName.begin(), fileName.end());

    init(fName, mat.rows(), mat.cols());

    MPI_Barrier(MPI_COMM_WORLD);
}
コード例 #11
0
ファイル: recrlswrapper.hpp プロジェクト: AzothAmmo/GURLS
void RecursiveRLSWrapper<T>::train(const gMat2D<T> &X, const gMat2D<T> &y)
{
    this->opt->removeOpt("split");
    this->opt->removeOpt("paramsel");
    this->opt->removeOpt("optimizer");
    this->opt->removeOpt("kernel");


    SplitHo<T> splitTask;
    GurlsOptionsList* split = splitTask.execute(X, y, *(this->opt));
    this->opt->addOpt("split", split);


    const gMat2D<unsigned long>& split_indices = split->getOptValue<OptMatrix<gMat2D<unsigned long> > >("indices");
    const gMat2D<unsigned long>& split_lasts = split->getOptValue<OptMatrix<gMat2D<unsigned long> > >("lasts");

    const unsigned long n = X.rows();
    const unsigned long d = X.cols();
    const unsigned long t = y.cols();

    const unsigned long last = split_lasts.getData()[0];
    const unsigned long nva = n-last;

    unsigned long* va = new unsigned long[nva];
    copy(va, split_indices.getData()+last, nva);

    gMat2D<T>* Xva = new gMat2D<T>(nva, d);
    gMat2D<T>* yva = new gMat2D<T>(nva, t);

    subMatrixFromRows(X.getData(), n, d, va, nva, Xva->getData());
    subMatrixFromRows(y.getData(), n, t, va, nva, yva->getData());

    gMat2D<T>* XtX = new gMat2D<T>(d, d);
    gMat2D<T>* Xty = new gMat2D<T>(d, t);


    dot(X.getData(), X.getData(), XtX->getData(), n, d, n, d, d, d, CblasTrans, CblasNoTrans, CblasColMajor);
    dot(X.getData(), y.getData(), Xty->getData(), n, d, n, t, d, t, CblasTrans, CblasNoTrans, CblasColMajor);


    GurlsOptionsList* kernel = new GurlsOptionsList("kernel");
    kernel->addOpt("XtX", new OptMatrix<gMat2D<T> >(*XtX));
    kernel->addOpt("Xty", new OptMatrix<gMat2D<T> >(*Xty));

    kernel->addOpt("Xva", new OptMatrix<gMat2D<T> >(*Xva));
    kernel->addOpt("yva", new OptMatrix<gMat2D<T> >(*yva));

    nTot = n;
    this->opt->addOpt("kernel", kernel);

    ParamSelHoPrimal<T> paramselTask;
    this->opt->addOpt("paramsel", paramselTask.execute(X, y, *(this->opt)));


    RLSPrimalRecInit<T> optimizerTask;
    this->opt->addOpt("optimizer", optimizerTask.execute(X, y, *(this->opt)));
}
コード例 #12
0
ファイル: gmath.cpp プロジェクト: cshen/GURLS
GURLS_EXPORT void cholesky(const gMat2D<float>& A, gMat2D<float>& L, bool upper)
{

    float* chol = cholesky<float>(A.getData(), A.rows(), A.cols(), upper);

    copy(L.getData(), chol, A.getSize());

    delete [] chol;
}
コード例 #13
0
ファイル: gmath.cpp プロジェクト: elen4/GURLS
GURLS_EXPORT void pinv(const gMat2D<float>& A, gMat2D<float>& Ainv, float RCOND)
{
    int r, c;
    float* inv = pinv(A.getData(), A.rows(), A.cols(), r, c, &RCOND);

    Ainv.resize(r, c);
    gurls::copy(Ainv.getData(), inv, r*c);

    delete[] inv;
}
コード例 #14
0
ファイル: gmath.cpp プロジェクト: elen4/GURLS
GURLS_EXPORT void dot(const gMat2D<float>& A, const gVec<float>& x, gVec<float>& y)
{
    if ( (A.cols() != x.getSize()) ||  (A.rows() != y.getSize()))
        throw gException(Exception_Inconsistent_Size);


    // y = alpha*A*x + beta*y
    float alpha = 1.0f;
    float beta = 0.0f;

    char transA = 'N';

    int m = A.rows();
    int n = A.cols();
    int lda = m;
    int inc = 1;

    sgemv_(&transA, &m, &n, &alpha, const_cast<float*>(A.getData()), &lda,
          const_cast<float*>(x.getData()), &inc, &beta, y.getData(), &inc);
}
コード例 #15
0
ファイル: gmath.cpp プロジェクト: xufango/contrib_bk
GURLS_EXPORT void eig(const gMat2D<float>& A, gVec<float>& Wr, gVec<float>& Wi) {

    if (A.cols() != A.rows()) {
        throw gException("The input matrix A must be squared");
    }

    char jobvl = 'N', jobvr = 'N';
    int n = A.cols(), lda = A.cols(), ldvl = 1, ldvr = 1;
    int info, lwork = 4*n;
    float* work = new float[lwork];
    sgeev_(&jobvl, &jobvr, &n, const_cast<gMat2D<float>&>(A).getData(), &lda, Wr.getData(), Wi.getData(), NULL, &ldvl, NULL, &ldvr, work, &lwork, &info);
    delete[] work;
}
コード例 #16
0
ファイル: gmath.cpp プロジェクト: xufango/contrib_bk
GURLS_EXPORT void dot(const gMat2D<double>& A, const gVec<double>& x, gVec<double>& y){

    if ( (A.cols() != x.getSize()) ||  (A.rows() != y.getSize()))
        throw gException(Exception_Inconsistent_Size);


    // y = alpha*A*x + beta*y
    double alpha = 1.0f;
    double beta = 0.0f;

    char transA = 'T';  // row major matrix

    int m = A.cols();   // row major matrix
    int n = A.rows();   // row major matrix
    int lda = m;        // row major matrix
    int inc = 1;

    dgemv_(&transA, &m, &n, &alpha, const_cast<double*>(A.getData()), &lda,
          const_cast<double*>(x.getData()), &inc, &beta,
          y.getData(), &inc);

}
コード例 #17
0
ファイル: gmath.cpp プロジェクト: xufango/contrib_bk
GURLS_EXPORT void cholesky(const gMat2D<float>& A, gMat2D<float>& L, bool upper){

    typedef float T;
    L = A;

    int LDA = A.rows();
    int n = A.cols();
    char UPLO = upper? 'U' : 'L';
    int info;

    spotrf_(&UPLO,&n, L.getData(),&LDA,&info);

    // This is required because we adopted a column major order to store the
    // data into matrices
    gMat2D<T> tmp(L.rows(), L.cols());
    if (!upper){
        L.uppertriangular(tmp);
    } else {
        L.lowertriangular(tmp);
    }
    tmp.transpose(L);

}
コード例 #18
0
ファイル: gmath.cpp プロジェクト: xufango/contrib_bk
GURLS_EXPORT void eig(const gMat2D<float>& A, gMat2D<float>& V, gVec<float>& Wr, gVec<float>& Wi) {

    if (A.cols() != A.rows()) {
        throw gException("The input matrix A must be squared");
    }

    char jobvl = 'N', jobvr = 'V';
    int n = A.cols(), lda = A.cols(), ldvl = 1, ldvr = A.cols();
    int info, lwork = 4*n;
    float* work = new float[lwork];
    gMat2D<float> Atmp = A;
    gMat2D<float> Vtmp = V;
    sgeev_(&jobvl, &jobvr, &n, Atmp.getData(), &lda, Wr.getData(), Wi.getData(), NULL, &ldvl, Vtmp.getData(), &ldvr, work, &lwork, &info);
    Vtmp.transpose(V);
    delete[] work;
}
コード例 #19
0
void RLSlinear::trainModel(gMat2D<float> &X, gMat2D<float> &Y, bool newGURLS)
{
    if(modelLinearRLS!=NULL)
        delete modelLinearRLS;

    if (newGURLS==false)
    {
        OptTaskSequence *seq = new OptTaskSequence();

        OptProcess* process_train = new OptProcess();
        OptProcess* process_predict = new OptProcess();

        *seq << "kernel:linear" << "split:ho" << "paramsel:hodual";
        *process_train << GURLS::computeNsave << GURLS::compute << GURLS::computeNsave;
        *process_predict << GURLS::load << GURLS::ignore << GURLS::load;

        *seq<< "optimizer:rlsdual"<< "pred:dual";
        *process_train<<GURLS::computeNsave<<GURLS::ignore;
        *process_predict<<GURLS::load<<GURLS::computeNsave;

        GurlsOptionsList * processes = new GurlsOptionsList("processes", false);
        processes->addOpt("train",process_train);
        processes->addOpt("test",process_predict);

        modelLinearRLS = new GurlsOptionsList(className, true);

        modelLinearRLS->addOpt("seq", seq);
        modelLinearRLS->addOpt("processes", processes);

        RLS.run(X,Y,*modelLinearRLS,"train");

    }
    else
    {
        unsigned long n = X.rows();
        unsigned long d = X.cols();
        unsigned long t = Y.cols(); // should be 1

        modelLinearRLS = train(X.getData(), Y.getData(), n, d, t, "krls", "linear");
    }

}
コード例 #20
0
ファイル: bigarray.hpp プロジェクト: elen4/GURLS
BigArray<T>::BigArray(std::string fileName, const gMat2D<T>& mat)
{
    init(fileName, mat.rows(), mat.cols());

    MPI_Barrier(MPI_COMM_WORLD);
}
コード例 #21
0
ファイル: bigarray.hpp プロジェクト: elen4/GURLS
void BigArray<T>::setMatrix(unsigned long startingRow, unsigned long startingCol, const gMat2D<T>&value)
{
    setMatrix(startingRow, startingCol, value.getData(), value.rows(), value.cols());
}
コード例 #22
0
ファイル: gmath.cpp プロジェクト: xufango/contrib_bk
GURLS_EXPORT void pinv(const gMat2D<float>& A, gMat2D<float>& Ainv, float RCOND){

    /*

subroutine SGELSS 	( 	INTEGER  	M,
  INTEGER  	N,
  INTEGER  	NRHS,
  REAL,dimension( lda, * )  	A,
  INTEGER  	LDA,
  REAL,dimension( ldb, * )  	B,
  INTEGER  	LDB,
  REAL,dimension( * )  	S,
  REAL  	RCOND,
  INTEGER  	RANK,
  REAL,dimension( * )  	WORK,
  INTEGER  	LWORK,
  INTEGER  	INFO
 )

*/

    int M = A.rows();
    int N = A.cols();

    // The following step is required because we are currently storing
    // the matrices using a column-major order while LAPACK's
    // routines require row-major ordering
    float* a = new float[M*N];
    const float* ptr_A = A.getData();
    float* ptr_a = a;
    for (int j = 0; j < N ; j++){
        for (int i = 0; i < M ; i++){
            *ptr_a++ = *(ptr_A+i*N+j);
        }
    }
    int LDA = M;
    int LDB = std::max(M, N);
    int NRHS = LDB;

    float *b = new float[LDB*NRHS], *b_ptr = b;
    for (int i = 0; i < LDB*NRHS; i++){
        *b_ptr++=0.f;
    }
    b_ptr = b;
    for (int i = 0; i < std::min(LDB, NRHS); i++, b_ptr+=(NRHS+1)){
        *b_ptr = 1.f;
    }

    float* S = new float[std::min(M,N)];
    float condnum = 0.f; // The condition number of A in the 2-norm = S(1)/S(min(m,n)).

    if (RCOND < 0){
        RCOND = 0.f;
    }
    int RANK = -1; // std::min(M,N);
    int LWORK = -1; //2 * (3*LDB + std::max( 2*std::min(M,N), LDB));
    float* WORK = new float[1];
    /*
   INFO:
   = 0:	successful exit
   < 0:	if INFO = -i, the i-th argument had an illegal value.
   > 0:	the algorithm for computing the SVD failed to converge;
     if INFO = i, i off-diagonal elements of an intermediate
     bidiagonal form did not converge to zero.
   */
    int INFO;

    /* Query and allocate the optimal workspace */
    /*int res = */sgelss_( &M, &N, &NRHS, a, &LDA, b, &LDB, S, &RCOND, &RANK, WORK, &LWORK, &INFO);
    LWORK = static_cast<int>(WORK[0]);
    delete [] WORK;
    WORK = new float[LWORK];

    /*res = */sgelss_( &M, &N, &NRHS, a, &LDA, b, &LDB, S, &RCOND, &RANK, WORK, &LWORK, &INFO);
    // TODO: check INFO on exit
    condnum = S[0]/(S[std::min(M, N)]-1);


//    gMat2D<float> *tmp = new gMat2D<float>(b, LDB, LDB, false);

    float *ainv = new float[N*M];
    float* ptr_b = ainv;
    float* ptr_B = b;
    for (int i = 0; i < N ; i++){
        for (int j = 0; j < M ; j++){
            *(ptr_b+i*M+j) = *(ptr_B+j*NRHS+i);
        }
    }
    Ainv = * new gMat2D<float>(ainv, N, M, true);

//	gMat2D<float> *tmp = new gMat2D<float>(b, LDB, NRHS, false);
//	gMat2D<float> *tmp1 = new gMat2D<float>(NRHS, LDB);
//	tmp->transpose(*tmp1);
//	Ainv = * new gMat2D<float>(tmp1->getData(), N, M, true);
//	std::cout << "A = " << std::endl << A << std::endl;
//	std::cout << "pinv(A) = " << std::endl << Ainv << std::endl;
    delete [] S;
    delete [] WORK;
    delete [] a;
//	delete tmp, tmp1;
    delete [] b;
}
コード例 #23
0
ファイル: gmath.cpp プロジェクト: elen4/GURLS
GURLS_EXPORT void cholesky(const gMat2D<float>& A, gMat2D<float>& L, bool upper)
{
    cholesky<float>(A.getData(), A.rows(), A.cols(), L.getData(), upper);
}
コード例 #24
0
ファイル: parametricEstimator.cpp プロジェクト: GiuliaP/iRRLS
    bool configure(ResourceFinder &rf)
    {        
        string name=rf.find("name").asString().c_str();
        setName(name.c_str());
        
        // Set verbosity
        verbose = rf.check("verbose",Value(0)).asInt();
               
        // Set dimensionalities
        d = rf.check("d",Value(0)).asInt();
        t = rf.check("t",Value(0)).asInt();
        
        if (d <= 0 || t <= 0 )
        {
            printf("Error: Inconsistent feature or output dimensionalities!\n");
            return false;
        }
        
        // Set perf type WARNING: perf types should be defined as separate sister classes
        perfType = rf.check("perf",Value("RMSE")).asString();
        
        if ( perfType != "MSE" && perfType != "RMSE" && perfType != "nMSE" )
        {
            printf("Error: Inconsistent performance measure! Set to RMSE.\n");
            perfType = "RMSE";
        }
        
        // Set number of saved performance measurements
        numPred  = rf.check("numPred",Value("-1")).asInt();
        
        // Set number of saved performance measurements
        savedPerfNum = rf.check("savedPerfNum",Value("0")).asInt();
        if (savedPerfNum > numPred)
        {
            savedPerfNum = numPred;
            cout << "Warning: savedPerfNum > numPred, setting savedPerfNum = numPred" << endl;
        }
        
        //experimentCount = rf.check("experimentCount",Value("0")).asInt();
        experimentCount = rf.find("experimentCount").asInt();
        
        // Print Configuration
        cout << endl << "-------------------------" << endl;
        cout << "Configuration parameters:" << endl << endl;
        cout << "experimentCount = " << experimentCount << endl;
        cout << "d = " << d << endl;
        cout << "t = " << t << endl;
        cout << "perf = " << perfType << endl;
        cout << "-------------------------" << endl << endl;
       
        // Open ports
    
        string fwslash="/";
        inVec.open((fwslash+name+"/vec:i").c_str());
        printf("inVec opened\n");
        
        pred.open((fwslash+name+"/pred:o").c_str());
        printf("pred opened\n");
        
        perf.open((fwslash+name+"/perf:o").c_str());
        printf("perf opened\n");
        
        rpcPort.open((fwslash+name+"/rpc:i").c_str());
        printf("rpcPort opened\n");

        // Attach rpcPort to the respond() method
        attach(rpcPort);

        // Initialize random number generator
        srand(static_cast<unsigned int>(time(NULL)));

        // Initialize error structures
        error.resize(1,t);
        error = gMat2D<T>::zeros(1, t);          //
        
        if (savedPerfNum > 0)
        {
            storedError.resize(savedPerfNum,t);
            storedError = gMat2D<T>::zeros(savedPerfNum, t);          //MSE            
        }

        updateCount = 0;
        
        //------------------------------------------
        //         Pre-training
        //------------------------------------------

        if ( pretrain == 1 )
        {
            if ( pretr_type == "fromFile" )
            {
                //------------------------------------------
                //         Pre-training from file
                //------------------------------------------
                string trainFilePath = rf.getContextPath() + "/data/" + pretrainFile;
                
                try
                {
                    // Load data files
                    cout << "Loading data file..." << endl;
                    trainSet.readCSV(trainFilePath);

                    cout << "File " + trainFilePath + " successfully read!" << endl;
                    cout << "trainSet: " << trainSet << endl;
                    cout << "n_pretr = " << n_pretr << endl;
                    cout << "d = " << d << endl;

                    //WARNING: Add matrix dimensionality check!

                    // Resize Xtr
                    Xtr.resize( n_pretr , d );
                    
                    // Initialize Xtr
                    //Xtr.submatrix(trainSet , n_pretr , d);
                    Xtr.submatrix(trainSet , 0 , 0);
                    cout << "Xtr initialized!" << endl << Xtr << endl;

                    // Resize ytr
                    ytr.resize( n_pretr , t );
                    cout << "ytr resized!" << endl;
                    
                    // Initialize ytr
                    gVec<T> tmpCol(trainSet.rows());
                    cout << "tmpCol" << tmpCol << endl;
                    for ( int i = 0 ; i < t ; ++i )
                    {
                        cout << "trainSet(d + i): " << trainSet(d + i) << endl;
                        tmpCol = trainSet(d + i);
                        gVec<T> tmpCol1(n_pretr);

                        //cout << tmpCol.subvec( (unsigned int) n_pretr ,  (unsigned int) 0);       // WARNING: Fixed in latest GURLS version

                        gVec<T> locs(n_pretr);
                        for (int j = 0 ; j < n_pretr ; ++j)
                            locs[j] = j;
                        cout << "locs" << locs << endl;
                        gVec<T>& tmpCol2 = tmpCol.copyLocations(locs);
                        cout << "tmpCol2" << tmpCol2 << endl;
                    
                        //tmpCol1 = tmpCol.subvec( (unsigned int) n_pretr );
                        //cout << "tmpCol1: " << tmpCol1 << endl;
                        ytr.setColumn( tmpCol2 , (long unsigned int) i);
                    }
                    cout << "ytr initialized!" << endl;

                    // Compute variance for each output on the training set
                    gMat2D<T> varCols = gMat2D<T>::zeros(1,t);
                    gVec<T>* sumCols_v = ytr.sum(COLUMNWISE);          // Vector containing the column-wise sum
                    gMat2D<T> meanCols(sumCols_v->getData(), 1, t, 1); // Matrix containing the column-wise sum
                    meanCols /= n_pretr;        // Matrix containing the column-wise mean
                    
                    if (verbose) cout << "Mean of the output columns: " << endl << meanCols << endl;
                    
                    for (int i = 0; i < n_pretr; i++)
                    {
                        gMat2D<T> ytri(ytr[i].getData(), 1, t, 1);
                        varCols += (ytri - meanCols) * (ytri - meanCols); // NOTE: Temporary assignment
                    }
                    varCols /= n_pretr;     // Compute variance
                    if (verbose) cout << "Variance of the output columns: " << endl << varCols << endl;

                    // Initialize model
                    cout << "Batch pretraining the RLS model with " << n_pretr << " samples." << endl;
                    estimator.train(Xtr, ytr);
                }
                
                catch (gException& e)
                {
                    cout << e.getMessage() << endl;
                    return false;   // Terminate program. NOTE: May be worth to set up specific error return values
                }
            }
            else if ( pretr_type == "fromStream" )
            {
                //------------------------------------------
                //         Pre-training from stream
                //------------------------------------------
                
                try
                {
                    cout << "Pretraining from stream started. Listening on port vec:i." << n_pretr << " samples expected." << endl;

                    // Resize Xtr
                    Xtr.resize( n_pretr , d );
                    
                    // Resize ytr
                    ytr.resize( n_pretr , t );
                    
                    // Initialize Xtr
                    for (int j = 0 ; j < n_pretr ; ++j)
                    {
                        // Wait for input feature vector
                        if(verbose) cout << "Expecting input vector # " << j+1 << endl;
                        
                        Bottle *bin = inVec.read();    // blocking call
                        
                        if (bin != 0)
                        {
                            if(verbose) cout << "Got it!" << endl << bin->toString() << endl;

                            //Store the received sample in gMat2D format for it to be compatible with gurls++
                            for (int i = 0 ; i < bin->size() ; ++i)
                            {
                                if ( i < d )
                                {
                                    Xtr(j,i) = bin->get(i).asDouble();
                                }
                                else if ( (i>=d) && (i<d+t) )
                                {
                                    ytr(j, i - d ) = bin->get(i).asDouble();
                                }
                            }
                            if(verbose) cout << "Xtr[j]:" << endl << Xtr[j] << endl << "ytr[j]:" << endl << ytr[j] << endl;
                        }
                        else
                            --j;        // WARNING: bug while closing with ctrl-c
                    }
                    
                    cout << "Xtr initialized!" << endl;
                    cout << "ytr initialized!" << endl;
                        
                    // Compute variance for each output on the training set
                    gMat2D<T> varCols = gMat2D<T>::zeros(1,t);
                    gVec<T>* sumCols_v = ytr.sum(COLUMNWISE);          // Vector containing the column-wise sum
                    gMat2D<T> meanCols(sumCols_v->getData(), 1, t, 1); // Matrix containing the column-wise sum
                    meanCols /= n_pretr;        // Matrix containing the column-wise mean
                    
                    if (verbose) cout << "Mean of the output columns: " << endl << meanCols << endl;
                    
                    for (int i = 0; i < n_pretr; i++)
                    {
                        gMat2D<T> ytri(ytr[i].getData(), 1, t, 1);
                        varCols += (ytri - meanCols) * (ytri - meanCols); // NOTE: Temporary assignment
                    }
                    varCols /= n_pretr;     // Compute variance
                    if (verbose) cout << "Variance of the output columns: " << endl << varCols << endl;

                    // Initialize model
                    cout << "Batch pretraining the RLS model with " << n_pretr << " samples." << endl;
                    estimator.train(Xtr, ytr);
                }
                
                catch (gException& e)
                {
                    cout << e.getMessage() << endl;
                    return false;   // Terminate program. NOTE: May be worth to set up specific error return values
                }
            }
            
            // Print detailed pretraining information
            if (verbose) 
                estimator.getOpt().printAll();
        }
        
        return true;
    }
コード例 #25
0
ファイル: gmath.cpp プロジェクト: elen4/GURLS
GURLS_EXPORT void lu(gMat2D<float>& A)
{
    gVec<int> pv(std::min(A.cols(), A.rows()));
    lu(A, pv);
}