Skip to content

Lockdown Microsoft.ML.StandardLearners public surface #2541

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 3 commits into from
Feb 16, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,12 @@ public sealed class FieldAwareFactorizationMachinePredictionTransformer : Predic
/// <summary>
/// The name of the feature column used by the prediction transformer.
/// </summary>
internal IReadOnlyList<string> FeatureColumns { get; }
private IReadOnlyList<string> FeatureColumns { get; }

/// <summary>
/// The type of the feature columns.
/// </summary>
internal IReadOnlyList<DataViewType> FeatureColumnTypes { get; }
private IReadOnlyList<DataViewType> FeatureColumnTypes { get; }

private readonly string _thresholdColumn;
private readonly float _threshold;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Microsoft.ML.Numeric
/// <param name="gradient">The gradient vector, which must be filled in (its initial contents are undefined)</param>
/// <param name="progress">The progress channel provider that can be used to report calculation progress. Can be null.</param>
/// <returns>The value of the function</returns>
public delegate Float DifferentiableFunction(in VBuffer<Float> input, ref VBuffer<Float> gradient, IProgressChannelProvider progress);
internal delegate Float DifferentiableFunction(in VBuffer<Float> input, ref VBuffer<Float> gradient, IProgressChannelProvider progress);

/// <summary>
/// A delegate for indexed sets of functions with gradients.
Expand All @@ -30,12 +30,12 @@ namespace Microsoft.ML.Numeric
/// <param name="input">The point at which to evaluate the function</param>
/// <param name="gradient">The gradient vector, which must be filled in (its initial contents are undefined)</param>
/// <returns>The value of the function</returns>
public delegate Float IndexedDifferentiableFunction(int index, in VBuffer<Float> input, ref VBuffer<Float> gradient);
internal delegate Float IndexedDifferentiableFunction(int index, in VBuffer<Float> input, ref VBuffer<Float> gradient);

