Skip to content

Conversion of prior and random trainers to estimators #876

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 20 commits into from
Sep 19, 2018

Conversation

artidoro
Copy link
Contributor

@artidoro artidoro commented Sep 10, 2018

Fixes #875.

Converted prior and random trainers to estimators.
Allowed feature column to be null for both prior and random estimator.

@artidoro artidoro requested a review from Zruty0 September 10, 2018 22:17
}

public override PredictionKind PredictionKind => PredictionKind.BinaryClassification;

private static readonly TrainerInfo _info = new TrainerInfo(normalization: false, caching: false);
public override TrainerInfo Info => _info;

public RandomTrainer(IHostEnvironment env, Arguments args)
Copy link
Contributor

@Zruty0 Zruty0 Sep 10, 2018

Choose a reason for hiding this comment

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

RandomTrainer [](start = 15, length = 13)

You should not remove this constructor. Once you start writing the test you will discover that it is necessary.

Add a second constructor instead. Both here and for Prior #Closed

@@ -38,38 +39,37 @@ namespace Microsoft.ML.Runtime.Learners
/// <summary>
/// A trainer that trains a predictor that returns random values
/// </summary>
public sealed class RandomTrainer : TrainerBase<RandomPredictor>

public sealed class RandomTrainer : TrainerEstimatorBase<BinaryPredictionTransformer<RandomPredictor>, RandomPredictor>
Copy link
Contributor

@Zruty0 Zruty0 Sep 10, 2018

Choose a reason for hiding this comment

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

TrainerEstimatorBase [](start = 40, length = 20)

It doesn't help you to derive from this base class, and it also hurts: you now have to take label, feature and weight columns, but you don't actually need them at all.

So, don't derive from TrainerEstimatorBase and just implement ITrainerEstimator #Closed

@Zruty0
Copy link
Contributor

Zruty0 commented Sep 10, 2018

using Float = System.Single;

remove this line and replace all usages of Float with float #Closed


Refers to: src/Microsoft.ML.StandardLearners/Standard/Simple/SimpleTrainers.cs:5 in dda393a. [](commit_id = dda393a, deletion_comment = False)

@Zruty0
Copy link
Contributor

Zruty0 commented Sep 10, 2018

    private readonly Random _random;

oh look, a System.Random. We probably want to get rid of this in favor of TauswortheHybrid. Check RandomUtils.Create #Closed


Refers to: src/Microsoft.ML.StandardLearners/Standard/Simple/SimpleTrainers.cs:99 in dda393a. [](commit_id = dda393a, deletion_comment = False)

public RandomTrainer(IHostEnvironment env, Arguments args)
: base(env, LoadNameValue)
protected override SchemaShape.Column[] OutputColumns => throw new NotImplementedException();

Copy link
Contributor

@Zruty0 Zruty0 Sep 10, 2018

Choose a reason for hiding this comment

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

That is obviously not sufficient. You need to list the columns that you are going to output. #Closed

Copy link
Contributor

Choose a reason for hiding this comment

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

Same goes for the other trainer


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

Copy link
Contributor

@Zruty0 Zruty0 left a comment

Choose a reason for hiding this comment

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

🕐

@artidoro artidoro changed the title WIP: Convertion of prior and random trainers to estimators WIP: Conversion of prior and random trainers to estimators Sep 11, 2018

RoleMappedData trainRoles = new RoleMappedData(cachedTrain);
var pred = Train(new TrainContext(trainRoles));
return new BinaryPredictionTransformer<RandomPredictor>(Host, pred, cachedTrain.Schema, trainRoles.Schema.Feature.Name);
Copy link
Contributor

@Zruty0 Zruty0 Sep 11, 2018

Choose a reason for hiding this comment

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

trainRoles.Schema.Feature.Name [](start = 100, length = 30)

this will make features a required column, which we don't want #Closed

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's follow through and formally say that feature column is also optional in PredictionTransformerBase: right now it is not


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

