-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add Microsoft.Extensions.ML integration package. #3827
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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
16 changes: 16 additions & 0 deletions
16
pkg/Microsoft.Extensions.ML/Microsoft.Extensions.ML.nupkgproj
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,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk" DefaultTargets="Pack"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<PackageDescription>An integration package for ML.NET models on scalable web apps and services.</PackageDescription> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging" Version="$(MicrosoftExtensionsPackageVersion)" /> | ||
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="$(MicrosoftExtensionsPackageVersion)" /> | ||
<PackageReference Include="Microsoft.Extensions.Options" Version="$(MicrosoftExtensionsPackageVersion)" /> | ||
<ProjectReference Include="..\Microsoft.ML\Microsoft.ML.nupkgproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
5 changes: 5 additions & 0 deletions
5
pkg/Microsoft.Extensions.ML/Microsoft.Extensions.ML.symbols.nupkgproj
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,5 @@ | ||
<Project DefaultTargets="Pack"> | ||
|
||
<Import Project="Microsoft.Extensions.ML.nupkgproj" /> | ||
|
||
</Project> |
213 changes: 213 additions & 0 deletions
213
src/Microsoft.Extensions.ML/Builder/BuilderExtensions.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,213 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.Extensions.ML | ||
{ | ||
/// <summary> | ||
/// Extension methods for <see cref="PredictionEnginePoolBuilder{TData, TPrediction}"/>. | ||
/// </summary> | ||
public static class BuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Adds the model at the specified location to the builder. | ||
/// </summary> | ||
/// <param name="builder">The builder to which to add the model.</param> | ||
/// <param name="uri">The location of the model.</param> | ||
/// <returns> | ||
/// The updated <see cref="PredictionEnginePoolBuilder{TData, TPrediction}"/>. | ||
/// </returns> | ||
public static PredictionEnginePoolBuilder<TData, TPrediction> FromUri<TData, TPrediction>( | ||
this PredictionEnginePoolBuilder<TData, TPrediction> builder, string uri) | ||
where TData : class | ||
where TPrediction : class, new() | ||
{ | ||
return builder.FromUri(string.Empty, new Uri(uri)); | ||
} | ||
|
||
/// <summary> | ||
/// Adds the named model at the specified location to the builder. | ||
/// </summary> | ||
/// <param name="builder">The builder to which to add the model.</param> | ||
/// <param name="modelName"> | ||
/// The name of the model which allows for uniquely identifying the model when | ||
/// multiple models have the same <typeparamref name="TData"/> and | ||
/// <typeparamref name="TPrediction"/> types. | ||
/// </param> | ||
/// <param name="uri">The location of the model.</param> | ||
/// <returns> | ||
/// The updated <see cref="PredictionEnginePoolBuilder{TData, TPrediction}"/>. | ||
/// </returns> | ||
public static PredictionEnginePoolBuilder<TData, TPrediction> FromUri<TData, TPrediction>( | ||
this PredictionEnginePoolBuilder<TData, TPrediction> builder, string modelName, string uri) | ||
where TData : class | ||
where TPrediction : class, new() | ||
{ | ||
return builder.FromUri(modelName, new Uri(uri)); | ||
} | ||
|
||
/// <summary> | ||
/// Adds the named model at the specified location to the builder. | ||
/// </summary> | ||
/// <param name="builder">The builder to which to add the model.</param> | ||
/// <param name="modelName"> | ||
/// The name of the model which allows for uniquely identifying the model when | ||
/// multiple models have the same <typeparamref name="TData"/> and | ||
/// <typeparamref name="TPrediction"/> types. | ||
/// </param> | ||
/// <param name="uri">The location of the model.</param> | ||
/// <returns> | ||
/// The updated <see cref="PredictionEnginePoolBuilder{TData, TPrediction}"/>. | ||
/// </returns> | ||
public static PredictionEnginePoolBuilder<TData, TPrediction> FromUri<TData, TPrediction>( | ||
this PredictionEnginePoolBuilder<TData, TPrediction> builder, string modelName, Uri uri) | ||
where TData : class where TPrediction : class, new() | ||
{ | ||
return builder.FromUri(modelName, uri, TimeSpan.FromMinutes(5)); | ||
} | ||
|
||
/// <summary> | ||
/// Adds the model at the specified location to the builder. | ||
/// </summary> | ||
/// <param name="builder">The builder to which to add the model.</param> | ||
/// <param name="uri">The location of the model.</param> | ||
/// <param name="period"> | ||
/// How often to query if the model has been updated at the specified location. | ||
/// </param> | ||
/// <returns> | ||
/// The updated <see cref="PredictionEnginePoolBuilder{TData, TPrediction}"/>. | ||
/// </returns> | ||
public static PredictionEnginePoolBuilder<TData, TPrediction> FromUri<TData, TPrediction>( | ||
this PredictionEnginePoolBuilder<TData, TPrediction> builder, string uri, TimeSpan period) | ||
where TData : class where TPrediction : class, new() | ||
{ | ||
return builder.FromUri(string.Empty, new Uri(uri), period); | ||
} | ||
|
||
/// <summary> | ||
/// Adds the named model at the specified location to the builder. | ||
/// </summary> | ||
/// <param name="builder">The builder to which to add the model.</param> | ||
/// <param name="modelName"> | ||
/// The name of the model which allows for uniquely identifying the model when | ||
/// multiple models have the same <typeparamref name="TData"/> and | ||
/// <typeparamref name="TPrediction"/> types. | ||
/// </param> | ||
/// <param name="uri">The location of the model.</param> | ||
/// <param name="period"> | ||
/// How often to query if the model has been updated at the specified location. | ||
/// </param> | ||
/// <returns> | ||
/// The updated <see cref="PredictionEnginePoolBuilder{TData, TPrediction}"/>. | ||
/// </returns> | ||
public static PredictionEnginePoolBuilder<TData, TPrediction> FromUri<TData, TPrediction>( | ||
this PredictionEnginePoolBuilder<TData, TPrediction> builder, string modelName, string uri, TimeSpan period) | ||
where TData : class | ||
where TPrediction : class, new() | ||
{ | ||
return builder.FromUri(modelName, new Uri(uri), period); | ||
} | ||
|
||
/// <summary> | ||
/// Adds the named model at the specified location to the builder. | ||
/// </summary> | ||
/// <param name="builder">The builder to which to add the model.</param> | ||
/// <param name="modelName"> | ||
/// The name of the model which allows for uniquely identifying the model when | ||
/// multiple models have the same <typeparamref name="TData"/> and | ||
/// <typeparamref name="TPrediction"/> types. | ||
/// </param> | ||
/// <param name="uri">The location of the model.</param> | ||
/// <param name="period"> | ||
/// How often to query if the model has been updated at the specified location. | ||
/// </param> | ||
/// <returns> | ||
/// The updated <see cref="PredictionEnginePoolBuilder{TData, TPrediction}"/>. | ||
/// </returns> | ||
public static PredictionEnginePoolBuilder<TData, TPrediction> FromUri<TData, TPrediction>( | ||
this PredictionEnginePoolBuilder<TData, TPrediction> builder, string modelName, Uri uri, TimeSpan period) | ||
where TData : class | ||
where TPrediction : class, new() | ||
{ | ||
builder.Services.AddTransient<UriModelLoader, UriModelLoader>(); | ||
builder.Services.AddOptions<PredictionEnginePoolOptions<TData, TPrediction>>(modelName) | ||
.Configure<UriModelLoader>((opt, loader) => | ||
{ | ||
loader.Start(uri, period); | ||
opt.ModelLoader = loader; | ||
}); | ||
return builder; | ||
} | ||
|
||
/// <summary> | ||
/// Adds the model at the specified file to the builder. | ||
/// </summary> | ||
/// <param name="builder">The builder to which to add the model.</param> | ||
/// <param name="filePath">The location of the model.</param> | ||
/// <returns> | ||
/// The updated <see cref="PredictionEnginePoolBuilder{TData, TPrediction}"/>. | ||
/// </returns> | ||
public static PredictionEnginePoolBuilder<TData, TPrediction> FromFile<TData, TPrediction>( | ||
this PredictionEnginePoolBuilder<TData, TPrediction> builder, string filePath) | ||
where TData : class | ||
where TPrediction : class, new() | ||
{ | ||
return builder.FromFile(string.Empty, filePath, true); | ||
} | ||
|
||
/// <summary> | ||
/// Adds the model at the specified file to the builder. | ||
/// </summary> | ||
/// <param name="builder">The builder to which to add the model.</param> | ||
/// <param name="modelName"> | ||
/// The name of the model which allows for uniquely identifying the model when | ||
/// multiple models have the same <typeparamref name="TData"/> and | ||
/// <typeparamref name="TPrediction"/> types. | ||
/// </param> | ||
/// <param name="filePath">The location of the model.</param> | ||
/// <returns> | ||
/// The updated <see cref="PredictionEnginePoolBuilder{TData, TPrediction}"/>. | ||
/// </returns> | ||
public static PredictionEnginePoolBuilder<TData, TPrediction> FromFile<TData, TPrediction>( | ||
this PredictionEnginePoolBuilder<TData, TPrediction> builder, string modelName, string filePath) | ||
where TData : class | ||
where TPrediction : class, new() | ||
{ | ||
return builder.FromFile(modelName, filePath, true); | ||
} | ||
|
||
/// <summary> | ||
/// Adds the model at the specified file to the builder. | ||
/// </summary> | ||
/// <param name="builder">The builder to which to add the model.</param> | ||
/// <param name="modelName"> | ||
/// The name of the model which allows for uniquely identifying the model when | ||
/// multiple models have the same <typeparamref name="TData"/> and | ||
/// <typeparamref name="TPrediction"/> types. | ||
/// </param> | ||
/// <param name="filePath">The location of the model.</param> | ||
/// <param name="watchForChanges"> | ||
/// Whether to watch for changes to the file path and update the model when the file is changed or not. | ||
/// </param> | ||
/// <returns> | ||
/// The updated <see cref="PredictionEnginePoolBuilder{TData, TPrediction}"/>. | ||
/// </returns> | ||
public static PredictionEnginePoolBuilder<TData, TPrediction> FromFile<TData, TPrediction>( | ||
this PredictionEnginePoolBuilder<TData, TPrediction> builder, string modelName, string filePath, bool watchForChanges) | ||
where TData : class | ||
where TPrediction : class, new() | ||
{ | ||
builder.Services.AddTransient<FileModelLoader, FileModelLoader>(); | ||
builder.Services.AddOptions<PredictionEnginePoolOptions<TData, TPrediction>>(modelName) | ||
.Configure<FileModelLoader>((options, loader) => | ||
{ | ||
loader.Start(filePath, watchForChanges); | ||
options.ModelLoader = loader; | ||
}); | ||
return builder; | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Microsoft.Extensions.ML/Builder/PredictionEnginePoolBuilder.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,33 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.ML; | ||
|
||
namespace Microsoft.Extensions.ML | ||
{ | ||
/// <summary> | ||
/// A class that provides the mechanisms to configure a pool | ||
/// of ML.NET <see cref="PredictionEngine{TData, TPrediction}"/> objects. | ||
/// </summary> | ||
public class PredictionEnginePoolBuilder<TData, TPrediction> | ||
where TData : class | ||
where TPrediction : class, new() | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of <see cref="PredictionEnginePoolBuilder{TData, TPrediction}"/>. | ||
/// </summary> | ||
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param> | ||
public PredictionEnginePoolBuilder(IServiceCollection services) | ||
{ | ||
Services = services ?? throw new ArgumentException(nameof(services)); | ||
} | ||
|
||
/// <summary> | ||
/// The <see cref="IServiceCollection"/> to add services to. | ||
/// </summary> | ||
public IServiceCollection Services { get; private set; } | ||
eerhardt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Oops, something went wrong.
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.