Skip to content

Commit 715ba89

Browse files
authored
Fix SoftMax precision by utilizing double in the internal calculations (#3676)
* Fix SoftMax precision by utilizing double in the internal calculations * Brew install libomp 7.0.0 only. (#3721) * Brew install libomp 7.0.0 only. * Brew install libomp 7.0.0 only. * Brew install libomp 7.0.0 only. * Brew install libomp 7.0.0 only. * Fix SoftMax precision by utilizing double in the internal calculations
1 parent 822d4d8 commit 715ba89

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/Microsoft.ML.StandardTrainers/Standard/MulticlassClassification/OneVersusAllTrainer.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -740,15 +740,17 @@ public override ValueMapper<VBuffer<float>, VBuffer<float>> GetMapper()
740740

741741
private void NormalizeSoftmax(float[] scores, int count)
742742
{
743-
float sum = 0;
743+
double sum = 0;
744+
var score = new double[count];
745+
744746
for (int i = 0; i < count; i++)
745747
{
746-
scores[i] = (float)Math.Exp(scores[i]);
747-
sum += scores[i];
748+
score[i] = Math.Exp(scores[i]);
749+
sum += score[i];
748750
}
749751

750752
for (int i = 0; i < count; i++)
751-
scores[i] = scores[i] / sum;
753+
scores[i] = (float)(score[i] / sum);
752754
}
753755

754756
public override JToken SaveAsPfa(BoundPfaContext ctx, JToken input)

0 commit comments

Comments
 (0)