コード例 #1
0
ファイル: KDDTrack2.cs プロジェクト: zenogantner/MML-KDD
    static void Main(string[] args)
    {
        Assembly assembly = Assembly.GetExecutingAssembly();

        Assembly.LoadFile(Path.GetDirectoryName(assembly.Location) + Path.DirectorySeparatorChar + "MyMediaLiteExperimental.dll");

        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Handlers.UnhandledExceptionHandler);
        Console.CancelKeyPress += new ConsoleCancelEventHandler(AbortHandler);

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

        // read command line parameters
        string method = args[0];

        RecommenderParameters parameters = null;

        try     { parameters = new RecommenderParameters(args, 1); }
        catch (ArgumentException e) { Usage(e.Message); }

        // arguments for iteration search
        find_iter  = parameters.GetRemoveInt32("find_iter", 0);
        max_iter   = parameters.GetRemoveInt32("max_iter", 500);
        epsilon    = parameters.GetRemoveDouble("epsilon", 1);
        err_cutoff = parameters.GetRemoveDouble("err_cutoff", 2);

        // data arguments
        string data_dir = parameters.GetRemoveString("data_dir");

        if (data_dir != string.Empty)
        {
            data_dir = data_dir + "/mml-track2";
        }
        else
        {
            data_dir = "mml-track2";
        }
        sample_data   = parameters.GetRemoveBool("sample_data", false);
        predict_rated = parameters.GetRemoveBool("predict_rated", false);
        predict_score = parameters.GetRemoveBool("predict_score", false);

        // other arguments
        save_model_file = parameters.GetRemoveString("save_model");
        load_model_file = parameters.GetRemoveString("load_model");
        int random_seed = parameters.GetRemoveInt32("random_seed", -1);

        prediction_file = parameters.GetRemoveString("prediction_file");

        if (predict_rated)
        {
            predict_score = true;
        }

        Console.Error.WriteLine("predict_score={0}", predict_score);

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

        recommender_validate = Recommender.CreateItemRecommender(method);
        if (recommender_validate == null)
        {
            Usage(string.Format("Unknown method: '{0}'", method));
        }

        Recommender.Configure(recommender_validate, parameters, Usage);
        recommender_final = recommender_validate.Clone() as ItemRecommender;

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

        // load all the data
        LoadData(data_dir);

        if (load_model_file != string.Empty)
        {
            Recommender.LoadModel(recommender_validate, load_model_file + "-validate");
            Recommender.LoadModel(recommender_final, load_model_file + "-final");
        }

        Console.Write(recommender_validate.ToString());

        DoTrack2();
    }
コード例 #2
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();
    }
コード例 #3
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");
        //Console.Error.WriteLine("data_dir " + data_dir);
        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);
        string prediction_file = parameters.GetRemoveString("prediction_file");

        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 "MF-ItemMapping":
            recommender = Recommender.Configure(mf_map, parameters, Usage);
            break;

//				case "MF-ItemMapping-Optimal":
//					recommender = Recommender.Configure(mf_map_opt, parameters, Usage);
//					break;
//				case "BPR-MF-ItemMapping-kNN":
//					recommender = Recommender.Configure(mf_map_knn, parameters, Usage);
//					break;
//				case "BPR-MF-ItemMapping-SVR":
//					recommender = Recommender.Configure(mf_map_svr, parameters, Usage);
//					break;
        default:
            Usage(string.Format("Unknown method: '{0}'", method));
            break;
        }

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

        // TODO move loading into its own method

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

        // training data
        training_data       = MyMediaLite.IO.RatingPrediction.Read(Path.Combine(data_dir, trainfile), user_mapping, item_mapping);
        recommender.Ratings = training_data;

        // 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 = MyMediaLite.IO.RatingPrediction.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();

        Console.Error.WriteLine(string.Format(CultureInfo.InvariantCulture, "ratings range: [{0}, {1}]", recommender.MinRating, recommender.MaxRating));

        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);
        }
        Console.WriteLine();

        if (prediction_file != string.Empty)
        {
            Console.WriteLine();
            seconds = Utils.MeasureTime(
                delegate() {
                Prediction.WritePredictions(recommender, test_data, user_mapping, item_mapping, prediction_file);
            }
                );
            Console.Error.WriteLine("predicting_time " + seconds);
        }
    }
