Skip to content

Commit ccf34e3

Browse files
LehontiLehonti Ramos
and
Lehonti Ramos
authored
File-scoped namespaces in files under Prediction (Microsoft.ML.Core) (#6792)
Co-authored-by: Lehonti Ramos <john@doe>
1 parent e6a88c4 commit ccf34e3

File tree

5 files changed

+386
-391
lines changed

5 files changed

+386
-391
lines changed

src/Microsoft.ML.Core/Prediction/IPredictor.cs

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,61 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
namespace Microsoft.ML
5+
namespace Microsoft.ML;
6+
7+
/// <summary>
8+
/// Type of prediction task. Note that this is a legacy structure and usage of this should generally be
9+
/// discouraged in future projects. Its presence suggests that there are privileged and supported
10+
/// tasks, and anything outside of this is unsupported. This runs rather contrary to the idea of this
11+
/// being an expandable framework, and it is inappropriately limiting. For legacy pipelines based on
12+
/// <see cref="ITrainer"/> and <see cref="IPredictor"/> it is still useful, but for things based on
13+
/// the <see cref="IEstimator{TTransformer}"/> idiom, it is inappropriate.
14+
/// </summary>
15+
[BestFriend]
16+
internal enum PredictionKind
617
{
7-
/// <summary>
8-
/// Type of prediction task. Note that this is a legacy structure and usage of this should generally be
9-
/// discouraged in future projects. Its presence suggests that there are privileged and supported
10-
/// tasks, and anything outside of this is unsupported. This runs rather contrary to the idea of this
11-
/// being an expandable framework, and it is inappropriately limiting. For legacy pipelines based on
12-
/// <see cref="ITrainer"/> and <see cref="IPredictor"/> it is still useful, but for things based on
13-
/// the <see cref="IEstimator{TTransformer}"/> idiom, it is inappropriate.
14-
/// </summary>
15-
[BestFriend]
16-
internal enum PredictionKind
17-
{
18-
Unknown = 0,
19-
Custom = 1,
18+
Unknown = 0,
19+
Custom = 1,
2020

21-
BinaryClassification = 2,
22-
MulticlassClassification = 3,
23-
Regression = 4,
24-
MultiOutputRegression = 5,
25-
Ranking = 6,
26-
Recommendation = 7,
27-
AnomalyDetection = 8,
28-
Clustering = 9,
29-
SequenceClassification = 10,
21+
BinaryClassification = 2,
22+
MulticlassClassification = 3,
23+
Regression = 4,
24+
MultiOutputRegression = 5,
25+
Ranking = 6,
26+
Recommendation = 7,
27+
AnomalyDetection = 8,
28+
Clustering = 9,
29+
SequenceClassification = 10,
3030

31-
// More to be added later.
32-
}
31+
// More to be added later.
32+
}
3333

34+
/// <summary>
35+
/// Weakly typed version of IPredictor.
36+
/// </summary>
37+
[BestFriend]
38+
internal interface IPredictor
39+
{
3440
/// <summary>
35-
/// Weakly typed version of IPredictor.
41+
/// Return the type of prediction task.
3642
/// </summary>
37-
[BestFriend]
38-
internal interface IPredictor
39-
{
40-
/// <summary>
41-
/// Return the type of prediction task.
42-
/// </summary>
43-
PredictionKind PredictionKind { get; }
44-
}
43+
PredictionKind PredictionKind { get; }
44+
}
4545

46-
/// <summary>
47-
/// A predictor the produces values of the indicated type.
48-
/// REVIEW: Determine whether this is just a temporary shim or long term solution.
49-
/// </summary>
50-
[BestFriend]
51-
internal interface IPredictorProducing<out TResult> : IPredictor
52-
{
53-
}
46+
/// <summary>
47+
/// A predictor the produces values of the indicated type.
48+
/// REVIEW: Determine whether this is just a temporary shim or long term solution.
49+
/// </summary>
50+
[BestFriend]
51+
internal interface IPredictorProducing<out TResult> : IPredictor
52+
{
53+
}
5454

55-
/// <summary>
56-
/// A predictor that produces values and distributions of the indicated types.
57-
/// Note that from a public API perspective this is bad.
58-
/// </summary>
59-
[BestFriend]
60-
internal interface IDistPredictorProducing<out TResult, out TResultDistribution> : IPredictorProducing<TResult>
61-
{
62-
}
55+
/// <summary>
56+
/// A predictor that produces values and distributions of the indicated types.
57+
/// Note that from a public API perspective this is bad.
58+
/// </summary>
59+
[BestFriend]
60+
internal interface IDistPredictorProducing<out TResult, out TResultDistribution> : IPredictorProducing<TResult>
61+
{
6362
}

src/Microsoft.ML.Core/Prediction/ITrainer.cs

Lines changed: 87 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,104 +4,103 @@
44

55
using Microsoft.ML.Data;
66

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

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

19-
[BestFriend]
20-
internal delegate void SignatureBinaryClassifierTrainer();
21-
[BestFriend]
22-
internal delegate void SignatureMulticlassClassifierTrainer();
23-
[BestFriend]
24-
internal delegate void SignatureRegressorTrainer();
25-
[BestFriend]
26-
internal delegate void SignatureMultiOutputRegressorTrainer();
27-
[BestFriend]
28-
internal delegate void SignatureRankerTrainer();
29-
[BestFriend]
30-
internal delegate void SignatureAnomalyDetectorTrainer();
31-
[BestFriend]
32-
internal delegate void SignatureClusteringTrainer();
33-
[BestFriend]
34-
internal delegate void SignatureSequenceTrainer();
35-
[BestFriend]
36-
internal delegate void SignatureMatrixRecommendingTrainer();
12+
/// <summary>
13+
/// Loadable class signatures for trainers. Typically each trainer should register with
14+
/// both SignatureTrainer and SignatureXxxTrainer where Xxx is the prediction kind.
15+
/// </summary>
16+
[BestFriend]
17+
internal delegate void SignatureTrainer();
3718

19+
[BestFriend]
20+
internal delegate void SignatureBinaryClassifierTrainer();
21+
[BestFriend]
22+
internal delegate void SignatureMulticlassClassifierTrainer();
23+
[BestFriend]
24+
internal delegate void SignatureRegressorTrainer();
25+
[BestFriend]
26+
internal delegate void SignatureMultiOutputRegressorTrainer();
27+
[BestFriend]
28+
internal delegate void SignatureRankerTrainer();
29+
[BestFriend]
30+
internal delegate void SignatureAnomalyDetectorTrainer();
31+
[BestFriend]
32+
internal delegate void SignatureClusteringTrainer();
33+
[BestFriend]
34+
internal delegate void SignatureSequenceTrainer();
35+
[BestFriend]
36+
internal delegate void SignatureMatrixRecommendingTrainer();
37+
38+
/// <summary>
39+
/// The base interface for a trainers. Implementors should not implement this interface directly,
40+
/// but rather implement the more specific <see cref="ITrainer{TPredictor}"/>.
41+
/// </summary>
42+
[BestFriend]
43+
internal interface ITrainer
44+
{
3845
/// <summary>
39-
/// The base interface for a trainers. Implementors should not implement this interface directly,
40-
/// but rather implement the more specific <see cref="ITrainer{TPredictor}"/>.
46+
/// Auxiliary information about the trainer in terms of its capabilities
47+
/// and requirements.
4148
/// </summary>
42-
[BestFriend]
43-
internal interface ITrainer
44-
{
45-
/// <summary>
46-
/// Auxiliary information about the trainer in terms of its capabilities
47-
/// and requirements.
48-
/// </summary>
49-
TrainerInfo Info { get; }
49+
TrainerInfo Info { get; }
5050

51-
/// <summary>
52-
/// Return the type of prediction task for the produced predictor.
53-
/// </summary>
54-
PredictionKind PredictionKind { get; }
51+
/// <summary>
52+
/// Return the type of prediction task for the produced predictor.
53+
/// </summary>
54+
PredictionKind PredictionKind { get; }
5555

56-
/// <summary>
57-
/// Trains a predictor.
58-
/// </summary>
59-
/// <param name="context">A context containing at least the training data</param>
60-
/// <returns>The trained predictor</returns>
61-
/// <seealso cref="ITrainer{TPredictor}.Train(TrainContext)"/>
62-
IPredictor Train(TrainContext context);
63-
}
56+
/// <summary>
57+
/// Trains a predictor.
58+
/// </summary>
59+
/// <param name="context">A context containing at least the training data</param>
60+
/// <returns>The trained predictor</returns>
61+
/// <seealso cref="ITrainer{TPredictor}.Train(TrainContext)"/>
62+
IPredictor Train(TrainContext context);
63+
}
6464

65+
/// <summary>
66+
/// Strongly typed generic interface for a trainer. A trainer object takes training data
67+
/// and produces a predictor.
68+
/// </summary>
69+
/// <typeparam name="TPredictor"> Type of predictor produced</typeparam>
70+
[BestFriend]
71+
internal interface ITrainer<out TPredictor> : ITrainer
72+
where TPredictor : IPredictor
73+
{
6574
/// <summary>
66-
/// Strongly typed generic interface for a trainer. A trainer object takes training data
67-
/// and produces a predictor.
75+
/// Trains a predictor.
6876
/// </summary>
69-
/// <typeparam name="TPredictor"> Type of predictor produced</typeparam>
70-
[BestFriend]
71-
internal interface ITrainer<out TPredictor> : ITrainer
72-
where TPredictor : IPredictor
73-
{
74-
/// <summary>
75-
/// Trains a predictor.
76-
/// </summary>
77-
/// <param name="context">A context containing at least the training data</param>
78-
/// <returns>The trained predictor</returns>
79-
new TPredictor Train(TrainContext context);
80-
}
77+
/// <param name="context">A context containing at least the training data</param>
78+
/// <returns>The trained predictor</returns>
79+
new TPredictor Train(TrainContext context);
80+
}
8181

82-
[BestFriend]
83-
internal static class TrainerExtensions
84-
{
85-
/// <summary>
86-
/// Convenience train extension for the case where one has only a training set with no auxiliary information.
87-
/// Equivalent to calling <see cref="ITrainer.Train(TrainContext)"/>
88-
/// on a <see cref="TrainContext"/> constructed with <paramref name="trainData"/>.
89-
/// </summary>
90-
/// <param name="trainer">The trainer</param>
91-
/// <param name="trainData">The training data.</param>
92-
/// <returns>The trained predictor</returns>
93-
public static IPredictor Train(this ITrainer trainer, RoleMappedData trainData)
94-
=> trainer.Train(new TrainContext(trainData));
82+
[BestFriend]
83+
internal static class TrainerExtensions
84+
{
85+
/// <summary>
86+
/// Convenience train extension for the case where one has only a training set with no auxiliary information.
87+
/// Equivalent to calling <see cref="ITrainer.Train(TrainContext)"/>
88+
/// on a <see cref="TrainContext"/> constructed with <paramref name="trainData"/>.
89+
/// </summary>
90+
/// <param name="trainer">The trainer</param>
91+
/// <param name="trainData">The training data.</param>
92+
/// <returns>The trained predictor</returns>
93+
public static IPredictor Train(this ITrainer trainer, RoleMappedData trainData)
94+
=> trainer.Train(new TrainContext(trainData));
9595

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

0 commit comments

Comments
 (0)