-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from aslotte/Issue_2
Created templates for ML.NET with MLOps.NET
- Loading branch information
Showing
4 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/ML.NET.Templates/templates/ML.NET.Training.MLOps/.template.config/template.json
This file contains 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,41 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/template", | ||
"author": "Alexander Slotte", | ||
"classifications": [ "ML.NET", "MLOps.NET", "Training" ], | ||
"identity": "mlnet-training-mlops", | ||
"name": "ML.NET Console App for Training with MLOps.NET support", | ||
"shortName": "mlnet-training-mlops", | ||
"tags": { | ||
"language": "C#", | ||
"type": "project" | ||
}, | ||
"symbols": { | ||
"mlnetVersion": { | ||
"type": "parameter", | ||
"replaces": "{mlnetVersion}", | ||
"defaultValue": "1.5.1" | ||
}, | ||
"mlopsnetVersion": { | ||
"type": "parameter", | ||
"replaces": "{mlopsnetVersion}", | ||
"defaultValue": "1.1.0" | ||
}, | ||
"skipRestore": { | ||
"type": "parameter", | ||
"defaultValue": "false" | ||
} | ||
}, | ||
"primaryOutputs": [ | ||
{ "path": "ML.NET.Training.MLOps.csproj" } | ||
], | ||
"defaultName": "ModelTrainer", | ||
"postActions": [{ | ||
"condition": "(!skipRestore)", | ||
"description": "Restore NuGet packages required by this project.", | ||
"manualInstructions": [ | ||
{ "text": "Run 'dotnet restore'" } | ||
], | ||
"actionId": "210D431B-A78B-4D2F-B762-4ED3E3EA9025", | ||
"continueOnError": true | ||
}] | ||
} |
13 changes: 13 additions & 0 deletions
13
src/ML.NET.Templates/templates/ML.NET.Training.MLOps/ML.NET.Training.MLOps.csproj
This file contains 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,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.ML" Version="{mlnetVersion}" /> | ||
<PackageReference Include="MLOps.NET.SQLite" Version="{mlopsnetVersion}" /> | ||
</ItemGroup> | ||
|
||
</Project> |
76 changes: 76 additions & 0 deletions
76
src/ML.NET.Templates/templates/ML.NET.Training.MLOps/Program.cs
This file contains 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,76 @@ | ||
using Microsoft.ML; | ||
using Microsoft.ML.Data; | ||
using ML.NET.Training.Schema; | ||
using System; | ||
using System.Diagnostics; | ||
using MLOps.NET; | ||
using MLOps.NET.Entities.Impl; | ||
using MLOps.NET.SQLite; | ||
using MLOps.NET.Extensions; | ||
using System.Threading.Tasks; | ||
|
||
namespace ML.NET.Training | ||
{ | ||
class Program | ||
{ | ||
private static readonly string DataPath = ""; | ||
private static readonly string ModelName = "model.zip"; | ||
private static readonly bool HasHeader = true; | ||
private static readonly char SeparatorChar = ','; | ||
|
||
static async Task Main(string[] args) | ||
{ | ||
var stopWatch = new Stopwatch(); | ||
stopWatch.Start(); | ||
|
||
(IMLOpsContext mlOpsContext, Run run) = await CreateRun("MyExperiment"); | ||
var mlContext = new MLContext(seed: 1); | ||
|
||
Console.WriteLine($"Loading data from {DataPath}"); | ||
var data = mlContext.Data.LoadFromTextFile<ModelInput>(DataPath, hasHeader: HasHeader, separatorChar: SeparatorChar); | ||
|
||
Console.WriteLine("Logging data"); | ||
await mlOpsContext.Data.LogDataAsync(run.RunId, data); | ||
|
||
Console.WriteLine("Splitting the data"); | ||
var trainTestSplit = mlContext.Data.TrainTestSplit(data); | ||
|
||
Console.WriteLine("Transforming the data"); | ||
IEstimator<ITransformer> dataProcessPipeline = null; | ||
|
||
Console.WriteLine("Training the model"); | ||
IEstimator<ITransformer> trainer = null; | ||
EstimatorChain<ITransformer> trainingPipeline = dataProcessPipeline.Append(trainer); | ||
|
||
Console.WriteLine("Logging hyper-parameters"); | ||
await mlOpsContext.Training.LogHyperParametersAsync(run.RunId, trainer); | ||
|
||
ITransformer model = trainingPipeline.Fit(trainTestSplit.TrainSet); | ||
|
||
Console.WriteLine("Evaluating the model's performance"); | ||
//await mlOpsContext.Evaluation.LogMetricsAsync(run.RunId, metrics); | ||
|
||
stopWatch.Stop(); | ||
Console.WriteLine($"Training finished in: {stopWatch.ElapsedMilliseconds} milliseconds"); | ||
await mlOpsContext.LifeCycle.SetTrainingTimeAsync(run.RunId, stopWatch.Elapsed); | ||
|
||
Console.WriteLine($"Saving the model to {ModelName}"); | ||
mlContext.Model.Save(model, trainTestSplit.TrainSet.Schema, ModelName); | ||
|
||
Console.WriteLine("Uploading model to model repository"); | ||
await mlOpsContext.Model.UploadAsync(run.RunId, ModelName); | ||
} | ||
|
||
private static async Task<(IMLOpsContext, Run)> CreateRun(string experimentName) | ||
{ | ||
var mlOpsContext = new MLOpsBuilder() | ||
.UseSQLite() | ||
.UseLocalFileModelRepository() | ||
.Build(); | ||
|
||
var run = await mlOpsContext.LifeCycle.CreateRunAsync(experimentName); | ||
|
||
return (mlOpsContext, run); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/ML.NET.Templates/templates/ML.NET.Training.MLOps/Schema/ModelInput.cs
This file contains 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,11 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Microsoft.ML.Data; | ||
|
||
namespace ML.NET.Training.Schema | ||
{ | ||
public class ModelInput | ||
{ | ||
} | ||
} |