Skip to content

Commit ae1ecef

Browse files
zeahmedTomFinley
authored andcommitted
Fixed RandomUtils.NextFloat() extension methods (#177)
Removed two NextFloat() extension methods from RandomUtils and replaced all usage of them with `IRandom.NextSingle()`.
1 parent 616e75f commit ae1ecef

File tree

8 files changed

+10
-20
lines changed

8 files changed

+10
-20
lines changed

src/Microsoft.ML.Core/Utilities/Random.cs

-10
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,6 @@ public interface IRandom
4242

4343
public static class RandomUtils
4444
{
45-
public static Single NextFloat(this IRandom rand)
46-
{
47-
return rand.NextSingle();
48-
}
49-
50-
public static Single NextFloat(this Random rand)
51-
{
52-
return rand.NextDouble().ToFloat();
53-
}
54-
5545
public static TauswortheHybrid Create()
5646
{
5747
// Seed from a system random.

src/Microsoft.ML.Core/Utilities/Stats.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public static int SampleFromPoisson(IRandom rand, double lambda)
202202
// http://en.wikipedia.org/wiki/Laplace_distribution
203203
public static Float SampleFromLaplacian(IRandom rand, Float mean, Float scale)
204204
{
205-
Float u = rand.NextFloat();
205+
Float u = rand.NextSingle();
206206
u = u - 0.5f;
207207
Float ret = mean;
208208
if (u >= 0)
@@ -221,7 +221,7 @@ public static Float SampleFromLaplacian(IRandom rand, Float mean, Float scale)
221221
/// <returns></returns>
222222
public static Float SampleFromCauchy(IRandom rand)
223223
{
224-
return (Float)Math.Tan(Math.PI * (rand.NextFloat() - 0.5));
224+
return (Float)Math.Tan(Math.PI * (rand.NextSingle() - 0.5));
225225
}
226226

227227
/// <summary>

src/Microsoft.ML.Data/Transforms/GenerateNumberTransform.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ private void EnsureValue(ref long lastCounter, ref Float value, TauswortheHybrid
426426
Ch.Assert(lastCounter <= Input.Position);
427427
while (lastCounter < Input.Position)
428428
{
429-
value = rng.NextFloat();
429+
value = rng.NextSingle();
430430
lastCounter++;
431431
}
432432
}

src/Microsoft.ML.KMeansClustering/KMeansPlusPlusTrainer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ public static void Initialize(
328328
cumulativeWeight += probabilityWeight;
329329

330330
if (probabilityWeight > Epsilon &&
331-
host.Rand.NextFloat() < probabilityWeight / cumulativeWeight)
331+
host.Rand.NextSingle() < probabilityWeight / cumulativeWeight)
332332
{
333333
// again, numerical error may cause selection of the same candidate twice, so ensure that the distance is non-trivially positive
334334
Utils.Swap(ref cursor.Features, ref candidate);

src/Microsoft.ML.PipelineInference/AutoMlEngines/UniformRandomEngine.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ private void RandomlyPerturbSweepableHyperparameters(IEnumerable<TlcModule.Sweep
123123
break;
124124
case TlcModule.SweepableFloatParamAttribute floParam:
125125
var fvg = AutoMlUtils.ToIValueGenerator(floParam);
126-
floParam.RawValue = ((IParameterValue<float>)fvg.CreateFromNormalized(Host.Rand.NextFloat())).Value;
126+
floParam.RawValue = ((IParameterValue<float>)fvg.CreateFromNormalized(Host.Rand.NextSingle())).Value;
127127
break;
128128
case TlcModule.SweepableLongParamAttribute lonParam:
129129
var lvg = AutoMlUtils.ToIValueGenerator(lonParam);
130-
lonParam.RawValue = ((IParameterValue<long>)lvg.CreateFromNormalized(Host.Rand.NextFloat())).Value;
130+
lonParam.RawValue = ((IParameterValue<long>)lvg.CreateFromNormalized(Host.Rand.NextSingle())).Value;
131131
break;
132132
default:
133133
throw new NotSupportedException($"Unknown type of sweepable parameter attribute: {param.GetType()}");

src/Microsoft.ML.StandardLearners/Standard/LogisticRegression/LbfgsPredictorBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ protected virtual Optimizer InitializeOptimizer(IChannel ch, FloatLabelCursor.Fa
201201
{
202202
Float[] initWeights = new Float[BiasCount + WeightCount];
203203
for (int j = 0; j < initWeights.Length; j++)
204-
initWeights[j] = InitWtsDiameter * (Host.Rand.NextFloat() - (Float)0.5);
204+
initWeights[j] = InitWtsDiameter * (Host.Rand.NextSingle() - (Float)0.5);
205205
init = new VBuffer<Float>(initWeights.Length, initWeights);
206206
}
207207
else if (SgdInitializationTolerance > 0)

src/Microsoft.ML.StandardLearners/Standard/Online/OnlineLinear.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ protected virtual void InitCore(IChannel ch, int numFeatures, LinearPredictor pr
238238
{
239239
Weights = VBufferUtils.CreateDense<Float>(NumFeatures);
240240
for (int i = 0; i < NumFeatures; i++)
241-
Weights.Values[i] = Args.InitWtsDiameter * (Host.Rand.NextFloat() - (Float)0.5);
242-
Bias = Args.InitWtsDiameter * (Host.Rand.NextFloat() - (Float)0.5);
241+
Weights.Values[i] = Args.InitWtsDiameter * (Host.Rand.NextSingle() - (Float)0.5);
242+
Bias = Args.InitWtsDiameter * (Host.Rand.NextSingle() - (Float)0.5);
243243
}
244244
else if (NumFeatures <= 1000)
245245
Weights = VBufferUtils.CreateDense<Float>(NumFeatures);

src/Microsoft.ML.Transforms/RffTransform.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ private void GetDDimensionalFeatureMapping(int rowSize)
191191
private void GetDRotationTerms(int colSize)
192192
{
193193
for (int i = 0; i < colSize; ++i)
194-
RotationTerms[i] = (_rand.NextFloat() - (Float)0.5) * (Float)Math.PI;
194+
RotationTerms[i] = (_rand.NextSingle() - (Float)0.5) * (Float)Math.PI;
195195
}
196196

197197
private void InitializeFourierCoefficients(int rowSize, int colSize)

0 commit comments

Comments
 (0)