Exemplo n.º 1
0
        public override Factorization Factorize()
        {
            log.Info("starting to compute the factorization...");
            Features features = new Features(this);

            /// feature maps necessary for solving for implicit feedback
            IDictionary <int, double[]> userY = null;
            IDictionary <int, double[]> itemY = null;

            if (usesImplicitFeedback)
            {
                userY = userFeaturesMapping(dataModel.GetUserIDs(), dataModel.GetNumUsers(), features.getU());
                itemY = itemFeaturesMapping(dataModel.GetItemIDs(), dataModel.GetNumItems(), features.getM());
            }

            IList <Task> tasks;

            for (int iteration = 0; iteration < numIterations; iteration++)
            {
                log.Info("iteration {0}", iteration);

                /// fix M - compute U
                tasks = new List <Task>();
                var userIDsIterator = dataModel.GetUserIDs();
                try {
                    ImplicitFeedbackAlternatingLeastSquaresSolver implicitFeedbackSolver = usesImplicitFeedback
            ? new ImplicitFeedbackAlternatingLeastSquaresSolver(numFeatures, lambda, alpha, itemY) : null;

                    while (userIDsIterator.MoveNext())
                    {
                        long             userID          = userIDsIterator.Current;
                        var              itemIDsFromUser = dataModel.GetItemIDsFromUser(userID).GetEnumerator();
                        IPreferenceArray userPrefs       = dataModel.GetPreferencesFromUser(userID);

                        tasks.Add(Task.Factory.StartNew(() => {
                            List <double[]> featureVectors = new List <double[]>();
                            while (itemIDsFromUser.MoveNext())
                            {
                                long itemID = itemIDsFromUser.Current;
                                featureVectors.Add(features.getItemFeatureColumn(itemIndex(itemID)));
                            }

                            var userFeatures = usesImplicitFeedback
                                          ? implicitFeedbackSolver.solve(sparseUserRatingVector(userPrefs))
                                          : AlternatingLeastSquaresSolver.solve(featureVectors, ratingVector(userPrefs), lambda, numFeatures);

                            features.setFeatureColumnInU(userIndex(userID), userFeatures);
                        }
                                                        ));
                    }
                } finally {
                    // queue.shutdown();
                    try {
                        Task.WaitAll(tasks.ToArray(), 1000 * dataModel.GetNumUsers());
                    } catch (AggregateException e) {
                        log.Warn("Error when computing user features", e);
                        throw e;
                    }
                }

                /// fix U - compute M
                //queue = createQueue();
                tasks = new List <Task>();

                var itemIDsIterator = dataModel.GetItemIDs();
                try {
                    ImplicitFeedbackAlternatingLeastSquaresSolver implicitFeedbackSolver = usesImplicitFeedback
            ? new ImplicitFeedbackAlternatingLeastSquaresSolver(numFeatures, lambda, alpha, userY) : null;

                    while (itemIDsIterator.MoveNext())
                    {
                        long             itemID    = itemIDsIterator.Current;
                        IPreferenceArray itemPrefs = dataModel.GetPreferencesForItem(itemID);

                        tasks.Add(Task.Factory.StartNew(() => {
                            var featureVectors = new List <double[]>();
                            foreach (IPreference pref in itemPrefs)
                            {
                                long userID = pref.GetUserID();
                                featureVectors.Add(features.getUserFeatureColumn(userIndex(userID)));
                            }

                            var itemFeatures = usesImplicitFeedback
                  ? implicitFeedbackSolver.solve(sparseItemRatingVector(itemPrefs))
                  : AlternatingLeastSquaresSolver.solve(featureVectors, ratingVector(itemPrefs), lambda, numFeatures);

                            features.setFeatureColumnInM(itemIndex(itemID), itemFeatures);
                        }));
                    }
                } finally {
                    try {
                        Task.WaitAll(tasks.ToArray(), 1000 * dataModel.GetNumItems());
                        //queue.awaitTermination(dataModel.getNumItems(), TimeUnit.SECONDS);
                    } catch (AggregateException e) {
                        log.Warn("Error when computing item features", e);
                        throw e;
                    }
                }
            }

            log.Info("finished computation of the factorization...");
            return(createFactorization(features.getU(), features.getM()));
        }
