Skip to content
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

File-scoped namespaces in files under Prediction (Microsoft.ML.Core) #6792

Merged
merged 3 commits into from
Sep 1, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
99 changes: 49 additions & 50 deletions src/Microsoft.ML.Core/Prediction/IPredictor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,61 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace Microsoft.ML
namespace Microsoft.ML;

/// <summary>
/// Type of prediction task. Note that this is a legacy structure and usage of this should generally be
/// discouraged in future projects. Its presence suggests that there are privileged and supported
/// tasks, and anything outside of this is unsupported. This runs rather contrary to the idea of this
/// being an expandable framework, and it is inappropriately limiting. For legacy pipelines based on
/// <see cref="ITrainer"/> and <see cref="IPredictor"/> it is still useful, but for things based on
/// the <see cref="IEstimator{TTransformer}"/> idiom, it is inappropriate.
/// </summary>
[BestFriend]
internal enum PredictionKind
{
/// <summary>
/// Type of prediction task. Note that this is a legacy structure and usage of this should generally be
/// discouraged in future projects. Its presence suggests that there are privileged and supported
/// tasks, and anything outside of this is unsupported. This runs rather contrary to the idea of this
/// being an expandable framework, and it is inappropriately limiting. For legacy pipelines based on
/// <see cref="ITrainer"/> and <see cref="IPredictor"/> it is still useful, but for things based on
/// the <see cref="IEstimator{TTransformer}"/> idiom, it is inappropriate.
/// </summary>
[BestFriend]
internal enum PredictionKind
{
Unknown = 0,
Custom = 1,
Unknown = 0,
Custom = 1,

BinaryClassification = 2,
MulticlassClassification = 3,
Regression = 4,
MultiOutputRegression = 5,
Ranking = 6,
Recommendation = 7,
AnomalyDetection = 8,
Clustering = 9,
SequenceClassification = 10,
BinaryClassification = 2,
MulticlassClassification = 3,
Regression = 4,
MultiOutputRegression = 5,
Ranking = 6,
Recommendation = 7,
AnomalyDetection = 8,
Clustering = 9,
SequenceClassification = 10,

// More to be added later.
}
// More to be added later.
}

/// <summary>
/// Weakly typed version of IPredictor.
/// </summary>
[BestFriend]
internal interface IPredictor
{
/// <summary>
/// Weakly typed version of IPredictor.
/// Return the type of prediction task.
/// </summary>
[BestFriend]
internal interface IPredictor
{
/// <summary>
/// Return the type of prediction task.
/// </summary>
PredictionKind PredictionKind { get; }
}
PredictionKind PredictionKind { get; }
}

/// <summary>
/// A predictor the produces values of the indicated type.
/// REVIEW: Determine whether this is just a temporary shim or long term solution.
/// </summary>
[BestFriend]
internal interface IPredictorProducing<out TResult> : IPredictor
{
}
/// <summary>
/// A predictor the produces values of the indicated type.
/// REVIEW: Determine whether this is just a temporary shim or long term solution.
/// </summary>
[BestFriend]
internal interface IPredictorProducing<out TResult> : IPredictor
{
}

/// <summary>
/// A predictor that produces values and distributions of the indicated types.
/// Note that from a public API perspective this is bad.
/// </summary>
[BestFriend]
internal interface IDistPredictorProducing<out TResult, out TResultDistribution> : IPredictorProducing<TResult>
{
}
/// <summary>
/// A predictor that produces values and distributions of the indicated types.
/// Note that from a public API perspective this is bad.
/// </summary>
[BestFriend]
internal interface IDistPredictorProducing<out TResult, out TResultDistribution> : IPredictorProducing<TResult>
{
}
175 changes: 87 additions & 88 deletions src/Microsoft.ML.Core/Prediction/ITrainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,104 +4,103 @@

using Microsoft.ML.Data;

