8000 Add movie recommendation sample (#929) · dotnet/machinelearning-samples@24f4c77 · GitHub
[go: up one dir, main page]

Skip to content

Commit 24f4c77

Browse files
authored
Add movie recommendation sample (#929)
1 parent f435bdd commit 24f4c77

16 files changed

+487
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Ignore mbconfig since it has absolute paths to data files
2+
*.mbconfig
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31717.71
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MovieRecommendation_ModelBuilder", "MovieRecommendation_ModelBuilder\MovieRecommendation_ModelBuilder.csproj", "{CD8A95DB-E37F-4B5D-B250-9E7689474E7A}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MovieRecommender_Console", "MovieRecommender_Console\MovieRecommender_Console.csproj", "{78EDBEA7-A03F-44BE-8C27-629ECA9FC83E}"
9+
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MovieRecommender_WebApi", "MovieRecommender_WebApi\MovieRecommender_WebApi.csproj", "{72AAF5A5-A22A-4B9E-B1CF-0C032F4A28CC}"
11+
EndProject
12+
Global
13+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
14+
Debug|Any CPU = Debug|Any CPU
15+
Release|Any CPU = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{CD8A95DB-E37F-4B5D-B250-9E7689474E7A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{CD8A95DB-E37F-4B5D-B250-9E7689474E7A}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{CD8A95DB-E37F-4B5D-B250-9E7689474E7A}.Release|Any CPU.ActiveCfg = Release|Any CPU
21+
{CD8A95DB-E37F-4B5D-B250-9E7689474E7A}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{78EDBEA7-A03F-44BE-8C27-629ECA9FC83E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{78EDBEA7-A03F-44BE-8C27-629ECA9FC83E}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{78EDBEA7-A03F-44BE-8C27-629ECA9FC83E}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{78EDBEA7-A03F-44BE-8C27-629ECA9FC83E}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{72AAF5A5-A22A-4B9E-B1CF-0C032F4A28CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{72AAF5A5-A22A-4B9E-B1CF-0C032F4A28CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{72AAF5A5-A22A-4B9E-B1CF-0C032F4A28CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{72AAF5A5-A22A-4B9E-B1CF-0C032F4A28CC}.Release|Any CPU.Build.0 = Release|Any CPU
30+
EndGlobalSection
31+
GlobalSection(SolutionProperties) = preSolution
32+
HideSolutionNode = FALSE
33+
EndGlobalSection
34+
GlobalSection(ExtensibilityGlobals) = postSolution
35+
SolutionGuid = {68972A1D-BAC6-4F18-B210-06314C4BC7BB}
36+
EndGlobalSection
37+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net5.0</TargetFramework>
4+
</PropertyGroup>
5+
<ItemGroup>
6+
<PackageReference Include="Microsoft.ML" Version="1.6.0" />
7+
<PackageReference Include="Microsoft.ML.Recommender" Version="0.18.0" />
8+
</ItemGroup>
9+
<ItemGroup Label="MovieRecommender">
10+
<None Include="MovieRecommender.consumption.cs">
11+
<DependentUpon>MovieRecommender.mbconfig</DependentUpon>
12+
</None>
13+
<None Include="MovieRecommender.training.cs">
14+
<DependentUpon>MovieRecommender.mbconfig</DependentUpon>
15+
</None>
16+
<None Include="MovieRecommender.zip">
17+
<DependentUpon>MovieRecommender.mbconfig</DependentUpon>
18+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
19+
</None>
20+
</ItemGroup>
21+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// This file was auto-generated by ML.NET Model Builder.
2+
using Microsoft.ML;
3+
using Microsoft.ML.Data;
4+
using System;
5+
using System.Linq;
6+
using System.IO;
7+
using System.Collections.Generic;
8+
namespace MovieRecommendation_ModelBuilder
9+
{
10+
public partial class MovieRecommender
11+
{
12+
/// <summary>
13+
/// model input class for MovieRecommender.
14+
/// </summary>
15+
#region model input class
16+
public class ModelInput
17+
{
18+
[ColumnName(@"userId")]
19+
public float UserId { get; set; }
20+
21+
[ColumnName(@"movieId")]
22+
public float MovieId { get; set; }
23+
24+
[ColumnName(@"rating")]
25+
public float Rating { get; set; }
26+
27+
[ColumnName(@"timestamp")]
28+
public float Timestamp { get; set; }
29+
30+
}
31+
32+
#endregion
33+
34+
/// <summary>
35+
/// model output class for MovieRecommender.
36+
/// </summary>
37+
#region model output class
38+
public class ModelOutput
39+
{
40+
public float Score { get; set; }
41+
}
42+
#endregion
43+
44+
private static string MLNetModelPath = Path.GetFullPath("MovieRecommender.zip");
45+
46+
public static readonly Lazy<PredictionEngine<ModelInput, ModelOutput>> PredictEngine = new Lazy<PredictionEngine<ModelInput, ModelOutput>>(() => CreatePredictEngine(), true);
47+
48+
/// <summary>
49+
/// Use this method to predict on <see cref="ModelInput"/>.
50+
/// </summary>
51+
/// <param name="input">model input.</param>
52+
/// <returns><seealso cref=" ModelOutput"/></returns>
53+
public static ModelOutput Predict(ModelInput input)
54+
{
55+
var predEngine = PredictEngine.Value;
56+
return predEngine.Predict(input);
57+
}
58+
59+
private static PredictionEngine<ModelInput, ModelOutput> CreatePredictEngine()
60+
{
61+
var mlContext = new MLContext();
62+
ITransformer mlModel = mlContext.Model.Load(MLNetModelPath, out var _);
63+
return mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
64+
}
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// This file was auto-generated by ML.NET Model Builder.
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Microsoft.ML.Data;
8+
using Microsoft.ML.Trainers;
9+
using Microsoft.ML;
10+
11+
namespace MovieRecommendation_ModelBuilder
12+
{
13+
public partial class MovieRecommender
14+
{
15+
public static ITransformer RetrainPipeline(MLContext context, IDataView trainData)
16+
{
17+
var pipeline = BuildPipeline(context);
18+
var model = pipeline.Fit(trainData);
19+
20+
return model;
21+
}
22+
23+
/// <summary>
24+
/// build the pipeline that is used from model builder. Use this function to retrain model.
25+
/// </summary>
26+
/// <param name="mlContext"></param>
27+
/// <returns></returns>
28+
public static IEstimator<ITransformer> BuildPipeline(MLContext mlContext)
29+
{
30+
// Data process configuration with pipeline data transformations
31+
var pipeline = mlContext.Transforms.Conversion.MapValueToKey(@"userId", @"userId")
32+
.Append(mlContext.Transforms.Conversion.MapValueToKey(@"movieId", @"movieId"))
33+
.Append(mlContext.Recommendation().Trainers.MatrixFactorization(approximationRank:128,numberOfIterations:20,learningRate:0.1F,labelColumnName:@"rating",matrixColumnIndexColumnName:@"userId",matrixRowIndexColumnName:@"movieId"));
34+
35+
return pipeline;
36+
}
37+
}
38+
}
Original file lin F438 e numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// This file was auto-generated by ML.NET Model Builder.
2+
using Microsoft.ML;
3+
using Microsoft.ML.Data;
4+
using System;
5+
using System.Linq;
6+
using System.IO;
7+
using System.Collections.Generic;
8+
namespace MovieRecommender_Console
9+
{
10+
public partial class MovieRecommender
11+
{
12+
/// <summary>
13+
/// model input class for MovieRecommender.
14+
/// </summary>
15+
#region model input class
16+
public class ModelInput
17+
{
18+
[ColumnName(@"userId")]
19+
public float UserId { get; set; }
20+
21+
[ColumnName(@"movieId")]
22+
public float MovieId { get; set; }
23+
24+
[ColumnName(@"rating")]
25+
public float Rating { get; set; }
26+
27+
[ColumnName(@"timestamp")]
28+
public float Timestamp { get; set; }
29+
30+
}
31+
32+
#endregion
33+
34+
/// <summary>
35+
/// model output class for MovieRecommender.
36+
/// </summary>
37+
#region model output class
38+
public class ModelOutput
39+
{
40+
public float Score { get; set; }
41+
}
42+
#endregion
43+
44+
private static string MLNetModelPath = Path.GetFullPath("MovieRecommender.zip");
45+
46+
public static readonly Lazy<PredictionEngine<ModelInput, ModelOutput>> PredictEngine = new Lazy<PredictionEngine<ModelInput, ModelOutput>>(() => CreatePredictEngine(), true);
47+
48+
/// <summary>
49+
/// Use this method to predict on <see cref="ModelInput"/>.
50+
/// </summary>
51+
/// <param name="input">model input.</param>
52+
/// <returns><seealso cref=" ModelOutput"/></returns>
53+
public static ModelOutput Predict(ModelInput input)
54+
{
55+
var predEngine = PredictEngine.Value;
56+
return predEngine.Predict(input);
57+
}
58+
59+
private static PredictionEngine<ModelInput, ModelOutput> CreatePredictEngine()
60+
{
61+
var mlContext = new MLContext();
62+
ITransformer mlModel = mlContext.Model.Load(MLNetModelPath, out var _);
63+
return mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
64+
}
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// This file was auto-generated by ML.NET Model Builder.
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Microsoft.ML.Data;
8+
using Microsoft.ML.Trainers;
9+
using Microsoft.ML;
10+
11+
namespace MovieRecommender_Console
12+
{
13+
public partial class MovieRecommender
14+
{
15+
public static ITransformer RetrainPipeline(MLContext context, IDataView trainData)
16+
{
17+
var pipeline = BuildPipeline(context);
18+
var model = pipeline.Fit(trainData);
19+
20+
return model;
21+
}
22+
23+
/// <summary>
24+
/// build the pipeline that is used from model builder. Use this function to retrain model.
25+
/// </summary>
26+
/// <param name="mlContext"></param>
27+
/// <returns></returns>
28+
public static IEstimator<ITransformer> BuildPipeline(MLContext mlContext)
29+
{
30+
// Data process configuration with pipeline data transformations
31+
var pipeline = mlContext.Transforms.Conversion.MapValueToKey(@"userId", @"userId")
32+
.Append(mlContext.Transforms.Conversion.MapValueToKey(@"movieId", @"movieId"))
33+
.Append(mlContext.Recommendation().Trainers.MatrixFactorization(approximationRank:128,numberOfIterations:20,learningRate:0.1F,labelColumnName:@"rating",matrixColumnIndexColumnName:@"userId",matrixRowIndexColumnName:@"movieId"));
34+
35+
return pipeline;
36+
}
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net5.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<PackageReference Include="Microsoft.ML" Version="1.6.0" />
8+
<PackageReference Include="Microsoft.ML.Recommender" Version="0.18.0" />
9+
</ItemGroup>
10+
<ItemGroup Label="MovieRecommender">
11+
<None Include="MovieRecommender.consumption.cs">
12+
<DependentUpon>MovieRecommender.mbconfig</DependentUpon>
13+
</None>
14+
<None Include="MovieRecommender.training.cs">
15+
<DependentUpon>MovieRecommender.mbconfig</DependentUpon>
16+
</None>
17+
<None Include="MovieRecommender.zip">
18+
<DependentUpon>MovieRecommender.mbconfig</DependentUpon>
19+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
20+
</None>
21+
</ItemGroup>
22+
</Project>

0 commit comments

Comments
 (0)
0