public override RandomPredictor Train(TrainContext context)
{
Host.CheckValue(context, nameof(context));
return new RandomPredictor(Host, Host.Rand.Next());
}

/// <summary>
Copy link
Contributor

@Zruty0 Zruty0 Sep 11, 2018

Choose a reason for hiding this comment

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

summary [](start = 13, length = 7)

this is not a good comment.
A good comment has to describe the purpose of the method, and not its implementation.

In this particular case, I think, it's not necessary to have a summary comment at all, given that you're just implementing the interface, and the method is commented there. #Closed

@Zruty0
Copy link
Contributor

Zruty0 commented Sep 11, 2018

Still waiting for tests. #Closed

@@ -91,12 +91,36 @@ public IList<TestDataset> GetDatasetsForClassificationWeightingPredictorsTest()
[TestCategory("Binary")]
public void BinaryClassifierPerceptronTest()
{
var binaryPredictors = new[] { TestLearners.perceptron };
var binaryPredictors = new[] { TestLearners.perceptron};
Copy link
Contributor

@Zruty0 Zruty0 Sep 12, 2018

Choose a reason for hiding this comment

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

on}; [](start = 64, length = 4)

undo this change #Closed

***** Unexpected failure. Please refer to https://aka.ms/MLNetIssue to file an issue with details *****
***** Error log has been saved to '%Temp%\%ErrorLog%', please refer to https://aka.ms/MLNetIssue to file an issue with details *****
===== Begin detailed dump =====
(1) Unexpected exception: Object reference not set to an instance of an object., 'System.NullReferenceException'
Copy link
Contributor

@Zruty0 Zruty0 Sep 12, 2018

Choose a reason for hiding this comment

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

Unexpected exception [](start = 4, length = 20)

this is not a correct behavior #Closed

***** Unexpected failure. Please refer to https://aka.ms/MLNetIssue to file an issue with details *****
***** Error log has been saved to '%Temp%\%ErrorLog%', please refer to https://aka.ms/MLNetIssue to file an issue with details *****
===== Begin detailed dump =====
(1) Unexpected exception: Could not find file '%Output% 'System.IO.FileNotFoundException'
Copy link
Contributor

@Zruty0 Zruty0 Sep 12, 2018

Choose a reason for hiding this comment

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

Unexpected exception [](start = 3, length = 21)

make sure your code doesn't throw exceptions #Closed

@@ -96,7 +112,7 @@ private static VersionInfo GetVersionInfo()
// Keep all the serializable state here.
private readonly int _seed;
private readonly object _instanceLock;
private readonly Random _random;
private readonly TauswortheHybrid _random;
Copy link
Contributor

@Zruty0 Zruty0 Sep 12, 2018

Choose a reason for hiding this comment

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

TauswortheHybrid [](start = 25, length = 16)

would IRandom suffice here? #Closed

@@ -36,17 +36,18 @@ public abstract class PredictionTransformerBase<TModel> : IPredictionTransformer

public TModel Model { get; }

public PredictionTransformerBase(IHost host, TModel model, ISchema trainSchema, string featureColumn)
public PredictionTransformerBase(IHost host, TModel model, ISchema trainSchema, string featureColumn = null)
Copy link
Contributor

@Zruty0 Zruty0 Sep 13, 2018

Choose a reason for hiding this comment

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

= null [](start = 108, length = 7)

nope, that should still be required param. Just 'null' should now be acceptable. #Closed

Host = host;
Host.CheckValue(trainSchema, nameof(trainSchema));

Model = model;
FeatureColumn = featureColumn;
if (!trainSchema.TryGetColumnIndex(featureColumn, out int col))
if (!trainSchema.TryGetColumnIndex(featureColumn, out int col) && (featureColumn != null))
Copy link
Contributor

@Zruty0 Zruty0 Sep 13, 2018

