public override void Train(Split split)
        {
            var mmlInstance = (FM)MmlRecommenderInstance;
            var featBuilder = new FmFeatureBuilder();

            var wFm = MmlRecommenderInstance as WeightedBPRFM;

            if (DataType == WrapRec.IO.DataType.Ratings)
            {
                var mmlFeedback = new Ratings();
                foreach (var feedback in split.Train)
                {
                    var rating = (Rating)feedback;
                    mmlFeedback.Add(UsersMap.ToInternalID(rating.User.Id), ItemsMap.ToInternalID(rating.Item.Id), rating.Value);

                    // the attributes are translated so that they can be used later for training
                    foreach (var attr in feedback.GetAllAttributes())
                    {
                        attr.Translation = featBuilder.TranslateAttribute(attr);
                        // hard code attribute group. User is 0, item is 1, others is 2
                        attr.Group = 2;
                        if (wFm != null && !wFm.FeatureGroups.ContainsKey(attr.Translation.Item1))
                        {
                            wFm.FeatureGroups.Add(attr.Translation.Item1, 2);
                        }
                    }
                }
                ((IRatingPredictor)MmlRecommenderInstance).Ratings = mmlFeedback;
            }

            foreach (var feedback in split.Test)
            {
                // the attributes are translated so that they can be used later for training
                foreach (var attr in feedback.GetAllAttributes())
                {
                    attr.Translation = featBuilder.TranslateAttribute(attr);
                    // hard code attribute group. User is 0, item is 1, others is 2
                    attr.Group = 2;
                    if (wFm != null && !wFm.FeatureGroups.ContainsKey(attr.Translation.Item1))
                    {
                        wFm.FeatureGroups.Add(attr.Translation.Item1, 2);
                    }
                }
            }

            mmlInstance.Split          = split;
            mmlInstance.Model          = this;
            mmlInstance.UsersMap       = UsersMap;
            mmlInstance.ItemsMap       = ItemsMap;
            mmlInstance.FeatureBuilder = featBuilder;

            Logger.Current.Trace("Training with MmlFmRecommender recommender...");
            PureTrainTime = (int)Wrap.MeasureTime(delegate() { mmlInstance.Train(); }).TotalMilliseconds;
        }
Exemple #2
0
        public override void Train(Split split)
        {
            var mmlInstance = (BPRFM)MmlRecommenderInstance;
            var featBuilder = new FmFeatureBuilder();
            var mmlFeedback = new PosOnlyFeedback <SparseBooleanMatrix>();

            var wBprFm = MmlRecommenderInstance as WeightedBPRFM;

            foreach (var feedback in split.Train)
            {
                mmlFeedback.Add(UsersMap.ToInternalID(feedback.User.Id), ItemsMap.ToInternalID(feedback.Item.Id));

                // the attributes are translated so that they can be used later for training
                foreach (var attr in feedback.GetAllAttributes())
                {
                    attr.Translation = featBuilder.TranslateAttribute(attr);
                    // hard code attribute group. User is 0, item is 1, others is 2
                    attr.Group = 2;
                    if (wBprFm != null && !wBprFm.FeatureGroups.ContainsKey(attr.Translation.Item1))
                    {
                        wBprFm.FeatureGroups.Add(attr.Translation.Item1, 2);
                    }
                }
            }

            foreach (var feedback in split.Test)
            {
                // the attributes are translated so that they can be used later for training
                foreach (var attr in feedback.GetAllAttributes())
                {
                    attr.Translation = featBuilder.TranslateAttribute(attr);
                    // hard code attribute group. User is 0, item is 1, others is 2
                    attr.Group = 2;
                    if (wBprFm != null && !wBprFm.FeatureGroups.ContainsKey(attr.Translation.Item1))
                    {
                        wBprFm?.FeatureGroups.Add(attr.Translation.Item1, 2);
                    }
                }
            }

            mmlInstance.Feedback       = mmlFeedback;
            mmlInstance.Split          = split;
            mmlInstance.Model          = this;
            mmlInstance.UsersMap       = UsersMap;
            mmlInstance.ItemsMap       = ItemsMap;
            mmlInstance.FeatureBuilder = featBuilder;

            Logger.Current.Trace("Training with MmlBprfmRecommender recommender...");
            PureTrainTime = (int)Wrap.MeasureTime(delegate() { mmlInstance.Train(); }).TotalMilliseconds;
        }