-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
add pipelineitem for Ova #363
Merged
Merged
Changes from 2 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 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 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,74 @@ | ||
// 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.ML.Runtime; | ||
using Microsoft.ML.Runtime.Data; | ||
using Microsoft.ML.Runtime.EntryPoints; | ||
using static Microsoft.ML.Runtime.EntryPoints.CommonInputs; | ||
|
||
namespace Microsoft.ML.Models | ||
{ | ||
public sealed partial class OneVersusAll | ||
{ | ||
/// <summary> | ||
/// Create OneVersusAll multiclass trainer. | ||
/// </summary> | ||
/// <param name="trainer">Underlying binary trainer</param> | ||
/// <param name="useProbabilities">"Use probabilities (vs. raw outputs) to identify top-score category</param> | ||
public static ILearningPipelineItem With(ITrainerInputWithLabel trainer, bool useProbabilities = true) | ||
{ | ||
return new OvaPipelineItem(trainer, useProbabilities); | ||
} | ||
|
||
private class OvaPipelineItem : ILearningPipelineItem | ||
{ | ||
private Var<IDataView> _data; | ||
private ITrainerInputWithLabel _trainer; | ||
private bool _useProbabilities; | ||
|
||
public OvaPipelineItem(ITrainerInputWithLabel trainer, bool useProbabilities) | ||
{ | ||
_trainer = trainer; | ||
_useProbabilities = useProbabilities; | ||
} | ||
|
||
public ILearningPipelineStep ApplyStep(ILearningPipelineStep previousStep, Experiment experiment) | ||
{ | ||
using (var env = new TlcEnvironment()) | ||
{ | ||
var subgraph = env.CreateExperiment(); | ||
subgraph.Add(_trainer); | ||
var ova = new OneVersusAll(); | ||
if (previousStep != null) | ||
{ | ||
if (!(previousStep is ILearningPipelineDataStep dataStep)) | ||
{ | ||
throw new InvalidOperationException($"{ nameof(OneVersusAll)} only supports an { nameof(ILearningPipelineDataStep)} as an input."); | ||
} | ||
|
||
_data = dataStep.Data; | ||
ova.TrainingData = dataStep.Data; | ||
ova.UseProbabilities = _useProbabilities; | ||
ova.Nodes = subgraph; | ||
} | ||
Output output = experiment.Add(ova); | ||
return new OvaPipelineStep(output); | ||
} | ||
} | ||
|
||
public Var<IDataView> GetInputData() => _data; | ||
} | ||
|
||
private class OvaPipelineStep : ILearningPipelinePredictorStep | ||
{ | ||
public OvaPipelineStep(Output output) | ||
{ | ||
Model = output.PredictorModel; | ||
} | ||
|
||
public Var<IPredictorModel> Model { get; } | ||
} | ||
} | ||
} |
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
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
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.
It would also be better here to match on fixed decimal places.