Example #1
0
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;
}
Example #2
0
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;
}
Example #3
0
void BigArray<T>::getMatrix(unsigned long startingRow, unsigned long startingCol, unsigned long numRows, unsigned long numCols, gMat2D<T>&result) const
{
    if(startingRow >= this->numrows || startingCol >= this->numcols)
        throw gException(Exception_Index_Out_of_Bound);

    if(startingRow+numRows > this->numrows || startingCol+numCols > this->numcols)
        throw gException(Exception_Index_Out_of_Bound);

    result.resize(numRows, numCols);

    getMatrix(startingRow, startingCol, numRows, numCols, result.getData());
}
Example #4
0
    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;
    }