GClasses
GClasses::GMatrixFactorization Class Reference

Detailed Description

This factors the sparse matrix of ratings, M, such that M = PQ^T where each row in P gives the principal preferences for the corresponding user, and each row in Q gives the linear combination of those preferences that map to a rating for an item. (Actually, P and Q also contain an extra column added for a bias.) This class is implemented according to the specification on page 631 in Takacs, G., Pilaszy, I., Nemeth, B., and Tikk, D. Scalable collaborative filtering approaches for large recommender systems. The Journal of Machine Learning Research, 10:623–656, 2009. ISSN 1532-4435., except with the addition of learning-rate decay and a different stopping criteria.

#include <GRecommender.h>

Inheritance diagram for GClasses::GMatrixFactorization:
GClasses::GCollaborativeFilter

Public Member Functions

 GMatrixFactorization (size_t intrinsicDims)
 General-purpose constructor. More...
 
 GMatrixFactorization (const GDomNode *pNode, GLearnerLoader &ll)
 Deserialization constructor. More...
 
virtual ~GMatrixFactorization ()
 Destructor. More...
 
void clampItemElement (size_t item, size_t attr, double val)
 Specify that a certain attribute of a certain item profile has a fixed value. (Values for attr are from 0 to m_intrinsicDims-1. No mechanism is provided to clamp the bias value.) More...
 
void clampItems (const GMatrix &data, size_t offset=0)
 Assumes that column 0 of data is an item ID, and all other columns specify profile values to clamp beginning at the specifed profile offset. More...
 
void clampUserElement (size_t user, size_t attr, double val)
 Specify that a certain attribute of a certain user profile has a fixed value. (Values for attr are from 0 to m_intrinsicDims-1. No mechanism is provided to clamp the bias value.) More...
 
void clampUsers (const GMatrix &data, size_t offset=0)
 Assumes that column 0 of data is a user ID, and all other columns specify profile values to clamp beginning at the specifed profile offset. More...
 
GMatrixdropP ()
 Returns the matrix of user preference vectors, and gives ownership to the caller. More...
 
GMatrixdropQ ()
 Returns the matrix of item weight vectors, and gives ownership to the caller. More...
 
GMatrixgetP ()
 Returns the matrix of user preference vectors. More...
 
GMatrixgetQ ()
 Returns the matrix of item weight vectors. More...
 
virtual void impute (GVec &vec, size_t dims)
 See the comment for GCollaborativeFilter::impute. More...
 
void nonNegative ()
 Constrain all non-bias weights to be non-negative during training. More...
 
virtual double predict (size_t user, size_t item)
 See the comment for GCollaborativeFilter::predict. More...
 
virtual GDomNodeserialize (GDom *pDoc) const
 See the comment for GCollaborativeFilter::serialize. More...
 
void setDecayRate (double d)
 Set the rate to decay the learning rate. More...
 
void setMinIters (size_t i)
 Set the min number of iterations to train. More...
 
void setRegularizer (double d)
 Set the regularization value. More...
 
virtual void train (GMatrix &data)
 See the comment for GCollaborativeFilter::train. More...
 
- Public Member Functions inherited from GClasses::GCollaborativeFilter
 GCollaborativeFilter ()
 
 GCollaborativeFilter (const GDomNode *pNode, GLearnerLoader &ll)
 
virtual ~GCollaborativeFilter ()
 
void basicTest (double minMSE)
 Performs a basic unit test on this collaborative filter. More...
 
double crossValidate (GMatrix &data, size_t folds, double *pOutMAE=NULL)
 This randomly assigns each rating to one of the folds. Then, for each fold, it calls train with a dataset that contains everything except for the ratings in that fold. It predicts values for the items in the fold, and returns the mean-squared difference between the predictions and the actual ratings. If pOutMAE is non-NULL, it will be set to the mean-absolute error. More...
 
GMatrixprecisionRecall (GMatrix &data, bool ideal=false)
 This divides the data into two equal-size parts. It trains on one part, and then measures the precision/recall using the other part. It returns a three-column data set with recall scores in column 0 and corresponding precision scores in column 1. The false-positive rate is in column 2. (So, if you want a precision-recall plot, just drop column 2. If you want an ROC curve, drop column 1 and swap the remaining two columns.) This method assumes the ratings range from 0 to 1, so be sure to scale the ratings to fit that range before calling this method. If ideal is true, then it will ignore your model and report the ideal results as if your model always predicted the correct rating. (This is useful because it shows the best possible results.) More...
 
GRandrand ()
 Returns a reference to the pseudo-random number generator associated with this object. More...
 
double trainAndTest (GMatrix &train, GMatrix &test, double *pOutMAE=NULL)
 This trains on the training set, and then tests on the test set. Returns the mean-squared difference between actual and target predictions. More...
 
void trainDenseMatrix (const GMatrix &data, const GMatrix *pLabels=NULL)
 Train from an m-by-n dense matrix, where m is the number of users and n is the number of items. All attributes must be continuous. Missing values are indicated with UNKNOWN_REAL_VALUE. If pLabels is non-NULL, then the labels will be appended as additional items. More...
 

Static Public Member Functions

static void test ()
 Performs unit tests. Throws if a failure occurs. Returns if successful. More...
 
