An unofficial, read-only Hacker News reader. Join the beta →

The KL Divergence Is Never Negative, Except in PyTorch (opens external site)

97 points by Anon84 3 days ago | arxiv.org | 40 comments on HN

View full thread · View on Hacker News

Comment thread (10)

  1. It is surprisingly easy to get unexpected negative values with floating point operations. One I remember was a square root that was failing on a negative argument despite that argument being clamped against zero. The problem was that the clamp was using some macros

      #define MIN(a, b) ((a) < (b) ? (a) : (b))
      #define MAX(a, b) ((a) < (b) ? (b) : (a))
    
    and the arguments were being clamped with MAX(x, 0). Well, if x is negative zero, it is not less than 0, so the macro returns x. The square root then looks at the sign bit and fails. Negative zero is a perfectly possible result from a series of calculations that destructively cancel.
    Replies (2)
    1. > The square root then looks at the sign bit and fails.

      Every FP standard I know of that has negative zero requires that sqrt(-0) == 0. Eg (IEEE754-2008, probably the most popular and I don't have a PDF for 1985 or whatever):

      > The operation squareRoot(x) computes √ x. It has a positive sign for all operands ≥ 0, except that squareRoot(−0) shall be −0.

      > [Invalid operation if] g) squareRoot if the operand is less than zero [emphasis added]

      So that's a clear-cut compiler / FP library bug.

      Replies (1)
      1. Wait, sqrt(-0) is defined as -0 and not +0? Isn’t that surprising behavior?
        Replies (2)
        1. I mean either way sqrt(-0)^2 fails to equal -0, so both option are annoying.

          The real answer would be something like 0i, but that's not available.

          Replies (2)
          1. Why not −0i?
            Replies (2)
            1. Because i is not a meaningful concept in floating point numbers.
            2. It's customary to use the branch cut where sqrt(-1) = i.

              Honestly it doesn't really matter but that's because you can't actually tell i and -i apart.

          2. Next time we revise the floating point standard, we should add an "i bit" :)
        2. +0 would also be annoying - there's not really a good answer here.

          A result of -0.0 can mean two things (not counting it just being wrong):

          a: the real (as in number) result is exactly zero, with a negative sign by coincidence (eg (1-1)/(0-1)).

          b: the real result is strictly less than zero, but even tinier than the smallest denormal, so it was rounded up to zero.

          Case (a) means you must have sqrt(-0) == 0, or you get problems like derf_'s.

          In case of case (b), having a square root be negative (impossible!) is the best indication we can squeeze in that something might be weird.

    2. > It is surprisingly easy to get unexpected negative values with floating point operations.

      This why one should NEVER compute variance as difference of E(x^2) - (E(x))^2. Mathematically with infinite precision it is the same as E((x-E(x))^2) but in floating point arithmetic one can easily get negative variance when subtracting two floating numbers.