Skip to content

introduce IUnsupervisedLearningWithWeights #236

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
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions src/Microsoft.ML.Data/EntryPoints/InputBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ public abstract class LearnerInputBaseWithWeight : LearnerInputBaseWithLabel
public Optional<string> WeightColumn = Optional<string>.Implicit(DefaultColumnNames.Weight);
}

/// <summary>
/// The base class for all unsupervised learner inputs that support a weight column.
/// </summary>
[TlcModule.EntryPointKind(typeof(CommonInputs.IUnsupervisedTrainerWithWeight))]
public abstract class UnsupervisedLearnerInputBaseWithWeight : LearnerInputBase
{
[Argument(ArgumentType.AtMostOnce, HelpText = "Column to use for example weight", ShortName = "weight", SortOrder = 4, Visibility = ArgumentAttribute.VisibilityType.EntryPointsOnly)]
public Optional<string> WeightColumn = Optional<string>.Implicit(DefaultColumnNames.Weight);
}

/// <summary>
/// The base class for all evaluators inputs.
/// </summary>
Expand Down Expand Up @@ -224,6 +234,14 @@ public interface ITrainerInputWithLabel : ITrainerInput
string LabelColumn { get; }
}

/// <summary>
/// Interface that all API trainer input classes will implement.
/// </summary>
public interface IUnsupervisedTrainerWithWeight : ITrainerInput
{
Optional<string> WeightColumn { get; }
}

/// <summary>
/// Interface that all API trainer input classes will implement.
/// </summary>
Expand Down
7 changes: 4 additions & 3 deletions src/Microsoft.ML.KMeansClustering/KMeansPlusPlusTrainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public enum InitAlgorithm
KMeansParallel = 2
}

public class Arguments : LearnerInputBase
public class Arguments : UnsupervisedLearnerInputBaseWithWeight
{
[Argument(ArgumentType.AtMostOnce, HelpText = "The number of clusters", SortOrder = 50)]
[TGUI(SuggestedSweeps = "5,10,20,40")]
Expand Down Expand Up @@ -162,7 +162,7 @@ private void TrainCore(IChannel ch, RoleMappedData data)
long missingFeatureCount;
long totalTrainingInstances;

var cursorFactory = new FeatureFloatVectorCursor.Factory(data, CursOpt.Features | CursOpt.Id);
var cursorFactory = new FeatureFloatVectorCursor.Factory(data, CursOpt.Features | CursOpt.Id | CursOpt.Weight);
// REVIEW: It would be nice to extract these out into subcomponents in the future. We should
// revisit and even consider breaking these all into individual KMeans-flavored trainers, they
// all produce a valid set of output centroids with various trade-offs in runtime (with perhaps
Expand Down Expand Up @@ -234,7 +234,8 @@ public static CommonOutputs.ClusteringOutput TrainKMeans(IHostEnvironment env, A
EntryPointUtils.CheckInputArgs(host, input);

return LearnerEntryPointsUtils.Train<Arguments, CommonOutputs.ClusteringOutput>(host, input,
() => new KMeansPlusPlusTrainer(host, input));
() => new KMeansPlusPlusTrainer(host, input),
getWeight: () => LearnerEntryPointsUtils.FindColumn(host, input.TrainingData.Schema, input.WeightColumn));
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/Microsoft.ML.PCA/PcaTrainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public sealed class RandomizedPcaTrainer : TrainerBase<RoleMappedData, PcaPredic
internal const string Summary = "This algorithm trains an approximate PCA using Randomized SVD algorithm. "
+ "This PCA can be made into Kernel PCA by using Random Fourier Features transform.";

public class Arguments : LearnerInputBase
public class Arguments : UnsupervisedLearnerInputBaseWithWeight
{
[Argument(ArgumentType.AtMostOnce, HelpText = "The number of components in the PCA", ShortName = "k", SortOrder = 50)]
[TGUI(SuggestedSweeps = "10,20,40,80")]
Expand All @@ -67,9 +67,6 @@ public class Arguments : LearnerInputBase

[Argument(ArgumentType.AtMostOnce, HelpText = "The seed for random number generation", ShortName = "seed")]
public int? Seed;

[Argument(ArgumentType.AtMostOnce, HelpText = "Column to use for example weight", ShortName = "weight", SortOrder = 4, Visibility = ArgumentAttribute.VisibilityType.EntryPointsOnly)]
public Optional<string> WeightColumn = Optional<string>.Implicit(DefaultColumnNames.Weight);
}

private int _dimension;
Expand Down
31 changes: 31 additions & 0 deletions test/BaselineOutput/Common/EntryPoints/core_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9117,6 +9117,18 @@
"IsNullable": false,
"Default": "Features"
},
{
"Name": "WeightColumn",
"Type": "String",
"Desc": "Column to use for example weight",
"Aliases": [
"weight"
],
"Required": false,
"SortOrder": 4.0,
"IsNullable": false,
"Default": "Weight"
},
{
"Name": "NormalizeFeatures",
"Type": {
Expand Down Expand Up @@ -9253,6 +9265,7 @@
}
],
"InputKind": [
"IUnsupervisedTrainerWithWeight",
"ITrainerInput"
],
"OutputKind": [
Expand Down Expand Up @@ -10875,6 +10888,7 @@
}
],
"InputKind": [
"IUnsupervisedTrainerWithWeight",
"ITrainerInput"
],
"OutputKind": [
Expand Down Expand Up @@ -22577,6 +22591,23 @@
"Type": "TransformModel"
}
]
},
{
"Kind": "IUnsupervisedTrainerWithWeight",
"Settings": [
{
"Name": "WeightColumn",
"Type": "String"
},
{
"Name": "TrainingData",
"Type": "DataView"
},
{
"Name": "FeatureColumn",
"Type": "String"
}
]
}
]
}
2 changes: 1 addition & 1 deletion test/Microsoft.ML.Core.Tests/UnitTests/TestEntryPoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1918,7 +1918,7 @@ public void EntryPointTrainTestMacroNoTransformInput()
[Fact]
public void EntryPointKMeans()
{
TestEntryPointRoutine("Train-Tiny-28x28.txt", "Trainers.KMeansPlusPlusClusterer");
TestEntryPointRoutine("Train-Tiny-28x28.txt", "Trainers.KMeansPlusPlusClusterer", "col=Weight:R4:0 col=Features:R4:1-784", ",'InitAlgorithm':'KMeansPlusPlus'");
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a test without a Weight column?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In internal repo

Copy link
Contributor

Choose a reason for hiding this comment

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

Should we move the test to the public repo, or keep it in the internal?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We moving/enabling baseline tests in separate PRs.

}

[Fact]
Expand Down