-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Issue 3234: use model schema type instead of class definition schema #5228
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
3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -217,6 +217,7 @@ public class ModelInput | |
| public string[] CategoricalFeatures; | ||
| public float[] NumericalFeatures; | ||
| #pragma warning restore SA1401 | ||
| public float Label; | ||
|
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.
add extra column Label as the model modelwithoptionalcolumntransform's schema contains an extra Label column |
||
| } | ||
|
|
||
| public class ModelOutput | ||
|
|
@@ -233,14 +234,29 @@ public void LoadModelWithOptionalColumnTransform() | |
| SchemaDefinition inputSchemaDefinition = SchemaDefinition.Create(typeof(ModelInput)); | ||
| inputSchemaDefinition[nameof(ModelInput.CategoricalFeatures)].ColumnType = new VectorDataViewType(TextDataViewType.Instance, 5); | ||
| inputSchemaDefinition[nameof(ModelInput.NumericalFeatures)].ColumnType = new VectorDataViewType(NumberDataViewType.Single, 3); | ||
|
|
||
| var mlContext = new MLContext(1); | ||
| ITransformer trainedModel; | ||
| DataViewSchema dataViewSchema; | ||
| trainedModel = mlContext.Model.Load(TestCommon.GetDataPath(DataDir, "backcompat", "modelwithoptionalcolumntransform.zip"), out dataViewSchema); | ||
|
|
||
| var modelInput = new ModelInput() | ||
| { | ||
| CategoricalFeatures = new[] { "ABC", "ABC", "ABC", "ABC", "ABC" }, | ||
| NumericalFeatures = new float[] { 1, 1, 1 }, | ||
| Label = 1 | ||
| }; | ||
|
|
||
| // test create prediction engine with user defined schema | ||
| var model = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(trainedModel, inputSchemaDefinition: inputSchemaDefinition); | ||
| var prediction = model.Predict(new ModelInput() { CategoricalFeatures = new[] { "ABC", "ABC", "ABC", "ABC", "ABC" }, NumericalFeatures = new float [] { 1, 1, 1 } }); | ||
| var prediction = model.Predict(modelInput); | ||
|
|
||
| // test create prediction engine with schema loaded from model | ||
| var model2 = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(trainedModel, inputSchema: dataViewSchema); | ||
| var prediction2 = model2.Predict(modelInput); | ||
|
|
||
| Assert.Equal(1, prediction.Score[0]); | ||
| Assert.Equal(1, prediction2.Score[0]); | ||
| } | ||
|
|
||
| [Fact] | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will there be a check or at least logging for this corner case? Maybe we should put in a check for arrays of size 0, which is what
float[]would be. Also is there a check for the size of provided arrays matching the size of the correspondingVector?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test LoadModelWithOptionalColumnTransform is to test this case.
for length check, it is applied at below:
https://github.com/dotnet/machinelearning/blob/master/src/Microsoft.ML.Data/Scorers/SchemaBindablePredictorWrapper.cs#L129-L147
In reply to: 439585282 [](ancestors = 439585282)