Skip to content

Avoid numerical instability - #1652

Closed
naftaliharris wants to merge 1 commit into
apache:masterfrom
naftaliharris:patch-2
Closed

naftaliharris wants to merge 1 commit into
apache:masterfrom
naftaliharris:patch-2

Conversation

@naftaliharris

Copy link
Copy Markdown
Contributor

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
>>>

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
>>>
@AmplabJenkins

Copy link
Copy Markdown

Can one of the admins verify this patch?

@srowen

srowen commented Jul 30, 2014

Copy link
Copy Markdown
Member

Y'know, there's a similar issue in LogisticGradient.scala, in lines like:

math.log(1 + math.exp(margin))

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

math.log1p(math.exp(margin))

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.

@srowen

srowen commented Jul 30, 2014

Copy link
Copy Markdown
Member

See also https://issues.apache.org/jira/browse/SPARK-2748 and #1659 . This could be considered part of SPARK-2748.

@mengxr

mengxr commented Jul 30, 2014

Copy link
Copy Markdown
Contributor

Jenkins, add to whitelist.

@mengxr

mengxr commented Jul 30, 2014

Copy link
Copy Markdown
Contributor

Jenkins, test this please.

@SparkQA

SparkQA commented Jul 30, 2014

Copy link
Copy Markdown

QA tests have started for PR 1652. This patch merges cleanly.
View progress: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/17453/consoleFull

asfgit pushed a commit that referenced this pull request Jul 30, 2014
…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
@SparkQA

SparkQA commented Jul 30, 2014

Copy link
Copy Markdown

QA results for PR 1652:
- This patch PASSES unit tests.
- This patch merges cleanly
- This patch adds no public classes

For more information see test ouptut:
https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/17453/consoleFull

@mengxr

mengxr commented Jul 30, 2014

Copy link
Copy Markdown
Contributor

LGTM. Merged into master. Thanks!

@asfgit asfgit closed this in e3d85b7 Jul 30, 2014
@naftaliharris

Copy link
Copy Markdown
Contributor Author

Awesome, thank you! :-)

xiliu82 pushed a commit to xiliu82/spark that referenced this pull request Sep 4, 2014
…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
xiliu82 pushed a commit to xiliu82/spark that referenced this pull request Sep 4, 2014
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
MaxGekk added a commit to MaxGekk/spark that referenced this pull request Nov 23, 2026
…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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants