Repository navigation
Avoid numerical instability - #1652
naftaliharris wants to merge 1 commit into
Conversation
This avoids basically doing 1 - 1, for example: >>> from math import exp >>> margin = -40 >>> 1 - 1 / (1 + exp(margin)) 0.0 >>> exp(margin) / (1 + exp(margin)) 4.248354255291589e-18 >>>
|
Can one of the admins verify this patch? |
|
Y'know, there's a similar issue in
For -40, this gives 0.0, when really it should be about math.exp(-40) = 4.248354255291589e-18, since log(1+x) ~= x for very small x. This one can be fixed up with
I'll have a look for other instances beyond the 4 I see and open a JIRA? I could mention this PR too to bring it under one umbrella. |
|
See also https://issues.apache.org/jira/browse/SPARK-2748 and #1659 . This could be considered part of SPARK-2748. |
|
Jenkins, add to whitelist. |
|
Jenkins, test this please. |
|
QA tests have started for PR 1652. This patch merges cleanly. |
…Math.exp, Math.log In a few places in MLlib, an expression of the form `log(1.0 + p)` is evaluated. When p is so small that `1.0 + p == 1.0`, the result is 0.0. However the correct answer is very near `p`. This is why `Math.log1p` exists. Similarly for one instance of `exp(m) - 1` in GraphX; there's a special `Math.expm1` method. While the errors occur only for very small arguments, given their use in machine learning algorithms, this is entirely possible. Also note the related PR for Python: #1652 Author: Sean Owen <srowen@gmail.com> Closes #1659 from srowen/SPARK-2748 and squashes the following commits: c5926d4 [Sean Owen] Use log1p, expm1 for better precision for tiny arguments
|
QA results for PR 1652: |
|
LGTM. Merged into master. Thanks! |
|
Awesome, thank you! :-) |
…Math.exp, Math.log In a few places in MLlib, an expression of the form `log(1.0 + p)` is evaluated. When p is so small that `1.0 + p == 1.0`, the result is 0.0. However the correct answer is very near `p`. This is why `Math.log1p` exists. Similarly for one instance of `exp(m) - 1` in GraphX; there's a special `Math.expm1` method. While the errors occur only for very small arguments, given their use in machine learning algorithms, this is entirely possible. Also note the related PR for Python: apache#1652 Author: Sean Owen <srowen@gmail.com> Closes apache#1659 from srowen/SPARK-2748 and squashes the following commits: c5926d4 [Sean Owen] Use log1p, expm1 for better precision for tiny arguments
This avoids basically doing 1 - 1, for example: ```python >>> from math import exp >>> margin = -40 >>> 1 - 1 / (1 + exp(margin)) 0.0 >>> exp(margin) / (1 + exp(margin)) 4.248354255291589e-18 >>> ``` Author: Naftali Harris <naftaliharris@gmail.com> Closes apache#1652 from naftaliharris/patch-2 and squashes the following commits: 0d55a9f [Naftali Harris] Avoid numerical instability
…ut fusion (apache#1669) ### What changes were proposed in this pull request? This regenerates `sql/varka/plans/m8/casts_suites.tsv`, the record of Spark's cast suites under Varka, on current master. It also updates the record's counts in `VARKA-368.md` (now 193 fused, 681 declined). ### Why are the changes needed? Master's post-merge run 45171015126 failed on `8e631d8ed41`. `VarkaCastWithAnsiOffSuite`, `VarkaCastWithAnsiOnSuite` and `VarkaTryCastSuite` aborted in `afterAll`, because their lines in the record moved. apache#1646 added the record. It was generated and tested on a base without apache#1628, and apache#1628 merged before apache#1646. apache#1628 fuses every 64-bit decimal value as a projection output. So 120 lines changed: - A cast to a decimal now declines with "is row 350's" instead of "is row 348's". - Five identity casts of 64-bit decimals now fuse (for example `DECIMAL(10,0)` to `DECIMAL(10,0)`), in all three suites and modes. apache#1652 (also in that run) changes only matrix tooling and plans. ### Does this PR introduce _any_ user-facing change? No. This changes only a generated test record. ### How was this patch tested? - Regenerated with `VARKA_SUITES_REGEN=true build/sbt 'sql/testOnly *VarkaCastWithAnsiOffSuite *VarkaCastWithAnsiOnSuite *VarkaTryCastSuite'`. The 240 changed lines are exactly the lines CI printed. - The three suites then pass without the regen flag: 435 tests, 0 aborted. - `dev/varka_census.py --check` passes. The census changes no other file. `/code-review high` found no bugs and three nits. One round fixed two: the commit message now counts all five newly fused casts, and the plan's counts are updated. The third nit was the commit's placeholder author. It is kept, because the PR is squash-merged. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code (Claude Opus 5.5)
This avoids basically doing 1 - 1, for example: