-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Conversion of NAIndicatorTransform to estimator with related pigstensions #1217
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
21 commits
Select commit
Hold shift + click to select a range
e0c85cd
started conversion work
artidoro 9e23955
started conversion work
artidoro 11b7dd9
Merge branch 'master' of https://github.com/dotnet/machinelearning in…
artidoro e0e7d3b
wrote a first version of the conversion, need to debug it and make su…
artidoro 593f7d5
Merge branch 'master' of https://github.com/dotnet/machinelearning in…
artidoro 9c1e758
finished debugging tests
artidoro 5e20acd
Merge branch 'master' of https://github.com/dotnet/machinelearning in…
artidoro 4062bed
cleanup
artidoro 76a893a
fixing an issue
artidoro 8f331fd
fixed review comments
artidoro fe94d30
fixed some review comments and added a test
artidoro 122d48c
fixed entrypointcatalog test
artidoro ef8c999
Merge branch 'master' of https://github.com/dotnet/machinelearning in…
artidoro 28e9570
propagated metadata
artidoro f720a75
fixed review comments
artidoro 90784fb
Merge branch 'master' of https://github.com/dotnet/machinelearning in…
artidoro 5d4b6bc
fixed review comments
artidoro dd49b4b
Merge branch 'master' into naindicator
artidoro 0f4507f
removed unused object
artidoro 25c9b9e
Merge branch 'naindicator' of https://github.com/artidoro/machinelear…
artidoro 4a6a5d3
Merge branch 'master' of https://github.com/dotnet/machinelearning in…
artidoro 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
17 changes: 17 additions & 0 deletions
17
test/BaselineOutput/SingleDebug/NAIndicator/featurized.tsv
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#@ TextLoader{ | ||
#@ header+ | ||
#@ sep=tab | ||
#@ col=ScalarFloat:R4:0 | ||
#@ col=ScalarDouble:R8:1 | ||
#@ col=VectorFloat:R4:2-5 | ||
#@ col=VectorDoulbe:R8:6-9 | ||
#@ col=A:BL:10 | ||
#@ col=B:BL:11 | ||
#@ col=C:BL:12-15 | ||
#@ col=D:BL:16-19 | ||
#@ } | ||
ScalarFloat ScalarDouble 18 8:A 9:B | ||
5 5 5 1 1 1 5 1 1 1 10 0:0 | ||
5 5 5 4 4 5 5 4 4 5 10 0:0 | ||
3 3 3 1 1 1 3 1 1 1 10 0:0 | ||
6 6 6 8 8 1 6 8 8 1 10 0:0 |
17 changes: 17 additions & 0 deletions
17
test/BaselineOutput/SingleRelease/NAIndicator/featurized.tsv
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#@ TextLoader{ | ||
#@ header+ | ||
#@ sep=tab | ||
#@ col=ScalarFloat:R4:0 | ||
#@ col=ScalarDouble:R8:1 | ||
#@ col=VectorFloat:R4:2-5 | ||
#@ col=VectorDoulbe:R8:6-9 | ||
#@ col=A:BL:10 | ||
#@ col=B:BL:11 | ||
#@ col=C:BL:12-15 | ||
#@ col=D:BL:16-19 | ||
#@ } | ||
ScalarFloat ScalarDouble 18 8:A 9:B | ||
5 5 5 1 1 1 5 1 1 1 10 0:0 | ||
5 5 5 4 4 5 5 4 4 5 10 0:0 | ||
3 3 3 1 1 1 3 1 1 1 10 0:0 | ||
6 6 6 8 8 1 6 8 8 1 10 0:0 |
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
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
148 changes: 148 additions & 0 deletions
148
test/Microsoft.ML.Tests/Transformers/NAIndicatorTests.cs
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 |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// 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 Microsoft.ML.Runtime.Api; | ||
using Microsoft.ML.Runtime.Data; | ||
using Microsoft.ML.Runtime.Data.IO; | ||
using Microsoft.ML.Runtime.Model; | ||
using Microsoft.ML.Runtime.RunTests; | ||
using Microsoft.ML.Runtime.Tools; | ||
using Microsoft.ML.Transforms; | ||
using System; | ||
using System.IO; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Microsoft.ML.Tests.Transformers | ||
{ | ||
public class NAIndicatorTests : TestDataPipeBase | ||
{ | ||
private class TestClass | ||
{ | ||
public float A; | ||
public double B; | ||
[VectorType(2)] | ||
public float[] C; | ||
[VectorType(2)] | ||
public double[] D; | ||
} | ||
|
||
public NAIndicatorTests(ITestOutputHelper output) : base(output) | ||
{ | ||
} | ||
|
||
[Fact] | ||
public void NAIndicatorWorkout() | ||
{ | ||
var data = new[] { | ||
new TestClass() { A = 1, B = 3, C = new float[2]{ 1, 2 } , D = new double[2]{ 3,4} }, | ||
new TestClass() { A = float.NaN, B = double.NaN, C = new float[2]{ float.NaN, float.NaN } , D = new double[2]{ double.NaN,double.NaN}}, | ||
new TestClass() { A = float.NegativeInfinity, B = double.NegativeInfinity, C = new float[2]{ float.NegativeInfinity, float.NegativeInfinity } , D = new double[2]{ double.NegativeInfinity, double.NegativeInfinity}}, | ||
new TestClass() { A = float.PositiveInfinity, B = double.PositiveInfinity, C = new float[2]{ float.PositiveInfinity, float.PositiveInfinity, } , D = new double[2]{ double.PositiveInfinity, double.PositiveInfinity}}, | ||
new TestClass() { A = 2, B = 1, C = new float[2]{ 3, 4 } , D = new double[2]{ 5,6}}, | ||
}; | ||
|
||
var dataView = ComponentCreation.CreateDataView(Env, data); | ||
var pipe = new NAIndicatorEstimator(Env, | ||
new (string input, string output)[] { ("A", "NAA"), ("B", "NAB"), ("C", "NAC"), ("D", "NAD") }); | ||
TestEstimatorCore(pipe, dataView); | ||
Done(); | ||
} | ||
|
||
[Fact] | ||
public void TestCommandLine() | ||
{ | ||
Assert.Equal(Maml.Main(new[] { @"showschema loader=Text{col=A:R4:0} xf=NAIndicator{col=B:A} in=f:\2.txt" }), (int)0); | ||
} | ||
|
||
[Fact] | ||
public void TestOldSavingAndLoading() | ||
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. Can you add small metadata test? #Closed |
||
{ | ||
var data = new[] { | ||
new TestClass() { A = 1, B = 3, C = new float[2]{ 1, 2 } , D = new double[2]{ 3,4} }, | ||
new TestClass() { A = float.NaN, B = double.NaN, C = new float[2]{ float.NaN, float.NaN } , D = new double[2]{ double.NaN,double.NaN}}, | ||
new TestClass() { A = float.NegativeInfinity, B = double.NegativeInfinity, C = new float[2]{ float.NegativeInfinity, float.NegativeInfinity } , D = new double[2]{ double.NegativeInfinity, double.NegativeInfinity}}, | ||
new TestClass() { A = float.PositiveInfinity, B = double.PositiveInfinity, C = new float[2]{ float.PositiveInfinity, float.PositiveInfinity, } , D = new double[2]{ double.PositiveInfinity, double.PositiveInfinity}}, | ||
new TestClass() { A = 2, B = 1 , C = new float[2]{ 3, 4 } , D = new double[2]{ 5,6}}, | ||
}; | ||
|
||
var dataView = ComponentCreation.CreateDataView(Env, data); | ||
var pipe = new NAIndicatorEstimator(Env, | ||
new (string input, string output)[] { ("A", "NAA"), ("B", "NAB"), ("C", "NAC"), ("D", "NAD") }); | ||
var result = pipe.Fit(dataView).Transform(dataView); | ||
var resultRoles = new RoleMappedData(result); | ||
using (var ms = new MemoryStream()) | ||
{ | ||
TrainUtils.SaveModel(Env, Env.Start("saving"), ms, null, resultRoles); | ||
ms.Position = 0; | ||
var loadedView = ModelFileUtils.LoadTransforms(Env, dataView, ms); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void NAIndicatorFileOutput() | ||
{ | ||
string dataPath = GetDataPath("breast-cancer.txt"); | ||
var reader = TextLoader.CreateReader(Env, ctx => ( | ||
ScalarFloat: ctx.LoadFloat(1), | ||
ScalarDouble: ctx.LoadDouble(1), | ||
VectorFloat: ctx.LoadFloat(1, 4), | ||
VectorDoulbe: ctx.LoadDouble(1, 4) | ||
)); | ||
|
||
var data = reader.Read(new MultiFileSource(dataPath)).AsDynamic; | ||
var wrongCollection = new[] { new TestClass() { A = 1, B = 3, C = new float[2] { 1, 2 }, D = new double[2] { 3, 4 } } }; | ||
var invalidData = ComponentCreation.CreateDataView(Env, wrongCollection); | ||
var est = new NAIndicatorEstimator(Env, | ||
new (string input, string output)[] { ("ScalarFloat", "A"), ("ScalarDouble", "B"), ("VectorFloat", "C"), ("VectorDoulbe", "D") }); | ||
|
||
TestEstimatorCore(est, data, invalidInput: invalidData); | ||
var outputPath = GetOutputPath("NAIndicator", "featurized.tsv"); | ||
using (var ch = Env.Start("save")) | ||
{ | ||
var saver = new TextSaver(Env, new TextSaver.Arguments { Silent = true }); | ||
IDataView savedData = TakeFilter.Create(Env, est.Fit(data).Transform(data), 4); | ||
using (var fs = File.Create(outputPath)) | ||
DataSaverUtils.SaveDataView(ch, saver, savedData, fs, keepHidden: true); | ||
} | ||
|
||
CheckEquality("NAIndicator", "featurized.tsv"); | ||
Done(); | ||
} | ||
|
||
[Fact] | ||
public void NAIndicatorMetadataTest() | ||
{ | ||
var data = new[] { | ||
new TestClass() { A = 1, B = 3, C = new float[2]{ 1, 2 } , D = new double[2]{ 3,4} }, | ||
new TestClass() { A = float.NaN, B = double.NaN, C = new float[2]{ float.NaN, float.NaN } , D = new double[2]{ double.NaN,double.NaN}}, | ||
new TestClass() { A = float.NegativeInfinity, B = double.NegativeInfinity, C = new float[2]{ float.NegativeInfinity, float.NegativeInfinity } , D = new double[2]{ double.NegativeInfinity, double.NegativeInfinity}}, | ||
new TestClass() { A = float.PositiveInfinity, B = double.PositiveInfinity, C = new float[2]{ float.PositiveInfinity, float.PositiveInfinity, } , D = new double[2]{ double.PositiveInfinity, double.PositiveInfinity}}, | ||
new TestClass() { A = 2, B = 1, C = new float[2]{ 3, 4 } , D = new double[2]{ 5,6}}, | ||
}; | ||
|
||
var dataView = ComponentCreation.CreateDataView(Env, data); | ||
var pipe = new CategoricalEstimator(Env, new CategoricalEstimator.ColumnInfo("A", "CatA")); | ||
var newpipe = pipe.Append(new NAIndicatorEstimator(Env, new (string input, string output)[] { ("CatA", "NAA") })); | ||
var result = newpipe.Fit(dataView).Transform(dataView); | ||
Assert.True(result.Schema.TryGetColumnIndex("NAA", out var col)); | ||
// Check that the column is normalized. | ||
Assert.True(result.Schema.IsNormalized(col)); | ||
// Check that slot names metadata was correctly created. | ||
var value = new VBuffer<ReadOnlyMemory<char>>(); | ||
var type = result.Schema.GetMetadataTypeOrNull(MetadataUtils.Kinds.SlotNames, col); | ||
result.Schema.GetMetadata(MetadataUtils.Kinds.SlotNames, col, ref value); | ||
Assert.True(value.Length == 4); | ||
var mem = new ReadOnlyMemory<char>(); | ||
value.GetItemOrDefault(0, ref mem); | ||
Assert.True(mem.ToString() == "1"); | ||
value.GetItemOrDefault(1, ref mem); | ||
Assert.True(mem.ToString() == "-Infinity"); | ||
value.GetItemOrDefault(2, ref mem); | ||
Assert.True(mem.ToString() == "Infinity"); | ||
value.GetItemOrDefault(3, ref mem); | ||
Assert.True(mem.ToString() == "2"); | ||
} | ||
} | ||
} |
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.
nice catch :) #Resolved