Skip to content

Commit

Permalink
address another overflow issue in gradientMultiplier in LOR gradient …
Browse files Browse the repository at this point in the history
…code
  • Loading branch information
DB Tsai committed Jan 7, 2015
1 parent 64eefd0 commit f8447f9
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,18 @@ abstract class Gradient extends Serializable {
class LogisticGradient extends Gradient {
override def compute(data: Vector, label: Double, weights: Vector): (Vector, Double) = {
val margin = -1.0 * dot(data, weights)
val gradientMultiplier = (1.0 / (1.0 + math.exp(margin))) - label
/**
* gradientMultiplier = (1.0 / (1.0 + math.exp(margin))) - label
* However, the first part of gradientMultiplier can be potentially suffered from overflow,
* so we use the equivalent formula but more numerically stable.
*/
val gradientMultiplier = if (margin > 0.0) {
val temp = math.exp(-margin)
temp / (1.0 + temp) - label
} else {
1.0 / (1.0 + math.exp(margin)) - label
}

val gradient = data.copy
scal(gradientMultiplier, gradient)
val loss =
Expand Down

0 comments on commit f8447f9

Please sign in to comment.