namespace Microsoft.ML
{
// REVIEW: Would be nice if the registration under SignatureTrainer were automatic
// given registration for one of the "sub-class" signatures.
namespace Microsoft.ML;

/// <summary>
/// Loadable class signatures for trainers. Typically each trainer should register with
/// both SignatureTrainer and SignatureXxxTrainer where Xxx is the prediction kind.
/// </summary>
[BestFriend]
internal delegate void SignatureTrainer();
// REVIEW: Would be nice if the registration under SignatureTrainer were automatic
// given registration for one of the "sub-class" signatures.

[BestFriend]
internal delegate void SignatureBinaryClassifierTrainer();
[BestFriend]
internal delegate void SignatureMulticlassClassifierTrainer();
[BestFriend]
internal delegate void SignatureRegressorTrainer();
[BestFriend]
internal delegate void SignatureMultiOutputRegressorTrainer();
[BestFriend]
internal delegate void SignatureRankerTrainer();
[BestFriend]
internal delegate void SignatureAnomalyDetectorTrainer();
[BestFriend]
internal delegate void SignatureClusteringTrainer();
[BestFriend]
internal delegate void SignatureSequenceTrainer();
[BestFriend]
internal delegate void SignatureMatrixRecommendingTrainer();
/// <summary>
/// Loadable class signatures for trainers. Typically each trainer should register with
/// both SignatureTrainer and SignatureXxxTrainer where Xxx is the prediction kind.
/// </summary>
[BestFriend]
internal delegate void SignatureTrainer();

[BestFriend]
internal delegate void SignatureBinaryClassifierTrainer();
[BestFriend]
internal delegate void SignatureMulticlassClassifierTrainer();
[BestFriend]
internal delegate void SignatureRegressorTrainer();
[BestFriend]
internal delegate void SignatureMultiOutputRegressorTrainer();
[BestFriend]
internal delegate void SignatureRankerTrainer();
[BestFriend]
internal delegate void SignatureAnomalyDetectorTrainer();
[BestFriend]
internal delegate void SignatureClusteringTrainer();
[BestFriend]
internal delegate void SignatureSequenceTrainer();
[BestFriend]
internal delegate void SignatureMatrixRecommendingTrainer();

/// <summary>
/// The base interface for a trainers. Implementors should not implement this interface directly,
/// but rather implement the more specific <see cref="ITrainer{TPredictor}"/>.
/// </summary>
[BestFriend]
internal interface ITrainer
{
/// <summary>
/// The base interface for a trainers. Implementors should not implement this interface directly,
/// but rather implement the more specific <see cref="ITrainer{TPredictor}"/>.
/// Auxiliary information about the trainer in terms of its capabilities
/// and requirements.
/// </summary>
[BestFriend]
internal interface ITrainer
{
/// <summary>
/// Auxiliary information about the trainer in terms of its capabilities
/// and requirements.
/// </summary>
TrainerInfo Info { get; }
TrainerInfo Info { get; }

/// <summary>
/// Return the type of prediction task for the produced predictor.
/// </summary>
PredictionKind PredictionKind { get; }
/// <summary>
/// Return the type of prediction task for the produced predictor.
/// </summary>
PredictionKind PredictionKind { get; }

/// <summary>
/// Trains a predictor.
/// </summary>
/// <param name="context">A context containing at least the training data</param>
/// <returns>The trained predictor</returns>
/// <seealso cref="ITrainer{TPredictor}.Train(TrainContext)"/>
IPredictor Train(TrainContext context);
}
/// <summary>
/// Trains a predictor.
/// </summary>
/// <param name="context">A context containing at least the training data</param>
/// <returns>The trained predictor</returns>
/// <seealso cref="ITrainer{TPredictor}.Train(TrainContext)"/>
IPredictor Train(TrainContext context);
}

/// <summary>
/// Strongly typed generic interface for a trainer. A trainer object takes training data
/// and produces a predictor.
/// </summary>
/// <typeparam name="TPredictor"> Type of predictor produced</typeparam>
[BestFriend]
internal interface ITrainer<out TPredictor> : ITrainer
where TPredictor : IPredictor
{
/// <summary>
/// Strongly typed generic interface for a trainer. A trainer object takes training data
/// and produces a predictor.
/// Trains a predictor.
/// </summary>
/// <typeparam name="TPredictor"> Type of predictor produced</typeparam>
[BestFriend]
internal interface ITrainer<out TPredictor> : ITrainer
where TPredictor : IPredictor
{
/// <summary>
/// Trains a predictor.
/// </summary>
/// <param name="context">A context containing at least the training data</param>
/// <returns>The trained predictor</returns>
new TPredictor Train(TrainContext context);
}
/// <param name="context">A context containing at least the training data</param>
/// <returns>The trained predictor</returns>
new TPredictor Train(TrainContext context);
}

[BestFriend]
internal static class TrainerExtensions
{
/// <summary>
/// Convenience train extension for the case where one has only a training set with no auxiliary information.
/// Equivalent to calling <see cref="ITrainer.Train(TrainContext)"/>
/// on a <see cref="TrainContext"/> constructed with <paramref name="trainData"/>.
/// </summary>
/// <param name="trainer">The trainer</param>
/// <param name="trainData">The training data.</param>
/// <returns>The trained predictor</returns>
public static IPredictor Train(this ITrainer trainer, RoleMappedData trainData)
=> trainer.Train(new TrainContext(trainData));
[BestFriend]
internal static class TrainerExtensions
{
/// <summary>
/// Convenience train extension for the case where one has only a training set with no auxiliary information.
/// Equivalent to calling <see cref="ITrainer.Train(TrainContext)"/>
/// on a <see cref="TrainContext"/> constructed with <paramref name="trainData"/>.
/// </summary>
/// <param name="trainer">The trainer</param>
/// <param name="trainData">The training data.</param>
/// <returns>The trained predictor</returns>
public static IPredictor Train(this ITrainer trainer, RoleMappedData trainData)
=> trainer.Train(new TrainContext(trainData));

/// <summary>
/// Convenience train extension for the case where one has only a training set with no auxiliary information.
/// Equivalent to calling <see cref="ITrainer{TPredictor}.Train(TrainContext)"/>
/// on a <see cref="TrainContext"/> constructed with <paramref name="trainData"/>.
/// </summary>
/// <param name="trainer">The trainer</param>
/// <param name="trainData">The training data.</param>
/// <returns>The trained predictor</returns>
public static TPredictor Train<TPredictor>(this ITrainer<TPredictor> trainer, RoleMappedData trainData) where TPredictor : IPredictor
=> trainer.Train(new TrainContext(trainData));
}
/// <summary>
/// Convenience train extension for the case where one has only a training set with no auxiliary information.
/// Equivalent to calling <see cref="ITrainer{TPredictor}.Train(TrainContext)"/>
/// on a <see cref="TrainContext"/> constructed with <paramref name="trainData"/>.
/// </summary>
/// <param name="trainer">The trainer</param>
/// <param name="trainData">The training data.</param>
/// <returns>The trained predictor</returns>
public static TPredictor Train<TPredictor>(this ITrainer<TPredictor> trainer, RoleMappedData trainData) where TPredictor : IPredictor
=> trainer.Train(new TrainContext(trainData));
}