-
Notifications
You must be signed in to change notification settings - Fork 1.9k
AutoML Add Recommendation Task #4246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
maryamariyan
merged 33 commits into
dotnet:master
from
LittleLittleCloud:u/xiaoyun/recommendation
Oct 17, 2019
Merged
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
6f3d26c
[AutoML] Pull out Code Gen as separate library plus some changes in C…
LittleLittleCloud 7e0f6d0
pack codegen into mlnet
LittleLittleCloud 22edabb
pack codegen into mlnet (#4179)
LittleLittleCloud 50e0dcd
Merge branch 'features/automl' of https://github.com/dotnet/machinele…
LittleLittleCloud 09c56f7
add MatrixFactorization Trainer
LittleLittleCloud 15c58f1
add RecommendationExperiment and other functions
LittleLittleCloud ac57d9a
some refactor in MatrixFactorization, plus fix small bugs
LittleLittleCloud c07948f
add LabelFeautre ColumnPurpose and some update
LittleLittleCloud f182a20
Merge branch 'u/xiaoyun/recommendation'
LittleLittleCloud 9695ffe
add missing Native dll
LittleLittleCloud b54de14
remove mlnet project
LittleLittleCloud 913b4af
update based on comment
LittleLittleCloud 3fc520c
update example
LittleLittleCloud 2f47c02
Merge branch 'master' into u/xiaoyun/recommendation
maryamariyan c78efbf
nit: code style
maryamariyan 5864b78
- Rename RecommendationExperimentScenario.MF to RecommendationExperim…
maryamariyan 4010d90
nit: code style/ add space between if and (
maryamariyan fef926e
Fix compile error
maryamariyan 9c4852c
minor fixes
maryamariyan 74cbc5c
First stage changes
maryamariyan 7e7c272
change signature for ITrainerEstimator
maryamariyan 17500cf
Adding tests, checking code coverage
maryamariyan b882ee1
cleanup + improve SweepParams, taken from MatrixFactorizationTrainer
maryamariyan d7a272d
Address PR feedback - part1
maryamariyan b69d9c3
Apply PR feedbacks - Part 2
maryamariyan f9c6abb
Update test to reflect change made to sweep params
maryamariyan 7d856c8
Apply PR feedbacks: Part 3
maryamariyan 7852c5e
Adds more sweepable params and test
maryamariyan f889fa5
Rename to UserId/ItemId
maryamariyan 2ec0649
Rename User/Item ID: part 2
maryamariyan c39ae94
- Removing SamplingKey for first iteration
maryamariyan 7186280
Apply review comments
maryamariyan d3d6b4a
Minor rename
maryamariyan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add RecommendationExperiment and other functions
- Loading branch information
commit 15c58f1c466936d8824aaf77696c0f61eb2a416e
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
docs/samples/Microsoft.ML.AutoML.Samples/RecommendationExperiment.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using Microsoft.ML.AutoML; | ||
using Microsoft.ML.Data; | ||
|
||
namespace Microsoft.ML.AutoML.Samples | ||
{ | ||
public static class RecommendationExperiment | ||
{ | ||
private static string TrainDataPath = @"C:\Users\xiaoyuz\Desktop\machinelearning-samples\datasets\recommendation-ratings-train.csv"; | ||
private static string TestDataPath = @"C:\Users\xiaoyuz\Desktop\machinelearning-samples\datasets\recommendation-ratings-test.csv"; | ||
private static string ModelPath = @"C:\Users\xiaoyuz\source\test\recommendation.zip"; | ||
private static string LabelColumnName = "rating"; | ||
private static uint ExperimentTime = 60; | ||
|
||
public static void Run() | ||
{ | ||
MLContext mlContext = new MLContext(); | ||
|
||
// STEP 1: Load data | ||
IDataView trainDataView = mlContext.Data.LoadFromTextFile<TaxiTrip>(TrainDataPath, hasHeader: true, separatorChar: ','); | ||
IDataView testDataView = mlContext.Data.LoadFromTextFile<TaxiTrip>(TestDataPath, hasHeader: true, separatorChar: ','); | ||
|
||
var settings = new RecommendationExperimentSettings(RecommendationExperimentScenario.MF, "userId", "movieId"); | ||
// STEP 2: Run AutoML experiment | ||
Console.WriteLine($"Running AutoML regression experiment for {ExperimentTime} seconds..."); | ||
ExperimentResult<RegressionMetrics> experimentResult = mlContext.Auto() | ||
.CreateRecommendationExperiment(settings) | ||
.Execute(trainDataView, LabelColumnName); | ||
|
||
// STEP 3: Print metric from best model | ||
RunDetail<RegressionMetrics> bestRun = experimentResult.BestRun; | ||
maryamariyan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Console.WriteLine($"Total models produced: {experimentResult.RunDetails.Count()}"); | ||
Console.WriteLine($"Best model's trainer: {bestRun.TrainerName}"); | ||
Console.WriteLine($"Metrics of best model from validation data --"); | ||
PrintMetrics(bestRun.ValidationMetrics); | ||
|
||
// STEP 5: Evaluate test data | ||
IDataView testDataViewWithBestScore = bestRun.Model.Transform(testDataView); | ||
RegressionMetrics testMetrics = mlContext.Regression.Evaluate(testDataViewWithBestScore, labelColumnName: LabelColumnName); | ||
maryamariyan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Console.WriteLine($"Metrics of best model on test data --"); | ||
PrintMetrics(testMetrics); | ||
|
||
// STEP 6: Save the best model for later deployment and inferencing | ||
using (FileStream fs = File.Create(ModelPath)) | ||
maryamariyan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
mlContext.Model.Save(bestRun.Model, trainDataView.Schema, fs); | ||
|
||
// STEP 7: Create prediction engine from the best trained model | ||
var predictionEngine = mlContext.Model.CreatePredictionEngine<TaxiTrip, TaxiTripFarePrediction>(bestRun.Model); | ||
|
||
// STEP 8: Initialize a new test taxi trip, and get the predicted fare | ||
var testTaxiTrip = new TaxiTrip | ||
{ | ||
VendorId = "VTS", | ||
RateCode = 1, | ||
PassengerCount = 1, | ||
TripTimeInSeconds = 1140, | ||
TripDistance = 3.75f, | ||
PaymentType = "CRD" | ||
}; | ||
var prediction = predictionEngine.Predict(testTaxiTrip); | ||
Console.WriteLine($"Predicted fare for test taxi trip: {prediction.FareAmount}"); | ||
|
||
Console.WriteLine("Press any key to continue..."); | ||
Console.ReadKey(); | ||
} | ||
|
||
private static void PrintMetrics(RegressionMetrics metrics) | ||
{ | ||
Console.WriteLine($"MeanAbsoluteError: {metrics.MeanAbsoluteError}"); | ||
Console.WriteLine($"MeanSquaredError: {metrics.MeanSquaredError}"); | ||
Console.WriteLine($"RootMeanSquaredError: {metrics.RootMeanSquaredError}"); | ||
Console.WriteLine($"RSquared: {metrics.RSquared}"); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using Microsoft.ML.Data; | ||
using Microsoft.ML.Trainers; | ||
|
||
namespace Microsoft.ML.AutoML | ||
{ | ||
public enum RecommendationExperimentScenario | ||
{ | ||
MF, | ||
maryamariyan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public sealed class RecommendationExperimentSettings : ExperimentSettings | ||
maryamariyan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
public RecommendationExperimentScenario Scenerio { get; set; } | ||
maryamariyan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public string MatrixColumnIndexColumnName { get; set; } | ||
|
||
public string MatrixRowIndexColumnName { get; set; } | ||
|
||
// We can use RegressionMetric as evaluation Metric | ||
public RegressionMetric OptimizingMetric { get; set; } | ||
|
||
public ICollection<RecommendationTrainer> Trainers { get; } | ||
|
||
public RecommendationExperimentSettings(RecommendationExperimentScenario scenario, string columnIndexName, string rowIndexName) | ||
: this() | ||
{ | ||
if(scenario == RecommendationExperimentScenario.MF) | ||
maryamariyan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
AutoCatalog.ValuePairs[nameof(MatrixFactorizationTrainer.Options.MatrixColumnIndexColumnName)] = columnIndexName; | ||
AutoCatalog.ValuePairs[nameof(MatrixFactorizationTrainer.Options.MatrixRowIndexColumnName)] = rowIndexName; | ||
return; | ||
} | ||
throw new NotImplementedException(); | ||
} | ||
|
||
private RecommendationExperimentSettings() | ||
{ | ||
OptimizingMetric = RegressionMetric.RSquared; | ||
maryamariyan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Trainers = Enum.GetValues(typeof(RecommendationTrainer)).OfType<RecommendationTrainer>().ToList(); | ||
} | ||
} | ||
|
||
public enum RecommendationTrainer | ||
{ | ||
MatrixFactorization, | ||
} | ||
|
||
public sealed class RecommendationExperiment : ExperimentBase<RegressionMetrics, RecommendationExperimentSettings> | ||
maryamariyan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
internal RecommendationExperiment(MLContext context, RecommendationExperimentSettings settings) | ||
: base(context, | ||
new RegressionMetricsAgent(context, settings.OptimizingMetric), | ||
new OptimizingMetricInfo(settings.OptimizingMetric), | ||
settings, | ||
TaskKind.Recommendation, | ||
TrainerExtensionUtil.GetTrainerNames(settings.Trainers)) | ||
{ | ||
} | ||
private protected override CrossValidationRunDetail<RegressionMetrics> GetBestCrossValRun(IEnumerable<CrossValidationRunDetail<RegressionMetrics>> results) | ||
{ | ||
return BestResultUtil.GetBestRun(results, MetricsAgent, OptimizingMetricInfo.IsMaximizing); | ||
} | ||
|
||
private protected override RunDetail<RegressionMetrics> GetBestRun(IEnumerable<RunDetail<RegressionMetrics>> results) | ||
{ | ||
return BestResultUtil.GetBestRun(results, MetricsAgent, OptimizingMetricInfo.IsMaximizing); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.