From 9abe7f65f2ff48aec182be47d1edc84a7ff5fda9 Mon Sep 17 00:00:00 2001 From: Zeeshan Ahmed Date: Wed, 16 May 2018 16:29:36 -0700 Subject: [PATCH 1/2] Removed two NextFloat() extension methods from RandomUtils and renamed NextSingle() -> NextFloat() --- src/Microsoft.ML.Core/Utilities/Random.cs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/src/Microsoft.ML.Core/Utilities/Random.cs b/src/Microsoft.ML.Core/Utilities/Random.cs index 88dd45209f..2924f076d1 100644 --- a/src/Microsoft.ML.Core/Utilities/Random.cs +++ b/src/Microsoft.ML.Core/Utilities/Random.cs @@ -11,9 +11,9 @@ namespace Microsoft.ML.Runtime public interface IRandom { /// - /// Generates a Single in the range [0, 1). + /// Generates a Float (single precision floating point) in the range [0, 1). /// - Single NextSingle(); + Single NextFloat(); /// /// Generates a Double in the range [0, 1). @@ -42,16 +42,6 @@ public interface IRandom public static class RandomUtils { - public static Single NextFloat(this IRandom rand) - { - return rand.NextSingle(); - } - - public static Single NextFloat(this Random rand) - { - return rand.NextDouble().ToFloat(); - } - public static TauswortheHybrid Create() { // Seed from a system random. @@ -117,7 +107,7 @@ private SysRandom(Random rnd) _rnd = rnd; } - public Single NextSingle() + public Single NextFloat() { // Since the largest value that NextDouble() can return rounds to 1 when cast to Single, // we need to protect against returning 1. @@ -237,7 +227,7 @@ private static uint GetSeed(IRandom rng) } } - public Single NextSingle() + public Single NextFloat() { NextState(); return GetSingle(); From c3f8402a92ec3fcc583b970fc92c59ae16b60490 Mon Sep 17 00:00:00 2001 From: Zeeshan Ahmed Date: Wed, 16 May 2018 17:26:04 -0700 Subject: [PATCH 2/2] Renamed NextFloat() -> NextSingle() --- src/Microsoft.ML.Core/Utilities/Random.cs | 8 ++++---- src/Microsoft.ML.Core/Utilities/Stats.cs | 4 ++-- .../Transforms/GenerateNumberTransform.cs | 2 +- .../KMeansPlusPlusTrainer.cs | 2 +- .../AutoMlEngines/UniformRandomEngine.cs | 4 ++-- .../Standard/LogisticRegression/LbfgsPredictorBase.cs | 2 +- .../Standard/Online/OnlineLinear.cs | 4 ++-- src/Microsoft.ML.Transforms/RffTransform.cs | 2 +- 8 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Microsoft.ML.Core/Utilities/Random.cs b/src/Microsoft.ML.Core/Utilities/Random.cs index 2924f076d1..7b1b14c974 100644 --- a/src/Microsoft.ML.Core/Utilities/Random.cs +++ b/src/Microsoft.ML.Core/Utilities/Random.cs @@ -11,9 +11,9 @@ namespace Microsoft.ML.Runtime public interface IRandom { /// - /// Generates a Float (single precision floating point) in the range [0, 1). + /// Generates a Single in the range [0, 1). /// - Single NextFloat(); + Single NextSingle(); /// /// Generates a Double in the range [0, 1). @@ -107,7 +107,7 @@ private SysRandom(Random rnd) _rnd = rnd; } - public Single NextFloat() + public Single NextSingle() { // Since the largest value that NextDouble() can return rounds to 1 when cast to Single, // we need to protect against returning 1. @@ -227,7 +227,7 @@ private static uint GetSeed(IRandom rng) } } - public Single NextFloat() + public Single NextSingle() { NextState(); return GetSingle(); diff --git a/src/Microsoft.ML.Core/Utilities/Stats.cs b/src/Microsoft.ML.Core/Utilities/Stats.cs index 6b048c4de3..33992950db 100644 --- a/src/Microsoft.ML.Core/Utilities/Stats.cs +++ b/src/Microsoft.ML.Core/Utilities/Stats.cs @@ -202,7 +202,7 @@ public static int SampleFromPoisson(IRandom rand, double lambda) // http://en.wikipedia.org/wiki/Laplace_distribution public static Float SampleFromLaplacian(IRandom rand, Float mean, Float scale) { - Float u = rand.NextFloat(); + Float u = rand.NextSingle(); u = u - 0.5f; Float ret = mean; if (u >= 0) @@ -221,7 +221,7 @@ public static Float SampleFromLaplacian(IRandom rand, Float mean, Float scale) /// public static Float SampleFromCauchy(IRandom rand) { - return (Float)Math.Tan(Math.PI * (rand.NextFloat() - 0.5)); + return (Float)Math.Tan(Math.PI * (rand.NextSingle() - 0.5)); } /// diff --git a/src/Microsoft.ML.Data/Transforms/GenerateNumberTransform.cs b/src/Microsoft.ML.Data/Transforms/GenerateNumberTransform.cs index 1b1fc0328d..f80589bdab 100644 --- a/src/Microsoft.ML.Data/Transforms/GenerateNumberTransform.cs +++ b/src/Microsoft.ML.Data/Transforms/GenerateNumberTransform.cs @@ -426,7 +426,7 @@ private void EnsureValue(ref long lastCounter, ref Float value, TauswortheHybrid Ch.Assert(lastCounter <= Input.Position); while (lastCounter < Input.Position) { - value = rng.NextFloat(); + value = rng.NextSingle(); lastCounter++; } } diff --git a/src/Microsoft.ML.KMeansClustering/KMeansPlusPlusTrainer.cs b/src/Microsoft.ML.KMeansClustering/KMeansPlusPlusTrainer.cs index 87fd220cf8..b833278890 100644 --- a/src/Microsoft.ML.KMeansClustering/KMeansPlusPlusTrainer.cs +++ b/src/Microsoft.ML.KMeansClustering/KMeansPlusPlusTrainer.cs @@ -328,7 +328,7 @@ public static void Initialize( cumulativeWeight += probabilityWeight; if (probabilityWeight > Epsilon && - host.Rand.NextFloat() < probabilityWeight / cumulativeWeight) + host.Rand.NextSingle() < probabilityWeight / cumulativeWeight) { // again, numerical error may cause selection of the same candidate twice, so ensure that the distance is non-trivially positive Utils.Swap(ref cursor.Features, ref candidate); diff --git a/src/Microsoft.ML.PipelineInference/AutoMlEngines/UniformRandomEngine.cs b/src/Microsoft.ML.PipelineInference/AutoMlEngines/UniformRandomEngine.cs index 2a4bb51836..9f304312c2 100644 --- a/src/Microsoft.ML.PipelineInference/AutoMlEngines/UniformRandomEngine.cs +++ b/src/Microsoft.ML.PipelineInference/AutoMlEngines/UniformRandomEngine.cs @@ -123,11 +123,11 @@ private void RandomlyPerturbSweepableHyperparameters(IEnumerable)fvg.CreateFromNormalized(Host.Rand.NextFloat())).Value; + floParam.RawValue = ((IParameterValue)fvg.CreateFromNormalized(Host.Rand.NextSingle())).Value; break; case TlcModule.SweepableLongParamAttribute lonParam: var lvg = AutoMlUtils.ToIValueGenerator(lonParam); - lonParam.RawValue = ((IParameterValue)lvg.CreateFromNormalized(Host.Rand.NextFloat())).Value; + lonParam.RawValue = ((IParameterValue)lvg.CreateFromNormalized(Host.Rand.NextSingle())).Value; break; default: throw new NotSupportedException($"Unknown type of sweepable parameter attribute: {param.GetType()}"); diff --git a/src/Microsoft.ML.StandardLearners/Standard/LogisticRegression/LbfgsPredictorBase.cs b/src/Microsoft.ML.StandardLearners/Standard/LogisticRegression/LbfgsPredictorBase.cs index e4273d7ce9..89f4866228 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/LogisticRegression/LbfgsPredictorBase.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/LogisticRegression/LbfgsPredictorBase.cs @@ -201,7 +201,7 @@ protected virtual Optimizer InitializeOptimizer(IChannel ch, FloatLabelCursor.Fa { Float[] initWeights = new Float[BiasCount + WeightCount]; for (int j = 0; j < initWeights.Length; j++) - initWeights[j] = InitWtsDiameter * (Host.Rand.NextFloat() - (Float)0.5); + initWeights[j] = InitWtsDiameter * (Host.Rand.NextSingle() - (Float)0.5); init = new VBuffer(initWeights.Length, initWeights); } else if (SgdInitializationTolerance > 0) diff --git a/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineLinear.cs b/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineLinear.cs index 253800171d..bcd4b33d58 100644 --- a/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineLinear.cs +++ b/src/Microsoft.ML.StandardLearners/Standard/Online/OnlineLinear.cs @@ -238,8 +238,8 @@ protected virtual void InitCore(IChannel ch, int numFeatures, LinearPredictor pr { Weights = VBufferUtils.CreateDense(NumFeatures); for (int i = 0; i < NumFeatures; i++) - Weights.Values[i] = Args.InitWtsDiameter * (Host.Rand.NextFloat() - (Float)0.5); - Bias = Args.InitWtsDiameter * (Host.Rand.NextFloat() - (Float)0.5); + Weights.Values[i] = Args.InitWtsDiameter * (Host.Rand.NextSingle() - (Float)0.5); + Bias = Args.InitWtsDiameter * (Host.Rand.NextSingle() - (Float)0.5); } else if (NumFeatures <= 1000) Weights = VBufferUtils.CreateDense(NumFeatures); diff --git a/src/Microsoft.ML.Transforms/RffTransform.cs b/src/Microsoft.ML.Transforms/RffTransform.cs index 6063559923..b8f49b4dce 100644 --- a/src/Microsoft.ML.Transforms/RffTransform.cs +++ b/src/Microsoft.ML.Transforms/RffTransform.cs @@ -191,7 +191,7 @@ private void GetDDimensionalFeatureMapping(int rowSize) private void GetDRotationTerms(int colSize) { for (int i = 0; i < colSize; ++i) - RotationTerms[i] = (_rand.NextFloat() - (Float)0.5) * (Float)Math.PI; + RotationTerms[i] = (_rand.NextSingle() - (Float)0.5) * (Float)Math.PI; } private void InitializeFourierCoefficients(int rowSize, int colSize)