/// <summary>
/// Class to aggregate an indexed differentiable function into a single function, in parallel
/// </summary>
public class DifferentiableFunctionAggregator
internal class DifferentiableFunctionAggregator
{
private readonly IndexedDifferentiableFunction _func;
private readonly int _maxIndex;
Expand Down Expand Up @@ -154,7 +154,7 @@ public Float Eval(in VBuffer<Float> input, ref VBuffer<Float> gradient)
/// May have false negatives if extreme values cause the numeric gradient to be off,
/// for example, if the norm of x is very large, or if the gradient is changing rapidly at x.
/// </remarks>
public static class GradientTester
internal static class GradientTester
{
// approximately u^(1/3), where u is the unit roundoff ~ 1.1e-16.
// the optimal value of eps for the central difference approximation, Nocedal & Wright
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.ML.StandardLearners/Optimizer/L1Optimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft.ML.Numeric
/// If you use this code for published research, please cite
/// Galen Andrew and Jianfeng Gao, "Scalable Training of L1-Regularized Log-Linear Models", ICML 2007
/// </summary>
public sealed class L1Optimizer : Optimizer
internal sealed class L1Optimizer : Optimizer
{
// Biases do not contribute to the L1 norm and are assumed to be at the beginning of the weights.
private readonly int _biasCount;
Expand Down
12 changes: 6 additions & 6 deletions src/Microsoft.ML.StandardLearners/Optimizer/LineSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Microsoft.ML.Numeric
/// <summary>
/// Line search that does not use derivatives
/// </summary>
public interface ILineSearch : IDiffLineSearch
internal interface ILineSearch : IDiffLineSearch
{
/// <summary>
/// Finds a local minimum of the function
Expand All @@ -28,12 +28,12 @@ public interface ILineSearch : IDiffLineSearch
/// <param name="x">Point to evaluate</param>
/// <param name="deriv">Derivative at that point</param>
/// <returns></returns>
public delegate Float DiffFunc1D(Float x, out Float deriv);
internal delegate Float DiffFunc1D(Float x, out Float deriv);

/// <summary>
/// Line search that uses derivatives
/// </summary>
public interface IDiffLineSearch
internal interface IDiffLineSearch
{
/// <summary>
/// Finds a local minimum of the function
Expand All @@ -48,7 +48,7 @@ public interface IDiffLineSearch
/// <summary>
/// Cubic interpolation line search
/// </summary>
public sealed class CubicInterpLineSearch : IDiffLineSearch
internal sealed class CubicInterpLineSearch : IDiffLineSearch
{
private Float _step;
private const Float _minProgress = (Float)0.01;
Expand Down Expand Up @@ -217,7 +217,7 @@ private Float FindMinimum(DiffFunc1D func, Float initValue, Float initDeriv)
/// <summary>
/// Finds local minimum with golden section search.
/// </summary>
public sealed class GoldenSectionSearch : ILineSearch
internal sealed class GoldenSectionSearch : ILineSearch
{
private Float _step;
private static readonly Float _phi = (1 + MathUtils.Sqrt(5)) / 2;
Expand Down Expand Up @@ -396,7 +396,7 @@ private Float FindMinimum(Func<Float, Float> func)
/// <summary>
/// Backtracking line search with Armijo condition
/// </summary>
public sealed class BacktrackingLineSearch : IDiffLineSearch
internal sealed class BacktrackingLineSearch : IDiffLineSearch
{
private Float _step;
private Float _c1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.ML.Numeric
/// <summary>
/// An object which is used to decide whether to stop optimization.
/// </summary>
public interface ITerminationCriterion
internal interface ITerminationCriterion
{
/// <summary>
/// Name appropriate for display to the user.
Expand All @@ -37,7 +37,7 @@ public interface ITerminationCriterion
/// <summary>
/// A wrapper for a termination criterion that checks the gradient at a specified interval
/// </summary>
public sealed class GradientCheckingMonitor : ITerminationCriterion
internal sealed class GradientCheckingMonitor : ITerminationCriterion
{
private const string _checkingMessage = " Checking gradient...";
private readonly ITerminationCriterion _termCrit;
Expand Down Expand Up @@ -104,7 +104,7 @@ public void Reset()
/// <summary>
/// An abstract partial implementation of ITerminationCriterion for those which do not require resetting
/// </summary>
public abstract class StaticTerminationCriterion : ITerminationCriterion
internal abstract class StaticTerminationCriterion : ITerminationCriterion
{
public abstract string FriendlyName { get; }

Expand All @@ -127,7 +127,7 @@ public void Reset() { }
/// <summary>
/// Terminates when the geometrically-weighted average improvement falls below the tolerance
/// </summary>
public sealed class MeanImprovementCriterion : ITerminationCriterion
internal sealed class MeanImprovementCriterion : ITerminationCriterion
{
private readonly Float _tol;
private readonly Float _lambda;
Expand Down Expand Up @@ -190,7 +190,7 @@ public void Reset()
/// <remarks>
/// Inappropriate for functions whose optimal value is non-positive, because of normalization
/// </remarks>
public sealed class MeanRelativeImprovementCriterion : ITerminationCriterion
internal sealed class MeanRelativeImprovementCriterion : ITerminationCriterion
{
private readonly int _n;
private readonly Float _tol;
Expand Down Expand Up @@ -280,7 +280,7 @@ public void Reset()
/// that H > (1 / sigmaSq) * I at all points)
/// Inappropriate for functions whose optimal value is non-positive, because of normalization
/// </remarks>
public sealed class UpperBoundOnDistanceWithL2 : StaticTerminationCriterion
internal sealed class UpperBoundOnDistanceWithL2 : StaticTerminationCriterion
{
private readonly Float _sigmaSq;
private readonly Float _tol;
Expand Down Expand Up @@ -345,7 +345,7 @@ public override string ToString()
/// <remarks>
/// Inappropriate for functions whose optimal value is non-positive, because of normalization
/// </remarks>
public sealed class RelativeNormGradient : StaticTerminationCriterion
internal sealed class RelativeNormGradient : StaticTerminationCriterion
{
private readonly Float _tol;

Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.ML.StandardLearners/Optimizer/Optimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.ML.Numeric
/// <summary>
/// Limited-memory BFGS quasi-Newton optimization routine
/// </summary>
public class Optimizer
internal class Optimizer
{
/// Based on Nocedal and Wright, "Numerical Optimization, Second Edition"

Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.ML.StandardLearners/Optimizer/SgdOptimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ namespace Microsoft.ML.Numeric
/// </summary>
/// <param name="x">Current iterate</param>
/// <returns>True if search should terminate</returns>
public delegate bool DTerminate(in VBuffer<Float> x);
internal delegate bool DTerminate(in VBuffer<Float> x);

/// <summary>
/// Stochastic gradient descent with variations (minibatch, momentum, averaging).
/// </summary>
public sealed class SgdOptimizer
internal sealed class SgdOptimizer
{
private int _batchSize;

Expand Down Expand Up @@ -227,7 +227,7 @@ public void Minimize(DStochasticGradient f, ref VBuffer<Float> initial, ref VBuf
/// <summary>
/// Deterministic gradient descent with line search
/// </summary>
public class GDOptimizer
internal class GDOptimizer
{
/// <summary>
/// Line search to use.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public abstract class LinearModelParameters : ModelParametersBase<float>,
ISingleCanSavePfa,
ISingleCanSaveOnnx
{
protected readonly VBuffer<float> Weight;
[BestFriend]
private protected readonly VBuffer<float> Weight;

// _weightsDense is not persisted and is used for performance when the input instance is sparse.
private VBuffer<float> _weightsDense;
Expand Down Expand Up @@ -250,7 +251,7 @@ bool ISingleCanSaveOnnx.SaveAsOnnx(OnnxContext ctx, string[] outputs, string fea
}

// Generate the score from the given values, assuming they have already been normalized.
protected virtual float Score(in VBuffer<float> src)
private protected virtual float Score(in VBuffer<float> src)
{
if (src.IsDense)
{
Expand Down Expand Up @@ -711,7 +712,7 @@ private protected override void SaveCore(ModelSaveContext ctx)
ctx.SetVersionInfo(GetVersionInfo());
}

protected override float Score(in VBuffer<float> src)
private protected override float Score(in VBuffer<float> src)
{
return MathUtils.ExpSlow(base.Score(in src));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,28 +105,28 @@ internal static class Defaults

private const string RegisterName = nameof(LbfgsTrainerBase<TArgs, TTransformer, TModel>);

protected int NumFeatures;
protected VBuffer<float> CurrentWeights;
protected long NumGoodRows;
protected Double WeightSum;
protected bool ShowTrainingStats;
private protected int NumFeatures;
private protected VBuffer<float> CurrentWeights;
private protected long NumGoodRows;
private protected Double WeightSum;
private protected bool ShowTrainingStats;

private TModel _srcPredictor;

protected readonly TArgs Args;
protected readonly float L2Weight;
protected readonly float L1Weight;
protected readonly float OptTol;
protected readonly int MemorySize;
protected readonly int MaxIterations;
protected readonly float SgdInitializationTolerance;
protected readonly bool Quiet;
protected readonly float InitWtsDiameter;
protected readonly bool UseThreads;
protected readonly int? NumThreads;
protected readonly bool DenseOptimizer;
protected readonly long MaxNormalizationExamples;
protected readonly bool EnforceNonNegativity;
private protected readonly TArgs Args;
private protected readonly float L2Weight;
private protected readonly float L1Weight;
private protected readonly float OptTol;
private protected readonly int MemorySize;
private protected readonly int MaxIterations;
private protected readonly float SgdInitializationTolerance;
private protected readonly bool Quiet;
private protected readonly float InitWtsDiameter;
private protected readonly bool UseThreads;
private protected readonly int? NumThreads;
private protected readonly bool DenseOptimizer;
private protected readonly long MaxNormalizationExamples;
private protected readonly bool EnforceNonNegativity;

// The training data, when NOT using multiple threads.
private RoleMappedData _data;
Expand Down Expand Up @@ -256,9 +256,9 @@ private static TArgs ArgsInit(string featureColumn, SchemaShape.Column labelColu
return args;
}

protected virtual int ClassCount => 1;
protected int BiasCount => ClassCount;
protected int WeightCount => ClassCount * NumFeatures;
private protected virtual int ClassCount => 1;
private protected int BiasCount => ClassCount;
private protected int WeightCount => ClassCount * NumFeatures;
private protected virtual Optimizer InitializeOptimizer(IChannel ch, FloatLabelCursor.Factory cursorFactory,
out VBuffer<float> init, out ITerminationCriterion terminationCriterion)
{
Expand Down Expand Up @@ -364,15 +364,15 @@ private protected virtual VBuffer<float> InitializeWeightsSgd(IChannel ch, Float
return result;
}

protected abstract VBuffer<float> InitializeWeightsFromPredictor(TModel srcPredictor);
private protected abstract VBuffer<float> InitializeWeightsFromPredictor(TModel srcPredictor);

private protected abstract void CheckLabel(RoleMappedData data);

protected virtual void PreTrainingProcessInstance(float label, in VBuffer<float> feat, float weight)
private protected virtual void PreTrainingProcessInstance(float label, in VBuffer<float> feat, float weight)
{
}

protected abstract TModel CreatePredictor();
private protected abstract TModel CreatePredictor();

/// <summary>
/// The basic training calls the optimizer
Expand Down Expand Up @@ -570,24 +570,24 @@ private protected virtual void TrainCore(IChannel ch, RoleMappedData data)

// Ensure that the bias portion of vec is represented in vec.
// REVIEW: Is this really necessary?
protected void EnsureBiases(ref VBuffer<float> vec)
private protected void EnsureBiases(ref VBuffer<float> vec)
{
// REVIEW: Consider promoting this "densify first n entries" to a general purpose utility,
// if we ever encounter other situations where this becomes useful.
Contracts.Assert(vec.Length == BiasCount + WeightCount);
VBufferUtils.DensifyFirst(ref vec, BiasCount);
}

protected abstract float AccumulateOneGradient(in VBuffer<float> feat, float label, float weight,
private protected abstract float AccumulateOneGradient(in VBuffer<float> feat, float label, float weight,
in VBuffer<float> xDense, ref VBuffer<float> grad, ref float[] scratch);

private protected abstract void ComputeTrainingStatistics(IChannel ch, FloatLabelCursor.Factory cursorFactory, float loss, int numParams);

protected abstract void ProcessPriorDistribution(float label, float weight);
private protected abstract void ProcessPriorDistribution(float label, float weight);
/// <summary>
/// The gradient being used by the optimizer
/// </summary>
protected virtual float DifferentiableFunction(in VBuffer<float> x, ref VBuffer<float> gradient,
private protected virtual float DifferentiableFunction(in VBuffer<float> x, ref VBuffer<float> gradient,
IProgressChannelProvider progress)
{
Contracts.Assert((_numChunks == 0) != (_data == null));
Expand Down Expand Up @@ -647,7 +647,7 @@ protected virtual float DifferentiableFunction(in VBuffer<float> x, ref VBuffer<
/// REVIEW: consider getting rid of multithread-targeted members
/// Using TPL, the distinction between Multithreaded and Sequential implementations is unnecessary
/// </remarks>
protected virtual float DifferentiableFunctionMultithreaded(in VBuffer<float> xDense, ref VBuffer<float> gradient, IProgressChannel pch)
private protected virtual float DifferentiableFunctionMultithreaded(in VBuffer<float> xDense, ref VBuffer<float> gradient, IProgressChannel pch)
{
Contracts.Assert(_data == null);
Contracts.Assert(_cursorFactory == null);
Expand Down Expand Up @@ -679,7 +679,7 @@ protected virtual float DifferentiableFunctionMultithreaded(in VBuffer<float> xD
return loss;
}

protected float DifferentiableFunctionComputeChunk(int ichk, in VBuffer<float> xDense, ref VBuffer<float> grad, IProgressChannel pch)
private protected float DifferentiableFunctionComputeChunk(int ichk, in VBuffer<float> xDense, ref VBuffer<float> grad, IProgressChannel pch)
{
Contracts.Assert(0 <= ichk && ichk < _numChunks);
Contracts.AssertValueOrNull(pch);
Expand Down Expand Up @@ -733,7 +733,7 @@ private protected float DifferentiableFunctionStream(FloatLabelCursor.Factory cu
return (float)loss;
}

protected VBuffer<float> InitializeWeights(IEnumerable<float> weights, IEnumerable<float> biases)
private protected VBuffer<float> InitializeWeights(IEnumerable<float> weights, IEnumerable<float> biases)
{
Contracts.AssertValue(biases);
Contracts.AssertValue(weights);
Expand Down
Loading