srowen commented on a change in pull request #32822:
URL: https://github.com/apache/spark/pull/32822#discussion_r649087032



##########
File path: mllib-local/src/main/scala/org/apache/spark/ml/impl/Utils.scala
##########
@@ -94,4 +95,43 @@ private[spark] object Utils {
       math.log1p(math.exp(x))
     }
   }
+
+  /**
+   * Perform in-place softmax conversion.
+   */
+  def softmax(values: Array[Double]): Unit = {
+    var maxValue = Double.MinValue
+    var i = 0
+    while (i < values.length) {
+      val value = values(i)
+      if (value.isPosInfinity) {
+        java.util.Arrays.fill(values, 0)
+        values(i) = 1.0
+        return
+      } else if (value > maxValue) {
+        maxValue = value
+      }
+      i += 1
+    }
+
+    var sum = 0.0
+    i = 0
+    if (maxValue > 0) {

Review comment:
       Hm, would this not be valuable even if the max were very small? avoids 
underflow in exp in the same way it avoids overflow when the max is large. This 
avoids two branches




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to