- Static Public Member Functions inherited from GClasses::GCollaborativeFilter
static double areaUnderCurve (GMatrix &data)
 Pass in the data returned by the precisionRecall function (unmodified), and this will compute the area under the ROC curve. More...
 

Protected Member Functions

void clampP (size_t i)
 
void clampQ (size_t i)
 
double validate (GMatrix &data)
 Returns the sum-squared error for the specified set of ratings. More...
 
- Protected Member Functions inherited from GClasses::GCollaborativeFilter
GDomNodebaseDomNode (GDom *pDoc, const char *szClassName) const
 Child classes should use this in their implementation of serialize. More...
 

Protected Attributes

double m_decayRate
 
size_t m_intrinsicDims
 
size_t m_minIters
 
bool m_nonNeg
 
GMatrixm_pP
 
GMatrixm_pPMask
 
GMatrixm_pPWeights
 
GMatrixm_pQ
 
GMatrixm_pQMask
 
GMatrixm_pQWeights
 
double m_regularizer
 
- Protected Attributes inherited from GClasses::GCollaborativeFilter
GRand m_rand
 

Constructor & Destructor Documentation

GClasses::GMatrixFactorization::GMatrixFactorization ( size_t  intrinsicDims)

General-purpose constructor.

GClasses::GMatrixFactorization::GMatrixFactorization ( const GDomNode pNode,
GLearnerLoader ll 
)

Deserialization constructor.

virtual GClasses::GMatrixFactorization::~GMatrixFactorization ( )
virtual

Destructor.

Member Function Documentation

void GClasses::GMatrixFactorization::clampItemElement ( size_t  item,
size_t  attr,
double  val 
)

Specify that a certain attribute of a certain item profile has a fixed value. (Values for attr are from 0 to m_intrinsicDims-1. No mechanism is provided to clamp the bias value.)

void GClasses::GMatrixFactorization::clampItems ( const GMatrix data,
size_t  offset = 0 
)

Assumes that column 0 of data is an item ID, and all other columns specify profile values to clamp beginning at the specifed profile offset.

void GClasses::GMatrixFactorization::clampP ( size_t  i)
protected
void GClasses::GMatrixFactorization::clampQ ( size_t  i)
protected
void GClasses::GMatrixFactorization::clampUserElement ( size_t  user,
size_t  attr,
double  val 
)

Specify that a certain attribute of a certain user profile has a fixed value. (Values for attr are from 0 to m_intrinsicDims-1. No mechanism is provided to clamp the bias value.)

void GClasses::GMatrixFactorization::clampUsers ( const GMatrix data,
size_t  offset = 0 
)

Assumes that column 0 of data is a user ID, and all other columns specify profile values to clamp beginning at the specifed profile offset.

GMatrix* GClasses::GMatrixFactorization::dropP ( )
inline

Returns the matrix of user preference vectors, and gives ownership to the caller.

GMatrix* GClasses::GMatrixFactorization::dropQ ( )
inline

Returns the matrix of item weight vectors, and gives ownership to the caller.

GMatrix* GClasses::GMatrixFactorization::getP ( )
inline

Returns the matrix of user preference vectors.

GMatrix* GClasses::GMatrixFactorization::getQ ( )
inline

Returns the matrix of item weight vectors.

virtual void GClasses::GMatrixFactorization::impute ( GVec vec,
size_t  dims 
)
virtual
void GClasses::GMatrixFactorization::nonNegative ( )
inline

Constrain all non-bias weights to be non-negative during training.

virtual double GClasses::GMatrixFactorization::predict ( size_t  user,
size_t  item 
)
virtual
virtual GDomNode* GClasses::GMatrixFactorization::serialize ( GDom pDoc) const
virtual
void GClasses::GMatrixFactorization::setDecayRate ( double  d)
inline

Set the rate to decay the learning rate.

void GClasses::GMatrixFactorization::setMinIters ( size_t  i)
inline

Set the min number of iterations to train.

void GClasses::GMatrixFactorization::setRegularizer ( double  d)
inline

Set the regularization value.

static void GClasses::GMatrixFactorization::test ( )
static

Performs unit tests. Throws if a failure occurs. Returns if successful.

virtual void GClasses::GMatrixFactorization::train ( GMatrix data)
virtual
double GClasses::GMatrixFactorization::validate ( GMatrix data)
protected

Returns the sum-squared error for the specified set of ratings.

Member Data Documentation

double GClasses::GMatrixFactorization::m_decayRate
protected
size_t GClasses::GMatrixFactorization::m_intrinsicDims
protected
size_t GClasses::GMatrixFactorization::m_minIters
protected
bool GClasses::GMatrixFactorization::m_nonNeg
protected
GMatrix* GClasses::GMatrixFactorization::m_pP
protected
GMatrix* GClasses::GMatrixFactorization::m_pPMask
protected
GMatrix* GClasses::GMatrixFactorization::m_pPWeights
protected
GMatrix* GClasses::GMatrixFactorization::m_pQ
protected
GMatrix* GClasses::GMatrixFactorization::m_pQMask
protected
GMatrix* GClasses::GMatrixFactorization::m_pQWeights
protected
double GClasses::GMatrixFactorization::m_regularizer
protected