|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.mymedialite.ratingprediction.RatingPredictor
org.mymedialite.ratingprediction.IncrementalRatingPredictor
org.mymedialite.ratingprediction.MatrixFactorization
public class MatrixFactorization
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 double. 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.
Field Summary | |
---|---|
protected double |
globalBias
The bias (global average). |
double |
initMean
Mean of the normal distribution used to initialize the factors. |
double |
initStDev
Standard deviation of the normal distribution used to initialize the factors. |
protected Matrix<java.lang.Double> |
itemFactors
Matrix containing the latent item factors. |
double |
learnRate
Learn rate. |
int |
numFactors
Number of latent factors. |
int |
numIter
Number of iterations over the training data. |
double |
regularization
Regularization parameter. |
protected Matrix<java.lang.Double> |
userFactors
Matrix containing the latent user factors. |
Fields inherited from class org.mymedialite.ratingprediction.IncrementalRatingPredictor |
---|
updateItems, updateUsers |
Fields inherited from class org.mymedialite.ratingprediction.RatingPredictor |
---|
maxItemID, maxRating, maxUserID, minRating, ratings |
Constructor Summary | |
---|---|
MatrixFactorization()
Default constructor. |
Method Summary | |
---|---|
void |
addItem(int item_id)
|
void |
addRating(int user_id,
int item_id,
double rating)
Add a new rating and perform incremental training |
void |
addUser(int user_id)
|
double |
computeLoss()
Compute the regularized loss. |
int |
getNumIter()
Get the number of iterations to run the training |
protected void |
initModel()
Initialize the model data structure. |
void |
iterate()
Run one iteration (= pass over the training data) |
protected void |
iterate(java.util.List<java.lang.Integer> rating_indices,
boolean update_user,
boolean update_item)
Iterate once over rating data and adjust corresponding factors (stochastic gradient descent). |
void |
loadModel(java.lang.String filename)
Get the model parameters from a file |
double |
predict(int user_id,
int item_id)
Predict the rating of a given user for a given item. |
protected double |
predict(int user_id,
int item_id,
boolean bound)
|
void |
removeItem(int item_id)
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. |
void |
removeRating(int user_id,
int item_id)
Remove an existing rating and perform "incremental" training |
void |
removeUser(int user_id)
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. |
void |
retrainItem(int item_id)
Updates the latent factors of an item. |
void |
retrainUser(int user_id)
Updates the latent factors on a user. |
void |
saveModel(java.lang.String filename)
Save the model parameters to a file |
void |
setNumIter(int num_iter)
Set the number of iterations to run the training |
java.lang.String |
toString()
Return a string representation of the recommender |
void |
train()
Learn the model parameters of the recommender from the training data |
void |
updateRating(int user_id,
int item_id,
double rating)
Update an existing rating and perform incremental training |
Methods inherited from class org.mymedialite.ratingprediction.IncrementalRatingPredictor |
---|
getUpdateItems, getUpdateUsers, setUpdateItems, setUpdateUsers |
Methods inherited from class org.mymedialite.ratingprediction.RatingPredictor |
---|
canPredict, clone, getMaxRating, getMinRating, getRatings, setMaxRating, setMinRating, setRatings |
Methods inherited from class java.lang.Object |
---|
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface org.mymedialite.ratingprediction.IRatingPredictor |
---|
getMaxRating, getMinRating, setMaxRating, setMinRating |
Methods inherited from interface org.mymedialite.IRecommender |
---|
canPredict |
Field Detail |
---|
protected Matrix<java.lang.Double> userFactors
protected Matrix<java.lang.Double> itemFactors
protected double globalBias
public double initMean
public double initStDev
public int numFactors
public double learnRate
public double regularization
public int numIter
Constructor Detail |
---|
public MatrixFactorization()
Method Detail |
---|
public void setNumIter(int num_iter)
IIterativeModel
setNumIter
in interface IIterativeModel
public int getNumIter()
IIterativeModel
getNumIter
in interface IIterativeModel
protected void initModel()
public void train()
IRecommender
train
in interface IRecommender
train
in class RatingPredictor
public void iterate()
IIterativeModel
iterate
in interface IIterativeModel
public void retrainUser(int user_id)
user_id
- the user IDpublic void retrainItem(int item_id)
item_id
- the item IDprotected void iterate(java.util.List<java.lang.Integer> rating_indices, boolean update_user, boolean update_item)
rating_indices
- a list of indices pointing to the ratings to iterate overupdate_user
- true if user factors to be updatedupdate_item
- true if item factors to be updatedprotected double predict(int user_id, int item_id, boolean bound)
public double predict(int user_id, int item_id)
predict
in interface IRecommender
predict
in class RatingPredictor
user_id
- the user IDitem_id
- the item ID
public void addRating(int user_id, int item_id, double rating)
IIncrementalRatingPredictor
addRating
in interface IIncrementalRatingPredictor
addRating
in class IncrementalRatingPredictor
user_id
- the ID of the user who performed the ratingitem_id
- the ID of the rated itemrating
- the rating valuepublic void updateRating(int user_id, int item_id, double rating)
IIncrementalRatingPredictor
updateRating
in interface IIncrementalRatingPredictor
updateRating
in class IncrementalRatingPredictor
user_id
- the ID of the user who performed the ratingitem_id
- the ID of the rated itemrating
- the rating valuepublic void removeRating(int user_id, int item_id)
IIncrementalRatingPredictor
removeRating
in interface IIncrementalRatingPredictor
removeRating
in class IncrementalRatingPredictor
user_id
- the ID of the user who performed the ratingitem_id
- the ID of the rated itempublic void addUser(int user_id)
addUser
in class IncrementalRatingPredictor
public void addItem(int item_id)
addItem
in class IncrementalRatingPredictor
public void removeUser(int user_id)
IIncrementalRatingPredictor
removeUser
in interface IIncrementalRatingPredictor
removeUser
in class IncrementalRatingPredictor
user_id
- the ID of the user to be removedpublic void removeItem(int item_id)
IIncrementalRatingPredictor
removeItem
in interface IIncrementalRatingPredictor
removeItem
in class IncrementalRatingPredictor
item_id
- the ID of the user to be removedpublic void saveModel(java.lang.String filename) throws java.io.IOException
IRecommender
saveModel
in interface IRecommender
saveModel
in class RatingPredictor
filename
- the file to write to
java.io.IOException
public void loadModel(java.lang.String filename) throws java.io.IOException
IRecommender
loadModel
in interface IRecommender
loadModel
in class RatingPredictor
filename
- the file to read from
java.io.IOException
public double computeLoss()
computeLoss
in interface IIterativeModel
public java.lang.String toString()
IRecommender
toString
in interface IRecommender
toString
in class RatingPredictor
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |