Base class for BPR-MF plus attribute-to-factor mapping
Literature: Zeno Gantner, Lucas Drumond, Christoph Freudenthaler, Steffen Rendle, Lars Schmidt-Thieme: Learning Attribute-to-Feature Mappings for Cold-Start Recommendations. ICDM 2011. http://www.ismll.uni-hildesheim.de/pub/pdfs/Gantner_et_al2010Mapping.pdf
Наследование: MyMediaLite.ItemRecommendation.BPRMF
Пример #1
0
    public static void Main(string[] args)
    {
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Handlers.UnhandledExceptionHandler);

        // check number of command line parameters
        if (args.Length < 4)
            Usage("Not enough arguments.");

        // read command line parameters
        RecommenderParameters parameters = null;
        try	{ parameters = new RecommenderParameters(args, 4);	}
        catch (ArgumentException e)	{ Usage(e.Message); 		}

        // other parameters
        string data_dir             = parameters.GetRemoveString( "data_dir");
        string relevant_items_file  = parameters.GetRemoveString( "relevant_items");
        string item_attributes_file = parameters.GetRemoveString( "item_attributes");
        string user_attributes_file = parameters.GetRemoveString( "user_attributes");
        //string save_mapping_file    = parameters.GetRemoveString( "save_model");
        int random_seed             = parameters.GetRemoveInt32(  "random_seed", -1);
        bool no_eval                = parameters.GetRemoveBool(   "no_eval", false);
        bool compute_fit            = parameters.GetRemoveBool(   "compute_fit", false);

        if (random_seed != -1)
            MyMediaLite.Util.Random.InitInstance(random_seed);

        // main data files and method
        string trainfile = args[0].Equals("-") ? "-" : Path.Combine(data_dir, args[0]);
        string testfile  = args[1].Equals("-") ? "-" : Path.Combine(data_dir, args[1]);
        string load_model_file = args[2];
        string method    = args[3];

        // set correct recommender
        switch (method)
        {
            case "BPR-MF-ItemMapping":
                recommender = Recommender.Configure(bprmf_map, parameters, Usage);
                break;
            case "BPR-MF-ItemMapping-Optimal":
                recommender = Recommender.Configure(bprmf_map_bpr, parameters, Usage);
                break;
            case "BPR-MF-ItemMapping-Complex":
                recommender = Recommender.Configure(bprmf_map_com, parameters, Usage);
                break;
            case "BPR-MF-ItemMapping-kNN":
                recommender = Recommender.Configure(bprmf_map_knn, parameters, Usage);
                break;
            case "BPR-MF-ItemMapping-SVR":
                recommender = Recommender.Configure(bprmf_map_svr, parameters, Usage);
                break;
            case "BPR-MF-UserMapping":
                recommender = Recommender.Configure(bprmf_user_map, parameters, Usage);
                break;
            case "BPR-MF-UserMapping-Optimal":
                recommender = Recommender.Configure(bprmf_user_map_bpr, parameters, Usage);
                break;
            default:
                Usage(string.Format("Unknown method: '{0}'", method));
                break;
        }

        if (parameters.CheckForLeftovers())
            Usage(-1);

        // ID mapping objects
        var user_mapping = new EntityMapping();
        var item_mapping = new EntityMapping();

        // training data
        training_data = ItemRecommendation.Read(Path.Combine(data_dir, trainfile), user_mapping, item_mapping);
        recommender.Feedback = training_data;

        // relevant items
        if (! relevant_items_file.Equals(string.Empty) )
            relevant_items = new HashSet<int>(item_mapping.ToInternalID(Utils.ReadIntegers(Path.Combine(data_dir, relevant_items_file))));
        else
            relevant_items = training_data.AllItems;

        // user attributes
        if (recommender is IUserAttributeAwareRecommender)
        {
            if (user_attributes_file.Equals(string.Empty))
                Usage("Recommender expects user_attributes=FILE.");
            else
                ((IUserAttributeAwareRecommender)recommender).UserAttributes = AttributeData.Read(Path.Combine(data_dir, user_attributes_file), user_mapping);
        }

        // item attributes
        if (recommender is IItemAttributeAwareRecommender)
        {
            if (item_attributes_file.Equals(string.Empty))
                Usage("Recommender expects item_attributes=FILE.");
            else
                ((IItemAttributeAwareRecommender)recommender).ItemAttributes = AttributeData.Read(Path.Combine(data_dir, item_attributes_file), item_mapping);
        }

        // test data
        test_data = ItemRecommendation.Read( Path.Combine(data_dir, testfile), user_mapping, item_mapping );

        TimeSpan seconds;

        Recommender.LoadModel(recommender, load_model_file);

        // set the maximum user and item IDs in the recommender - this is important for the cold start use case
        recommender.MaxUserID = user_mapping.InternalIDs.Max();
        recommender.MaxItemID = item_mapping.InternalIDs.Max();

        DisplayDataStats();

        Console.Write(recommender.ToString() + " ");

        if (compute_fit)
        {
            seconds = Utils.MeasureTime( delegate() {
                int num_iter = recommender.NumIterMapping;
                recommender.NumIterMapping = 0;
                recommender.LearnAttributeToFactorMapping();
                Console.Error.WriteLine();
                Console.Error.WriteLine(string.Format(CultureInfo.InvariantCulture, "iteration {0} fit {1}", -1, recommender.ComputeFit()));

                recommender.NumIterMapping = 1;
                for (int i = 0; i < num_iter; i++, i++)
                {
                    recommender.IterateMapping();
                    Console.Error.WriteLine(string.Format(CultureInfo.InvariantCulture, "iteration {0} fit {1}", i, recommender.ComputeFit()));
                }
                recommender.NumIterMapping = num_iter; // restore
            } );
        }
        else
        {
            seconds = Utils.MeasureTime( delegate() {
                recommender.LearnAttributeToFactorMapping();
            } );
        }
        Console.Write("mapping_time " + seconds + " ");

        if (!no_eval)
            seconds = EvaluateRecommender(recommender, test_data, training_data);
        Console.WriteLine();
    }
Пример #2
0
    static TimeSpan EvaluateRecommender(BPRMF_Mapping recommender, IPosOnlyFeedback test_data, IPosOnlyFeedback train_data)
    {
        Console.Error.WriteLine(string.Format(CultureInfo.InvariantCulture, "fit {0}", recommender.ComputeFit()));

        TimeSpan seconds = Utils.MeasureTime( delegate()
            {
                var result = Items.Evaluate(
                                recommender,
                                test_data,
                                train_data,
                                test_data.AllUsers,
                                relevant_items
                );
                DisplayResults(result);
            } );
        Console.Write(" testing " + seconds);

        return seconds;
    }