コード例 #4
0
ファイル: KDDTrack2.cs プロジェクト: zenogantner/MML-KDD
    static void Main(string[] args)
    {
        Assembly assembly = Assembly.GetExecutingAssembly();
        Assembly.LoadFile(Path.GetDirectoryName(assembly.Location) + Path.DirectorySeparatorChar + "MyMediaLiteExperimental.dll");

        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Handlers.UnhandledExceptionHandler);
        Console.CancelKeyPress += new ConsoleCancelEventHandler(AbortHandler);

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

        // read command line parameters
        string method = args[0];

        RecommenderParameters parameters = null;
        try	{ parameters = new RecommenderParameters(args, 1); }
        catch (ArgumentException e) { Usage(e.Message);	}

        // arguments for iteration search
        find_iter   = parameters.GetRemoveInt32(  "find_iter",   0);
        max_iter    = parameters.GetRemoveInt32(  "max_iter",    500);
        epsilon     = parameters.GetRemoveDouble( "epsilon",     1);
        err_cutoff  = parameters.GetRemoveDouble( "err_cutoff",  2);

        // data arguments
        string data_dir  = parameters.GetRemoveString( "data_dir");
        if (data_dir != string.Empty)
            data_dir = data_dir + "/mml-track2";
        else
            data_dir = "mml-track2";
        sample_data      = parameters.GetRemoveBool(   "sample_data",   false);
        predict_rated    = parameters.GetRemoveBool(   "predict_rated", false);
        predict_score    = parameters.GetRemoveBool(   "predict_score", false);

        // other arguments
        save_model_file  = parameters.GetRemoveString( "save_model");
        load_model_file  = parameters.GetRemoveString( "load_model");
        int random_seed  = parameters.GetRemoveInt32(  "random_seed",  -1);
        prediction_file  = parameters.GetRemoveString( "prediction_file");

        if (predict_rated)
            predict_score = true;

        Console.Error.WriteLine("predict_score={0}", predict_score);

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

        recommender_validate = Recommender.CreateItemRecommender(method);
        if (recommender_validate == null)
            Usage(string.Format("Unknown method: '{0}'", method));

         		Recommender.Configure(recommender_validate, parameters, Usage);
        recommender_final = recommender_validate.Clone() as ItemRecommender;

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

        // load all the data
        LoadData(data_dir);

        if (load_model_file != string.Empty)
        {
            Recommender.LoadModel(recommender_validate, load_model_file + "-validate");
            Recommender.LoadModel(recommender_final,    load_model_file + "-final");
        }

        Console.Write(recommender_validate.ToString());

        DoTrack2();
    }
コード例 #5
0
    static void Main(string[] args)
    {
        Assembly assembly = Assembly.GetExecutingAssembly();

        Assembly.LoadFile(Path.GetDirectoryName(assembly.Location) + Path.DirectorySeparatorChar + "MyMediaLiteExperimental.dll");

        double min_rating = 0;
        double max_rating = 100;

        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Handlers.UnhandledExceptionHandler);
        Console.CancelKeyPress += new ConsoleCancelEventHandler(AbortHandler);

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

        // read command line parameters
        string method = args[0];

        RecommenderParameters parameters = null;

        try     { parameters = new RecommenderParameters(args, 1); }
        catch (ArgumentException e) { Usage(e.Message); }

        // arguments for iteration search
        find_iter   = parameters.GetRemoveInt32("find_iter", 0);
        max_iter    = parameters.GetRemoveInt32("max_iter", 500);
        compute_fit = parameters.GetRemoveBool("compute_fit", false);
        epsilon     = parameters.GetRemoveDouble("epsilon", 0);
        rmse_cutoff = parameters.GetRemoveDouble("rmse_cutoff", double.MaxValue);
        mae_cutoff  = parameters.GetRemoveDouble("mae_cutoff", double.MaxValue);

        // data arguments
        string data_dir = parameters.GetRemoveString("data_dir");

        track2 = parameters.GetRemoveBool("track2", false);
        if (data_dir != string.Empty)
        {
            data_dir = data_dir + (track2 ? "/mml-track2" : "/track1");
        }
        else
        {
            data_dir = track2 ? "/mml-track2" : "track1";
        }
        sample_data = parameters.GetRemoveBool("sample_data", false);

        // other arguments
        save_model_file = parameters.GetRemoveString("save_model");
        load_model_file = parameters.GetRemoveString("load_model");
        int random_seed = parameters.GetRemoveInt32("random_seed", -1);

        no_eval          = parameters.GetRemoveBool("no_eval", false);
        prediction_file  = parameters.GetRemoveString("prediction_file");
        cross_validation = parameters.GetRemoveInt32("cross_validation", 0);
        good_rating_prob = parameters.GetRemoveBool("good_rating_prob", false);

        if (good_rating_prob)
        {
            max_rating = 1;
        }

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

        recommender = Recommender.CreateRatingPredictor(method);
        if (recommender == null)
        {
            Usage(string.Format("Unknown method: '{0}'", method));
        }

        Recommender.Configure(recommender, parameters, Usage);

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

        // load all the data
        TimeSpan loading_time = Utils.MeasureTime(delegate() { LoadData(data_dir); });

        Console.Error.WriteLine(string.Format(CultureInfo.InvariantCulture, "loading_time {0,0:0.##}", loading_time.TotalSeconds));

        recommender.Ratings = training_ratings;

        recommender.MinRating = min_rating;
        recommender.MaxRating = max_rating;
        Console.Error.WriteLine(string.Format(CultureInfo.InvariantCulture, "ratings range: [{0}, {1}]", recommender.MinRating, recommender.MaxRating));

        if (load_model_file != string.Empty)
        {
            Recommender.LoadModel(recommender, load_model_file);
        }

        DoTrack1();

        Console.Error.WriteLine("memory {0}", Memory.Usage);
    }
