コード例 #1
0
        public static FactorizationMatrix newRandomMatrix(Users users, Items items, int K, double miu)
        {
            RandomGenerator           randomGenerator = new RandomGenerator();
            Dictionary <User, double> buVector        = randomGenerator.newRandomVector <Users, User>(users, 0.1, -0.05);
            Dictionary <Item, double> biVector        = randomGenerator.newRandomVector <Items, Item>(items, 0.1, -0.05);
            Matrix <User>             p     = randomGenerator.newRandomMatrix <Users, User>(users, 0.1, -0.05, K);
            Matrix <Item>             q     = randomGenerator.newRandomMatrix <Items, Item>(items, 0.1, -0.05, K);
            FactorizationMatrix       model = new FactorizationMatrix(miu, buVector, biVector, p, q);

            return(model);
        }
コード例 #2
0
        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));
        }