Menu

Recommender systems - matrix factorization

Help
Anonymous
2013-04-05
2013-04-09
  • Anonymous

    Anonymous - 2013-04-05

    Hello,
    I am trying to use the Collaborative Filtering library, and in particular, the Matrix Factorization class. Here is my code:

    rank = 5;
    GClasses::GRand r(time(NULL));
    GClasses::GMatrixFactorization* G = new GClasses::GMatrixFactorization(rank,r);

    GClasses::GMatrix GM(0,3);
    int row_counter = 0;
    for (int i = 0;i < M;i++) {
    for (int j = 0;j < N;j++) {
    if (MissingEntriesMatrix[i][j]) {
    GM.newRow();
    GM[row_counter][0] = (double) i;
    GM[row_counter][1] = (double) j;
    GM[row_counter][2] = (double) RatingsMatrix[i][j];
    row_counter++;
    }
    }
    }

    G->train(GM);
    G->setRegularizer (0.1);
    GClasses::GMatrix GP = G->getP();
    GClasses::GMatrix
    GQ = G->getQ();

    cout << "number of rows " << GP->rows() << endl;
    cout << "number of cols " << GP->cols() << endl;

    for (int i = 0;i < GP->rows();i++) {
        for (int j = 0;j < GP->cols();j++) {
            cout << "element (" << i << "," << j << ") is " << *GP[i][j] << endl;
        }
    }
    

    It leads to a segmentation fault when printing row#1 of the matrix P. I am not sure why. Any help will be appreciated. The output is pasted below:

    number of rows in GP: 6040
    number of cols in GP: 6
    element (0,0) is 1.75919
    element (0,1) is 1.24583
    element (0,2) is 1.6636
    element (0,3) is 1.61166
    element (0,4) is 0.939978
    element (0,5) is 1.69074
    /prj/icecad/lsficengspool/x/1365184578.3205763: line 8: 16750 Segmentation fault

    Thanks,
    Harish

     
  • Mike Gashler

    Mike Gashler - 2013-04-05

    I cannot see any problems by visually inspecting this code. I think it will be necessary to build with debug symbols and run it in a debugger to obtain a call stack. I usually use Valgrind or KDevelop for this purpose.

     
  • Anonymous

    Anonymous - 2013-04-09

    Hi Mike,
    I did some fiddling around and the following modification to the code works:

    rank = 7;
    M = 20;
    N = 15;
    MFreg = 0.01;
    GClasses::GRand r(time(NULL));
    GClasses::GMatrixFactorization* G = new GClasses::GMatrixFactorization(rank,r);
    
    GClasses::GMatrix GM(0,3);
    int row_counter = 0;
    for (int i = 0;i < M;i++) {
        for (int j = 0;j < N;j++) {
            if (MissingEntriesMatrix[i][j]) {
                GM.newRow();
                GM[row_counter][0] = (double) i;
                GM[row_counter][1] = (double) j;
                GM[row_counter][2] = (double) RatingsMatrix[i][j];
                row_counter++;
            }
        }
    }
    
    
    G->train(GM);
    G->setRegularizer (MFreg);
    GClasses::GMatrix GP = *(G->getP());
    GClasses::GMatrix GQ = *(G->getQ());
    
    cout << "number of rows " << GP.rows() << endl;
    cout << "number of cols " << GP.cols() << endl;
    
    for (int i = 0;i < GP.rows();i++) {
        for (int j = 0;j < GP.cols();j++) {
            cout << "element (" << i << "," << j << ") is " << GP[i][j] << endl;
        }
    }
    

    Basically, declaring GP as a matrix pointer and accessing elements using *GP[i][j] does not work. On the other hand, declaring GP directly as a matrix and accessing elements using GP[i][j] works.

    Thanks,
    Harish

     
  • Mike Gashler

    Mike Gashler - 2013-04-09

    I am sorry I was not more help. In hindsight, now I think I understand what the problem was. "GP[i][j]" is the same as "(GP[i][j])", not (*GP[i])[j].

    So, if GP is a pointer to a matrix, then "GP[i][j]" evaluates GP as if it were a pointer to an array of matrices, so the whole term means "the jth row of the ith matrix".

    If you had replaced "GP[i][j]" with "(GP[i])[j]", that would fixed it too.

    Anyway, I am glad you got it to work.

    Mike

     
  • Mike Gashler

    Mike Gashler - 2013-04-09

    (Apparently the asterisk is a formatting character, because it removed most of them from my text. So, the previous comment only makes sense if you insert them before each GP.)

     

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.