コード例 #6
0
ファイル: KDDTrack1.cs プロジェクト: kinyue/MyMediaLite
    static void Main(string[] args)
    {
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Handlers.UnhandledExceptionHandler);
        Console.CancelKeyPress += new ConsoleCancelEventHandler(AbortHandler);

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

        // read command line parameters
        string method = args[0];

        RecommenderParameters parameters = null;
        try	{ parameters = new RecommenderParameters(args, 1); }
        catch (ArgumentException e) { Usage(e.Message);	}

        // arguments for iteration search
        find_iter   = parameters.GetRemoveInt32(  "find_iter",   0);
        max_iter    = parameters.GetRemoveInt32(  "max_iter",    500);
        compute_fit = parameters.GetRemoveBool(   "compute_fit", false);
        epsilon     = parameters.GetRemoveDouble( "epsilon",     0);
        rmse_cutoff = parameters.GetRemoveDouble( "rmse_cutoff", double.MaxValue);
        mae_cutoff  = parameters.GetRemoveDouble( "mae_cutoff",  double.MaxValue);

        // data arguments
        string data_dir  = parameters.GetRemoveString( "data_dir");
        track2           = parameters.GetRemoveBool(   "track2", false);
        if (data_dir != string.Empty)
            data_dir = data_dir + (track2 ? "/mml-track2" : "/track1");
        else
            data_dir = track2 ? "/mml-track2" : "track1";
        sample_data      = parameters.GetRemoveBool(   "sample_data", false);

        // other arguments
        save_model_file  = parameters.GetRemoveString( "save_model");
        load_model_file  = parameters.GetRemoveString( "load_model");
        int random_seed  = parameters.GetRemoveInt32(  "random_seed",      -1);
        no_eval          = parameters.GetRemoveBool(   "no_eval",          false);
        prediction_file  = parameters.GetRemoveString( "prediction_file");
        cross_validation = parameters.GetRemoveUInt32( "cross_validation", 0);
        good_rating_prob = parameters.GetRemoveBool(   "good_rating_prob", false);

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

        recommender = Recommender.CreateRatingPredictor(method);
        if (recommender == null)
            Usage(string.Format("Unknown method: '{0}'", method));

        Recommender.Configure(recommender, parameters, Usage);

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

        // load all the data
        TimeSpan loading_time = Wrap.MeasureTime(delegate() { LoadData(data_dir); });
        Console.Error.WriteLine(string.Format(CultureInfo.InvariantCulture, "loading_time {0,0:0.##}", loading_time.TotalSeconds));

        recommender.Ratings = training_ratings;

        Console.Error.WriteLine(string.Format(CultureInfo.InvariantCulture, "ratings range: [{0}, {1}]", recommender.MinRating, recommender.MaxRating));

        if (load_model_file != string.Empty)
            Model.Load(recommender, load_model_file);

        DoTrack1();

        Console.Error.WriteLine("memory {0}", Memory.Usage);
    }