public void Train() { Console.WriteLine("***************** Train Matrix Factorization Model *********************"); mf = FactorizationMatrix.newRandomMatrix(trainUsers, trainItems, cFeatures, miu); double error; double actualRating; double predictedRating; double prevRmse = Double.MaxValue; EvaluationEngine evaluationEngine = new EvaluationEngine(miu); double currRmse = evaluationEngine.computeRMSE(validationUsers, validationItems, this); int iteration = 1; while (prevRmse > currRmse) //TODO Eyal - add num iteration and improvment threshold conditions { Console.WriteLine("Iteraion: {0}, Current RMSE: {1}", iteration, currRmse); foreach (User user in trainUsers) { foreach (string itemId in user.GetRatedItems()) { Item item = trainItems.GetItemById(itemId); actualRating = user.GetRating(item.GetId()); predictedRating = mf.Predict(user, item); error = actualRating - predictedRating; var userBias = mf.getBu(user); mf.setBu(user, userBias + yRate * (error - lambdaRate * userBias)); var itemBias = mf.getBi(item); mf.setBi(item, itemBias + yRate * (error - lambdaRate * itemBias)); //update Qi and Pu mf.UpdateQiAndPu(item, user, error); } } prevRmse = currRmse; currRmse = evaluationEngine.computeRMSE(validationUsers, validationItems, this); iteration++; } Console.WriteLine(String.Format("Final Result - RMSE = {0}", prevRmse)); }
public double Predict(User user, Item item) { return(mf.Predict(user, item)); }