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 1 commit
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
31 changes: 31 additions & 0 deletions ZBaselines/Common/EntryPoints/core_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8957,6 +8957,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 @@ -9093,6 +9105,7 @@
}
],
"InputKind": [
"IUnsupervisedTrainerWithWeight",
"ITrainerInput"
],
"OutputKind": [
Expand Down Expand Up @@ -10715,6 +10728,7 @@
}
],
"InputKind": [
"IUnsupervisedTrainerWithWeight",
"ITrainerInput"
],
"OutputKind": [
Expand Down Expand Up @@ -22417,6 +22431,23 @@
"Type": "TransformModel"
}
]
},
{
"Kind": "IUnsupervisedTrainerWithWeight",
"Settings": [
{
"Name": "WeightColumn",
"Type": "String"
},
{
"Name": "TrainingData",
"Type": "DataView"
},
{
"Name": "FeatureColumn",
"Type": "String"
}
]
}
]
}
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
5 changes: 3 additions & 2 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 @@ -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