Simple matrix factorization class, learning is performed by stochastic gradient descent. More...
Public Member Functions | |
override void | AddRatings (IRatings ratings) |
Add new ratings and perform incremental training. | |
virtual bool | CanPredict (int user_id, int item_id) |
Check whether a useful prediction (i.e. not using a fallback/default answer) can be made for a given user-item combination. | |
Object | Clone () |
create a shallow copy of the object | |
virtual float | ComputeObjective () |
Compute the regularized loss. | |
virtual void | Iterate () |
Run one iteration (= pass over the training data). | |
override void | LoadModel (string filename) |
Get the model parameters from a file. | |
MatrixFactorization () | |
Default constructor. | |
override float | Predict (int user_id, int item_id) |
Predict the rating of a given user for a given item. | |
override void | RemoveItem (int item_id) |
Remove an item from the recommender model, and delete all ratings of this item. | |
override void | RemoveRatings (IDataSet ratings) |
Remove existing ratings and perform "incremental" training. | |
override void | RemoveUser (int user_id) |
Remove a user from the recommender model, and delete all their ratings. | |
virtual void | RetrainItem (int item_id) |
Updates the latent factors of an item. | |
virtual void | RetrainUser (int user_id) |
Updates the latent factors on a user. | |
override void | SaveModel (string filename) |
Save the model parameters to a file. | |
IList< Pair< int, float > > | ScoreItems (IList< Pair< int, float >> rated_items, IList< int > candidate_items) |
Rate a list of items given a list of ratings that represent a new user. | |
override string | ToString () |
Return a string representation of the recommender. | |
override void | Train () |
Learn the model parameters of the recommender from the training data. | |
override void | UpdateRatings (IRatings ratings) |
Update existing ratings and perform incremental training. | |
Protected Member Functions | |
override void | AddItem (int item_id) |
override void | AddUser (int user_id) |
virtual float[] | FoldIn (IList< Pair< int, float >> rated_items) |
Compute parameters (latent factors) for a user represented by ratings. | |
virtual void | InitModel () |
Initialize the model data structure. | |
virtual void | Iterate (IList< int > rating_indices, bool update_user, bool update_item) |
Iterate once over rating data and adjust corresponding factors (stochastic gradient descent). | |
virtual float | Predict (float[] user_vector, int item_id) |
Predict rating for a fold-in user and an item. | |
Protected Attributes | |
float | global_bias |
The bias (global average). | |
Matrix< float > | item_factors |
Matrix containing the latent item factors. | |
float | max_rating |
Maximum rating value. | |
float | min_rating |
Minimum rating value. | |
IRatings | ratings |
rating data | |
Matrix< float > | user_factors |
Matrix containing the latent user factors. | |
Properties | |
double | InitMean [get, set] |
Mean of the normal distribution used to initialize the factors. | |
double | InitStdDev [get, set] |
Standard deviation of the normal distribution used to initialize the factors. | |
float | LearnRate [get, set] |
Learn rate. | |
int | MaxItemID [get, set] |
Maximum item ID. | |
virtual float | MaxRating [get, set] |
Maximum rating value. | |
int | MaxUserID [get, set] |
Maximum user ID. | |
virtual float | MinRating [get, set] |
Minimum rating value. | |
uint | NumFactors [get, set] |
Number of latent factors. | |
uint | NumIter [get, set] |
Number of iterations over the training data. | |
virtual IRatings | Ratings [get, set] |
The rating data. | |
virtual float | Regularization [get, set] |
Regularization parameter. | |
bool | UpdateItems [get, set] |
true if items shall be updated when doing incremental updates | |
bool | UpdateUsers [get, set] |
true if users shall be updated when doing incremental updates |
Simple matrix factorization class, learning is performed by stochastic gradient descent.
Factorizing the observed rating values using a factor matrix for users and one for items.
NaN values in the model occur if values become too large or too small to be represented by the type float. If you encounter such problems, there are three ways to fix them: (1) (preferred) Use BiasedMatrixFactorization, which is more stable. (2) Change the range of rating values (1 to 5 works generally well with the default settings). (3) Change the learn_rate (decrease it if your range is larger than 1 to 5).
This recommender supports incremental updates.
MatrixFactorization | ( | ) | [inline] |
Default constructor.
override void AddRatings | ( | IRatings | ratings | ) | [inline, virtual] |
Add new ratings and perform incremental training.
ratings | the ratings |
Reimplemented from IncrementalRatingPredictor.
virtual bool CanPredict | ( | int | user_id, | |
int | item_id | |||
) | [inline, virtual, inherited] |
Check whether a useful prediction (i.e. not using a fallback/default answer) can be made for a given user-item combination.
It is up to the recommender implementor to decide when a prediction is useful, and to document it accordingly.
user_id | the user ID | |
item_id | the item ID |
Implements IRecommender.
Reimplemented in BiPolarSlopeOne, Constant, GlobalAverage, ItemAverage, Random, SlopeOne, and UserAverage.
Object Clone | ( | ) | [inline, inherited] |
create a shallow copy of the object
virtual float ComputeObjective | ( | ) | [inline, virtual] |
Compute the regularized loss.
Implements IIterativeModel.
Reimplemented in BiasedMatrixFactorization, and SocialMF.
virtual float [] FoldIn | ( | IList< Pair< int, float >> | rated_items | ) | [inline, protected, virtual] |
Compute parameters (latent factors) for a user represented by ratings.
rated_items | a list of (item ID, rating value) pairs |
Reimplemented in BiasedMatrixFactorization, SigmoidSVDPlusPlus, and SVDPlusPlus.
virtual void InitModel | ( | ) | [inline, protected, virtual] |
Initialize the model data structure.
Reimplemented in BiasedMatrixFactorization, SocialMF, and SVDPlusPlus.
virtual void Iterate | ( | IList< int > | rating_indices, | |
bool | update_user, | |||
bool | update_item | |||
) | [inline, protected, virtual] |
Iterate once over rating data and adjust corresponding factors (stochastic gradient descent).
rating_indices | a list of indices pointing to the ratings to iterate over | |
update_user | true if user factors to be updated | |
update_item | true if item factors to be updated |
Reimplemented in BiasedMatrixFactorization, SigmoidSVDPlusPlus, SocialMF, and SVDPlusPlus.
virtual void Iterate | ( | ) | [inline, virtual] |
Run one iteration (= pass over the training data).
Implements IIterativeModel.
Reimplemented in BiasedMatrixFactorization.
override void LoadModel | ( | string | filename | ) | [inline, virtual] |
Get the model parameters from a file.
filename | the name of the file to read from |
Reimplemented from RatingPredictor.
Reimplemented in BiasedMatrixFactorization, SigmoidSVDPlusPlus, and SVDPlusPlus.
override float Predict | ( | int | user_id, | |
int | item_id | |||
) | [inline, virtual] |
Predict the rating of a given user for a given item.
If the user or the item are not known to the recommender, the global average is returned. To avoid this behavior for unknown entities, use CanPredict() to check before.
user_id | the user ID | |
item_id | the item ID |
Implements RatingPredictor.
Reimplemented in BiasedMatrixFactorization, SigmoidSVDPlusPlus, and SVDPlusPlus.
virtual float Predict | ( | float[] | user_vector, | |
int | item_id | |||
) | [inline, protected, virtual] |
Predict rating for a fold-in user and an item.
user_vector | a float vector representing the user | |
item_id | the item ID |
Reimplemented in BiasedMatrixFactorization.
override void RemoveItem | ( | int | item_id | ) | [inline, virtual] |
Remove an item from the recommender model, and delete all ratings of this item.
It is up to the recommender implementor whether there should be model updates after this action, both options are valid.
item_id | the ID of the user to be removed |
Reimplemented from IncrementalRatingPredictor.
Reimplemented in BiasedMatrixFactorization.
override void RemoveRatings | ( | IDataSet | ratings | ) | [inline, virtual] |
Remove existing ratings and perform "incremental" training.
ratings | the user and item IDs of the ratings to be removed |
Reimplemented from IncrementalRatingPredictor.
override void RemoveUser | ( | int | user_id | ) | [inline, virtual] |
Remove a user from the recommender model, and delete all their ratings.
It is up to the recommender implementor whether there should be model updates after this action, both options are valid.
user_id | the ID of the user to be removed |
Reimplemented from IncrementalRatingPredictor.
Reimplemented in BiasedMatrixFactorization.
virtual void RetrainItem | ( | int | item_id | ) | [inline, virtual] |
Updates the latent factors of an item.
item_id | the item ID |
Reimplemented in BiasedMatrixFactorization.
virtual void RetrainUser | ( | int | user_id | ) | [inline, virtual] |
Updates the latent factors on a user.
user_id | the user ID |
Reimplemented in BiasedMatrixFactorization, and SVDPlusPlus.
override void SaveModel | ( | string | filename | ) | [inline, virtual] |
Save the model parameters to a file.
filename | the name of the file to write to |
Reimplemented from RatingPredictor.
Reimplemented in BiasedMatrixFactorization, and SVDPlusPlus.
IList<Pair<int, float> > ScoreItems | ( | IList< Pair< int, float >> | rated_items, | |
IList< int > | candidate_items | |||
) | [inline] |
Rate a list of items given a list of ratings that represent a new user.
rated_items | the ratings (item IDs and rating values) representing the new user | |
candidate_items | the items to be rated |
Implements IFoldInRatingPredictor.
override string ToString | ( | ) | [inline] |
Return a string representation of the recommender.
The ToString() method of recommenders should list the class name and all hyperparameters, separated by space characters.
Reimplemented from RatingPredictor.
Reimplemented in BiasedMatrixFactorization, SigmoidSVDPlusPlus, SocialMF, and SVDPlusPlus.
override void UpdateRatings | ( | IRatings | ratings | ) | [inline, virtual] |
Update existing ratings and perform incremental training.
ratings | the ratings |
Reimplemented from IncrementalRatingPredictor.
float global_bias [protected] |
The bias (global average).
Matrix<float> item_factors [protected] |
Matrix containing the latent item factors.
float max_rating [protected, inherited] |
Maximum rating value.
float min_rating [protected, inherited] |
Minimum rating value.
Matrix<float> user_factors [protected] |
Matrix containing the latent user factors.
double InitMean [get, set] |
Mean of the normal distribution used to initialize the factors.
double InitStdDev [get, set] |
Standard deviation of the normal distribution used to initialize the factors.
float LearnRate [get, set] |
Learn rate.
int MaxItemID [get, set, inherited] |
Maximum item ID.
virtual float MaxRating [get, set, inherited] |
Maximum rating value.
Implements IRatingPredictor.
int MaxUserID [get, set, inherited] |
Maximum user ID.
virtual float MinRating [get, set, inherited] |
Minimum rating value.
Implements IRatingPredictor.
uint NumFactors [get, set] |
Number of latent factors.
uint NumIter [get, set] |
Number of iterations over the training data.
Implements IIterativeModel.
The rating data.
Reimplemented in FactorWiseMatrixFactorization, ItemKNN, KNN, TimeAwareRatingPredictor, and UserKNN.
virtual float Regularization [get, set] |
Regularization parameter.
Reimplemented in BiasedMatrixFactorization.
bool UpdateItems [get, set, inherited] |
true if items shall be updated when doing incremental updates
Default should true. Set to false if you do not want any updates to the item model parameters when doing incremental updates.
Implements IIncrementalRatingPredictor.
bool UpdateUsers [get, set, inherited] |
true if users shall be updated when doing incremental updates
Default should be true. Set to false if you do not want any updates to the user model parameters when doing incremental updates.
Implements IIncrementalRatingPredictor.