-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Prediction engine options #5964
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 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fc233aa
Prediction engine no longer disposes model.
michaelgsharp e09622f
Advanced options for the prediction engine.
michaelgsharp abb5165
Fixed Test
michaelgsharp cc2158d
Updates from PR comments.
michaelgsharp 8a1dbdb
Comments from PR
michaelgsharp 2189839
Update src/Microsoft.ML.Data/Model/ModelOperationsCatalog.cs
michaelgsharp 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
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 |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using Microsoft.ML.CommandLine; | ||
| using Microsoft.ML.Data; | ||
| using Microsoft.ML.Runtime; | ||
|
|
||
|
|
@@ -58,8 +59,8 @@ public sealed class PredictionEngine<TSrc, TDst> : PredictionEngineBase<TSrc, TD | |
| where TDst : class, new() | ||
| { | ||
| internal PredictionEngine(IHostEnvironment env, ITransformer transformer, bool ignoreMissingColumns, | ||
| SchemaDefinition inputSchemaDefinition = null, SchemaDefinition outputSchemaDefinition = null) | ||
| : base(env, transformer, ignoreMissingColumns, inputSchemaDefinition, outputSchemaDefinition) | ||
| SchemaDefinition inputSchemaDefinition = null, SchemaDefinition outputSchemaDefinition = null, bool ownModelFile = true) | ||
| : base(env, transformer, ignoreMissingColumns, inputSchemaDefinition, outputSchemaDefinition, ownModelFile) | ||
| { | ||
| } | ||
|
|
||
|
|
@@ -92,6 +93,7 @@ public abstract class PredictionEngineBase<TSrc, TDst> : IDisposable | |
| private readonly DataViewConstructionUtils.InputRow<TSrc> _inputRow; | ||
| private readonly IRowReadableAs<TDst> _outputRow; | ||
| private readonly Action _disposer; | ||
| private readonly bool _ownModelFile; | ||
| private bool _disposed; | ||
|
|
||
| /// <summary> | ||
|
|
@@ -104,14 +106,15 @@ public abstract class PredictionEngineBase<TSrc, TDst> : IDisposable | |
|
|
||
| [BestFriend] | ||
| private protected PredictionEngineBase(IHostEnvironment env, ITransformer transformer, bool ignoreMissingColumns, | ||
| SchemaDefinition inputSchemaDefinition = null, SchemaDefinition outputSchemaDefinition = null) | ||
| SchemaDefinition inputSchemaDefinition = null, SchemaDefinition outputSchemaDefinition = null, bool ownModelFile = true) | ||
| { | ||
| Contracts.CheckValue(env, nameof(env)); | ||
| env.AssertValue(transformer); | ||
| Transformer = transformer; | ||
| var makeMapper = TransformerChecker(env, transformer); | ||
| env.AssertValue(makeMapper); | ||
| _inputRow = DataViewConstructionUtils.CreateInputRow<TSrc>(env, inputSchemaDefinition); | ||
| _ownModelFile = ownModelFile; | ||
| PredictionEngineCore(env, _inputRow, makeMapper(_inputRow.Schema), ignoreMissingColumns, outputSchemaDefinition, out _disposer, out _outputRow); | ||
| OutputSchema = Transformer.GetOutputSchema(_inputRow.Schema); | ||
| } | ||
|
|
@@ -139,7 +142,9 @@ public void Dispose() | |
| return; | ||
|
|
||
| _disposer?.Invoke(); | ||
| (Transformer as IDisposable)?.Dispose(); | ||
|
|
||
| if (_ownModelFile) | ||
| (Transformer as IDisposable)?.Dispose(); | ||
|
|
||
| _disposed = true; | ||
| } | ||
|
|
@@ -170,4 +175,34 @@ public TDst Predict(TSrc example) | |
| /// is reused.</param> | ||
| public abstract void Predict(TSrc example, ref TDst prediction); | ||
| } | ||
|
|
||
| public sealed class PredictionEngine | ||
|
eerhardt marked this conversation as resolved.
Outdated
|
||
| { | ||
| /// <summary> | ||
| /// Options for the <see cref="PredictionEngine{TSrc, TDst}"/> as used in | ||
| /// [RandomizedPca(Options)](xref:Microsoft.ML.PcaCatalog.RandomizedPca(Microsoft.ML.AnomalyDetectionCatalog.AnomalyDetectionTrainers,Microsoft.ML.Trainers.RandomizedPcaTrainer.Options)). | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a copy-paste error? #Resolved
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, resolved. |
||
| /// </summary> | ||
| public sealed class Options | ||
| { | ||
| [Argument(ArgumentType.AtMostOnce, HelpText = "Whether to throw an error if a column exists in the output schema but not the output object.", ShortName = "ignore", SortOrder = 50)] | ||
| public bool IgnoreMissingColumns = Defaults.IgnoreMissingColumns; | ||
|
|
||
| [Argument(ArgumentType.AtMostOnce, HelpText = "Additional settings of the input schema.", ShortName = "input", SortOrder = 50)] | ||
| public SchemaDefinition InputSchemaDefinition = Defaults.InputSchemaDefinition; | ||
|
|
||
| [Argument(ArgumentType.AtMostOnce, HelpText = "Additional settings of the output schema.", ShortName = "output")] | ||
| public SchemaDefinition OutputSchemaDefinition = Defaults.OutputSchemaDefinition; | ||
|
|
||
| [Argument(ArgumentType.AtMostOnce, HelpText = "Whether the prediction engine owns the model file and should dispose of it.", ShortName = "own")] | ||
| public bool OwnModelFile = Defaults.OwnModelFile; | ||
|
eerhardt marked this conversation as resolved.
Outdated
|
||
|
|
||
| internal static class Defaults | ||
| { | ||
| public const bool IgnoreMissingColumns = true; | ||
| public const SchemaDefinition InputSchemaDefinition = null; | ||
| public const SchemaDefinition OutputSchemaDefinition = null; | ||
| public const bool OwnModelFile = true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.