Exemplo n.º 2
0
        public override Factorization factorize()
        {
            log.info("starting to compute the factorization...", new object[0]);
            Features features                       = new Features(this);
            IDictionary <int, double[]> y           = null;
            IDictionary <int, double[]> dictionary2 = null;

            if (this.usesImplicitFeedback)
            {
                y           = this.userFeaturesMapping(this.dataModel.getUserIDs(), this.dataModel.getNumUsers(), features.getU());
                dictionary2 = this.itemFeaturesMapping(this.dataModel.getItemIDs(), this.dataModel.getNumItems(), features.getM());
            }
            for (int i = 0; i < this.numIterations; i++)
            {
                AggregateException exception;
                log.info("iteration {0}", new object[] { i });
                IList <Task>       source     = new List <Task>();
                IEnumerator <long> enumerator = this.dataModel.getUserIDs();
                try
                {
                    ImplicitFeedbackAlternatingLeastSquaresSolver implicitFeedbackSolver = this.usesImplicitFeedback ? new ImplicitFeedbackAlternatingLeastSquaresSolver(this.numFeatures, this.lambda, this.alpha, dictionary2) : null;
                    while (enumerator.MoveNext())
                    {
                        long userID = enumerator.Current;
                        IEnumerator <long> itemIDsFromUser = this.dataModel.getItemIDsFromUser(userID).GetEnumerator();
                        PreferenceArray    userPrefs       = this.dataModel.getPreferencesFromUser(userID);
                        source.Add(Task.Factory.StartNew(delegate
                        {
                            List <double[]> featureVectors = new List <double[]>();
                            while (itemIDsFromUser.MoveNext())
                            {
                                long current = itemIDsFromUser.Current;
                                featureVectors.Add(features.getItemFeatureColumn(this.itemIndex(current)));
                            }
                            double[] vector = this.usesImplicitFeedback ? implicitFeedbackSolver.solve(this.sparseUserRatingVector(userPrefs)) : AlternatingLeastSquaresSolver.solve(featureVectors, ratingVector(userPrefs), this.lambda, this.numFeatures);
                            features.setFeatureColumnInU(this.userIndex(userID), vector);
                        }));
                    }
                }
                finally
                {
                    try
                    {
                        Task.WaitAll(source.ToArray <Task>(), (int)(0x3e8 * this.dataModel.getNumUsers()));
                    }
                    catch (AggregateException exception1)
                    {
                        exception = exception1;
                        log.warn("Error when computing user features", new object[] { exception });
                        throw exception;
                    }
                }
                source = new List <Task>();
                IEnumerator <long> enumerator2 = this.dataModel.getItemIDs();
                try
                {
                    ImplicitFeedbackAlternatingLeastSquaresSolver implicitFeedbackSolver = this.usesImplicitFeedback ? new ImplicitFeedbackAlternatingLeastSquaresSolver(this.numFeatures, this.lambda, this.alpha, y) : null;
                    while (enumerator2.MoveNext())
                    {
                        long            itemID    = enumerator2.Current;
                        PreferenceArray itemPrefs = this.dataModel.getPreferencesForItem(itemID);
                        source.Add(Task.Factory.StartNew(delegate
                        {
                            List <double[]> featureVectors = new List <double[]>();
                            foreach (Preference preference in itemPrefs)
                            {
                                long num1 = preference.getUserID();
                                featureVectors.Add(features.getUserFeatureColumn(this.userIndex(num1)));
                            }
                            double[] vector = this.usesImplicitFeedback ? implicitFeedbackSolver.solve(this.sparseItemRatingVector(itemPrefs)) : AlternatingLeastSquaresSolver.solve(featureVectors, ratingVector(itemPrefs), this.lambda, this.numFeatures);
                            features.setFeatureColumnInM(this.itemIndex(itemID), vector);
                        }));
                    }
                }
                finally
                {
                    try
                    {
                        Task.WaitAll(source.ToArray <Task>(), (int)(0x3e8 * this.dataModel.getNumItems()));
                    }
                    catch (AggregateException exception2)
                    {
                        exception = exception2;
                        log.warn("Error when computing item features", new object[] { exception });
                        throw exception;
                    }
                }
            }
            log.info("finished computation of the factorization...", new object[0]);
            return(base.createFactorization(features.getU(), features.getM()));
        }