Skip to content

Commit

Permalink
Merge pull request #10 from aslotte/Issue_2
Browse files Browse the repository at this point in the history
Created templates for ML.NET with MLOps.NET
  • Loading branch information
aslotte authored Aug 23, 2020
2 parents 22915d4 + 67c39b8 commit 5f2af95
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
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
}]
}
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 src/ML.NET.Templates/templates/ML.NET.Training.MLOps/Program.cs
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);
}
}
}
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
{
}
}

0 comments on commit 5f2af95

Please sign in to comment.