Skip to content

Commit

Permalink
Fixed RandomUtils.NextFloat() extension methods (dotnet#177)
Browse files Browse the repository at this point in the history
Removed two NextFloat() extension methods from RandomUtils and replaced all usage of them with `IRandom.NextSingle()`.
  • Loading branch information
zeahmed authored and eerhardt committed Jul 27, 2018
1 parent 0b000ba commit 462f9d4
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 20 deletions.
10 changes: 0 additions & 10 deletions src/Microsoft.ML.Core/Utilities/Random.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.ML.Core/Utilities/Stats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -221,7 +221,7 @@ public static Float SampleFromLaplacian(IRandom rand, Float mean, Float scale)
/// <returns></returns>
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));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.ML.KMeansClustering/KMeansPlusPlusTrainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ private void RandomlyPerturbSweepableHyperparameters(IEnumerable<TlcModule.Sweep
break;
case TlcModule.SweepableFloatParamAttribute floParam:
var fvg = AutoMlUtils.ToIValueGenerator(floParam);
floParam.RawValue = ((IParameterValue<float>)fvg.CreateFromNormalized(Host.Rand.NextFloat())).Value;
floParam.RawValue = ((IParameterValue<float>)fvg.CreateFromNormalized(Host.Rand.NextSingle())).Value;
break;
case TlcModule.SweepableLongParamAttribute lonParam:
var lvg = AutoMlUtils.ToIValueGenerator(lonParam);
lonParam.RawValue = ((IParameterValue<long>)lvg.CreateFromNormalized(Host.Rand.NextFloat())).Value;
lonParam.RawValue = ((IParameterValue<long>)lvg.CreateFromNormalized(Host.Rand.NextSingle())).Value;
break;
default:
throw new NotSupportedException($"Unknown type of sweepable parameter attribute: {param.GetType()}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Float>(initWeights.Length, initWeights);
}
else if (SgdInitializationTolerance > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ protected virtual void InitCore(IChannel ch, int numFeatures, LinearPredictor pr
{
Weights = VBufferUtils.CreateDense<Float>(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<Float>(NumFeatures);
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.ML.Transforms/RffTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 462f9d4

Please sign in to comment.