Skip to content

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions test/Microsoft.ML.Tests/Microsoft.ML.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Scenarios\Api\AspirationalExamples.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.ML.Ensemble\Microsoft.ML.Ensemble.csproj" />
Expand All @@ -28,4 +31,8 @@
<ItemGroup>
<PackageReference Include="MlNetMklDeps" Version="$(MlNetMklDepsPackageVersion)" />
</ItemGroup>

<ItemGroup>
<None Include="Scenarios\Api\AspirationalExamples.cs" />
</ItemGroup>
</Project>
61 changes: 61 additions & 0 deletions test/Microsoft.ML.Tests/Scenarios/Api/AspirationalExamples.cs
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 => (
Copy link
Member

@eerhardt eerhardt Aug 7, 2018

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>? #Pending

Copy link
Contributor Author

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)

Copy link

@interesaaat interesaaat Aug 8, 2018

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

Copy link
Contributor Author

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:

TextReader.CreateEstimator(row=>...).Fit(dataPath).Read(dataPath)

(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 to Read. 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)

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 => (
Copy link

@interesaaat interesaaat Aug 8, 2018

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)

Copy link

@interesaaat interesaaat Aug 8, 2018

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

@Zruty0 Zruty0 Aug 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can actually call Fit over different datasets, of course.

The only thing we really need to MakeEstimator is to have the input schema. We sort-of piggy-backed on the TextReader here to define the schema and simultaneously define how to read the data file.
We could instead do

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 data until whenever you want.
But at SOME point you will have to define HOW to read these columns, so lines 30-34 are still needed


In reply to: 208660933 [](ancestors = 208660933)

Choose a reason for hiding this comment

The 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 env.TextReader.Read(dataPath) and the validation of the schema will be done on the pipeline at fit time. Basically you could reverse the pattern you have now: define the schema on the pipeline and have the dataset schema matched at fit time, instead of define the schema on the dataset and have the data.Schema thing to pass the schema to the pipeline.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 ISchema and make text reader configuration of out it: you need to tell what column indices go where. Note the ReadFloat(0) and other arguments to ReadXXX


In reply to: 208726028 [](ancestors = 208726028)

Choose a reason for hiding this comment

The 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 data.map(row => row._2, row._1, row._0) if there is the need to change column orders for example. Even because if you have many possible input dataset, some with the right order and some not, it is verbose to repeat the schema definition for all of them, where verbosity == more chances or errors.

// 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
});
}
}
}