Choose a reason for hiding this comment

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

&& [](start = 75, length = 2)

swap the 2 conditions or you get a wrong error :) #Closed

@@ -13,6 +11,8 @@
using Microsoft.ML.Runtime.Model;
using Microsoft.ML.Runtime.Training;
using Microsoft.ML.Runtime.Internal.Internallearn;
using Microsoft.ML.Core.Data;
using System.Linq;
Copy link
Contributor

@Zruty0 Zruty0 Sep 13, 2018

Choose a reason for hiding this comment

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

System [](start = 6, length = 6)

sort namespaces #Closed

@Zruty0
Copy link
Contributor

Zruty0 commented Sep 13, 2018

Now waiting for test containing TestEstimatorCore


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

@@ -211,12 +236,16 @@ public sealed class Arguments
public override TrainerInfo Info => _info;

public PriorTrainer(IHostEnvironment env, Arguments args)
Copy link
Member

@sfilipi sfilipi Sep 17, 2018

Choose a reason for hiding this comment

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

public [](start = 8, length = 6)

private #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.

Why should I make the constructor private?


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

@@ -211,12 +236,16 @@ public sealed class Arguments
public override TrainerInfo Info => _info;

public PriorTrainer(IHostEnvironment env, Arguments args)
: base(env, LoadNameValue)
: base(Contracts.CheckRef(env, nameof(env)).Register(LoadNameValue), MakeFeatureColumn(DefaultColumnNames.Features), MakeLabelColumn(DefaultColumnNames.Label), null)
{
Copy link
Member

@sfilipi sfilipi Sep 17, 2018

Choose a reason for hiding this comment

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

{ [](start = 8, length = 1)

keep the args check. #Resolved

@artidoro
Copy link
Contributor Author

artidoro commented Sep 17, 2018

    public RandomTrainer(IHostEnvironment env, Arguments args)

Same question as below: Why should this be private? Isn't this the only constructor of RandomTrainer?


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


Refers to: src/Microsoft.ML.StandardLearners/Standard/Simple/SimpleTrainers.cs:58 in 3dcb705. [](commit_id = 3dcb705, deletion_comment = False)

@Zruty0
Copy link
Contributor

Zruty0 commented Sep 17, 2018

    public RandomTrainer(IHostEnvironment env, Arguments args)

you should actually have a parameterless constructor, and this one needs to be present just for the component catalog, and it can be made private now.


In reply to: 422106631 [](ancestors = 422106631,422096175)


Refers to: src/Microsoft.ML.StandardLearners/Standard/Simple/SimpleTrainers.cs:58 in 3dcb705. [](commit_id = 3dcb705, deletion_comment = False)

[Fact]
public void TestEstimatorPrior()
{
using (var env = new TlcEnvironment())
Copy link
Contributor

@Ivanidzo4ka Ivanidzo4ka Sep 17, 2018

Choose a reason for hiding this comment

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

env [](start = 23, length = 3)

Use baseclass Env, no need to create another one. #Resolved

{
}

public PriorTrainer(IHost host, SchemaShape.Column feature, SchemaShape.Column label, SchemaShape.Column weight)
Copy link
Contributor

@Ivanidzo4ka Ivanidzo4ka Sep 17, 2018

Choose a reason for hiding this comment

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

public [](start = 8, length = 6)

Can it be private?
I understand what you use TrainerEstimatorBase which requires feature column, but for this class it's pointless, and exposing it to user would be potentially confusing. #Resolved

Float lab = default(Float);
Float weight = 1;
var getWeight = colWeight >= 0 ? cursor.GetGetter<float>(colWeight) : null;
float lab = default(float);
Copy link
Contributor

@Ivanidzo4ka Ivanidzo4ka Sep 17, 2018

Choose a reason for hiding this comment

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

float [](start = 36, length = 5)

you can omit it. #Resolved

Copy link
Contributor

@Ivanidzo4ka Ivanidzo4ka left a comment

Choose a reason for hiding this comment

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

:shipit:

/// </summary>
/// <param name="host"></param>
/// <param name="feature"></param>
/// <param name="label"></param>
Copy link
Contributor

@Ivanidzo4ka Ivanidzo4ka Sep 17, 2018

Choose a reason for hiding this comment

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

either omit, or put something into them #Resolved

{
dst = PredictCore();
}

private void MapDist(ref VBuffer<Float> src, ref Float score, ref Float prob)
private void MapDist(ref VBuffer<float> src, ref float score, ref float prob)
{
score = PredictCore();
prob = (score + 1) / 2;
}
}

// Learns the prior distribution for 0/1 class labels and just outputs that.
Copy link
Contributor

@Ivanidzo4ka Ivanidzo4ka Sep 17, 2018

Choose a reason for hiding this comment

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

// [](start = 4, length = 2)

Make it ///

#Resolved

@@ -534,7 +534,7 @@ public RoleMappedData(IDataView data, IEnumerable<KeyValuePair<RoleMappedSchema.
/// <param name="opt">Whether to consider the column names specified "optional" or not. If <c>false</c> then any non-empty
/// values for the column names that does not appear in <paramref name="data"/>'s schema will result in an exception being thrown,
/// but if <c>true</c> such values will be ignored</param>
public RoleMappedData(IDataView data, string label, string feature,
public RoleMappedData(IDataView data, string label, string feature = null,
Copy link
Contributor

@Zruty0 Zruty0 Sep 17, 2018

Choose a reason for hiding this comment

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

= null [](start = 74, length = 7)

undo this #Resolved

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 will handle this in my code then! Sorry about that


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

/// <summary>
/// Initializes PriorTrainer object.
/// </summary>
public PriorTrainer(IHost host, SchemaShape.Column label, SchemaShape.Column weight)
Copy link
Contributor

@Zruty0 Zruty0 Sep 17, 2018

Choose a reason for hiding this comment

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

SchemaShape.Column label [](start = 40, length = 24)

The constructor signature should be
PriorTrainer(IHostEnvironment env, string labelColumn, string weightColumn = null) #Resolved

Copy link
Contributor Author

@artidoro artidoro Sep 17, 2018

Choose a reason for hiding this comment

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

Ok, I made those Strings instead of SchemShape.Column.
Thank you!


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


private static SchemaShape.Column MakeLabelColumn(string labelColumn)
=> new SchemaShape.Column(labelColumn, SchemaShape.Column.VectorKind.Scalar, NumberType.R4, false);

Copy link
Member

@sfilipi sfilipi Sep 17, 2018

Choose a reason for hiding this comment

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

don't think you're using this #Resolved

Copy link
Member

@sfilipi sfilipi left a comment

Choose a reason for hiding this comment

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

:shipit:

Copy link
Member

@sfilipi sfilipi left a comment

Choose a reason for hiding this comment

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

:shipit:

var pipe = new RandomTrainer(Env);

// Test only that the schema propagation works.
// REVIEW: the save/load is not preserving the full state of the random predictor. This is unfortunate, but we don't care too much at this point.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it still the case?
If yes, can you write down what "full" state are we missing?

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 comment was written by Pete. I don't know exactly what "full state" refers to. I am going to search more in depth to understand what is causing the problem and file a separate issue if it is needed. The behavior that we observed is that the data produced by the model that was saved + loaded back is different from the data produced by the original model.


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

Copy link
Contributor

@Ivanidzo4ka Ivanidzo4ka left a comment

Choose a reason for hiding this comment

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

:shipit:

@Zruty0 Zruty0 merged commit 7b1c7d7 into dotnet:master Sep 19, 2018
@artidoro artidoro deleted the estimators branch January 5, 2019 00:02
@ghost ghost locked as resolved and limited conversation to collaborators Mar 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants