Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bugs in truncated_normal #585

Merged

Conversation

charlielam0615
Copy link
Collaborator

Description

truncated_normal in brainpy.math.random calls jr.uniform to sample from a uniform distribution and then inverse the cdf to get truncated values.

jr.uniform samples from a closed interval. When lower or upper takes inf values and when jr.uniform sampled an extreme value, the inverse cdf would have inf values, which result in underflow values after clipping.

Fix: subtract and add a small value to minval and maxval arguments of jr.uniform.

How Has This Been Tested

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • Code follows the code style of this project.
  • Changes follow the CONTRIBUTING guidelines.
  • Update necessary documentation accordingly.
  • Lint and tests pass locally with the changes.
  • Check issues and pull requests first. You don't want to duplicate effort.

Other information

Copy link
Collaborator

@chaoming0625 chaoming0625 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great PR. I have not aware of this issue!

Copy link
Collaborator

@chaoming0625 chaoming0625 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend to use the following samll value:

eps = lax.nextafter(0., np.array(np.inf, dtype=dtype)))
out = jr.uniform(key, size, dtype, minval=2 * l - 1 + eps , maxval=2 * u - 1 - eps)

use `lax.nextafter` to get small values.
This reverts commit a59be4b.
use `lax.nextafter` for `minval` and `maxval`
Copy link
Collaborator

@chaoming0625 chaoming0625 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

@chaoming0625 chaoming0625 merged commit 5871c9c into brainpy:master Jan 4, 2024
15 checks passed
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.

2 participants