-
Notifications
You must be signed in to change notification settings - Fork 1.9k
API 'getting started' examples #639
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Microsoft.ML.Tests.Scenarios.Api | ||
{ | ||
public class AspirationalExamples | ||
{ | ||
public class IrisPrediction | ||
{ | ||
public string PredictedLabel; | ||
} | ||
|
||
public class IrisExample | ||
{ | ||
public float SepalWidth { get; set; } | ||
public float SepalLength { get; set; } | ||
public float PetalWidth { get; set; } | ||
public float PetalLength { get; set; } | ||
} | ||
|
||
public void FirstExperienceWithML() | ||
{ | ||
// This is the 'getting started with ML' example, how we see it in our new API. | ||
// It currently doesn't compile, let alone work, but we still can discuss and improve the syntax. | ||
|
||
// Load the data into the system. | ||
string dataPath = "iris-data.txt"; | ||
var data = TextReader.FitAndRead(env, dataPath, row => ( | ||
Label: row.ReadString(0), | ||
SepalWidth: row.ReadFloat(1), | ||
SepalLength: row.ReadFloat(2), | ||
PetalWidth: row.ReadFloat(3), | ||
PetalLength: row.ReadFloat(4))); | ||
|
||
|
||
var preprocess = data.Schema.MakeEstimator(row => ( | ||
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. Why do we need to expose Schema to users here? Can we simply do data.MakeExtimator(row => ...) #Closed 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. this is how I originally phrased it, but it makes an appearance that the data itself is being 'used' and somehow 'remembered' by the estimator. Hence the 'Schema' in the middle. In reply to: 208640562 [](ancestors = 208640562) 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. I see. Now that I think about that, the flow looks a little bit odd in my opinion. You start from the data, then you create an estimator from the data (without using the data) and then you call fit over the data again. In principle you only really need the data at the beginning when you create the reader, right? Or you can actually call fit over different datasets? If the later is true, why do you need to create a reader over a specific dataset at the beginnig? In the current API data and pipeline creations are entangled in multiple places and I think it create confusion a bit. #Closed 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. you can actually call The only thing we really need to var preprocess = PigstyPipeline.WithSchema(row=>
row => (
Label: row.String(),
SepalWidth: row.Float(),
SepalLength: row.Float(),
PetalWidth: row.Float(),
PetalLength: row.Float())
.MakeEstimator(row => (Label: row.Label.DictionarizeLabel() ...) in this case, there is no need to read In reply to: 208660933 [](ancestors = 208660933) 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. Agreed: at some point you will need to create a reader and define the schema, but I think that separating the 2 (data creation from pipeline creation) is definitely cleaner. Additionally, for the datasets you could only define the schema on the pipeline, and do 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. I don't think this is going to work. You cannot just take an In reply to: 208726028 [](ancestors = 208726028) 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. Well but that is a problem of the dataset, not of the pipeline nor of the schema. In theory one would do something like |
||
// Convert string label to key. | ||
Label: row.Label.DictionarizeLabel(), | ||
// Concatenate all features into a vector. | ||
Features: row.SepalWidth.ConcatWith(row.SepalLength, row.PetalWidth, row.PetalLength))); | ||
|
||
// Create a learner and train it. | ||
var learner = preprocess.AppendEstimator(row => row.Label.SdcaPredict(row.Features, l1Coefficient: 0.1)); | ||
var classifier = learner.Fit(data); | ||
|
||
// Add another transformer that converts the integer prediction into string. | ||
var finalTransformer = classifier.AppendTransformer(row => row.PredictedLabel.KeyToValue()); | ||
|
||
// Make a prediction engine and predict. | ||
engine = finalTransformer.MakePredictionEngine<IrisExample, IrisPrediction>(); | ||
IrisPrediction prediction = engine.Predict(new IrisExample | ||
{ | ||
SepalWidth = 3.3f, | ||
SepalLength = 1.6f, | ||
PetalWidth = 0.2f, | ||
PetalLength = 5.1f | ||
}); | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
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.
Do we need to be concerned about asynchronous I/O? Should methods that are I/O bound return
Task<T>
? #PendingThere 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.
I think they should have an async analogy, but async-only - I'm not sure
In reply to: 208417516 [](ancestors = 208417516)
Uh oh!
There was an error while loading. Please reload this page.
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.
I sort of remember the discussion, but why do we need to expose FitAndRead to users here? Do we also have a Read case without Fit? Can we simply have something like env.TextReader.Read(datapath, row => ...)? #Pending
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.
Our existing TextLoader is trainable: it scans the schema of the file and determines number of features under certain conditions.
So, we are essentially calling:
(and discarding the estimator and the reader in the process.
I suppose if we want to discard the reader anyway, we might just as well rename
FitAndRead
toRead
. But technically, it would still call the above code, so I was reluctant to hide what we are actually doing.In reply to: 208640305 [](ancestors = 208640305)