Name | stable-trunc-gaussian JSON |
Version |
1.3.9
JSON |
| download |
home_page | |
Summary | A numerically-stable and differentiable implementation of the Truncated Gaussian distribution in Pytorch. |
upload_time | 2023-11-05 23:07:32 |
maintainer | |
docs_url | None |
author | |
requires_python | |
license | MIT License Copyright (c) 2023 Carlos Núñez Molina Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
keywords |
machine-learning
statistics
pytorch
stability
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
[![PyPI version](https://badge.fury.io/py/stable-trunc-gaussian.svg)](https://badge.fury.io/py/stable-trunc-gaussian)
[![Downloads](https://static.pepy.tech/badge/stable-trunc-gaussian)](https://pepy.tech/project/stable-trunc-gaussian)
# Stable Truncated Gaussian
A **differentiable** implementation of the **Truncated Gaussian (Normal)** distribution using Python and Pytorch, which is **numerically stable** even when the *μ* parameter lies outside the interval *\[a,b\]* given by the bounds of the distribution. In this situation, a naive evaluation of the mean, variance and log-probability of the distribution could otherwise result in [catastrophic cancellation](https://en.wikipedia.org/wiki/Catastrophic_cancellation). Our code is inspired by [TruncatedNormal.jl](https://github.com/cossio/TruncatedNormal.jl) and [torch_truncnorm](https://github.com/toshas/torch_truncnorm). Currently, we provide numerically-stable methods for calculating the mean, variance, log-probability, KL-divergence and sampling from the distribution. Our current implementation of `icdf` (which is used for sampling from the distribution) still needs some work for those situations where the *\[a,b\]* interval is small. For a comparison between our `icdf` implementation and the one provided by `scipy`, take a look at the *images_cdf_comparison* folder.
## Installation
Simply install with `pip`:
pip install stable-trunc-gaussian
## Example
Run the following code in Python:
from stable_trunc_gaussian import TruncatedGaussian as TG
from torch import tensor as t
# Create a Truncated Gaussian with mu=0, sigma=1, a=10, b=11
# Notice how mu is outside the interval [a,b]
dist = TG(t(0),t(1),t(10),t(11))
print("Mean:", dist.mean)
print("Variance:", dist.variance)
print("Log-prob(10.5):", dist.log_prob(t(10.5)))
Result:
Mean: tensor(10.0981)
Variance: tensor(0.0094)
Log-prob(10.5): tensor(-2.8126)
## Parallel vs Sequential Implementation
The class obtained by doing `from stable_trunc_gaussian import TruncatedGaussian` corresponds to a **parallel** implementation of the truncated gaussian, which makes possible to obtain several values (mean, variance and log-probs) in parallel. In case you are only interested in computing values sequentially, i.e., one at a time, we also provide a **sequential** implementation which results more efficient *only* for this case. In order to use this sequential implementation, simply do `from stable_trunc_gaussian import SeqTruncatedGaussian`. Here is an example:
from stable_trunc_gaussian import TruncatedGaussian, SeqTruncatedGaussian
from torch import tensor as t
# Parallel computation
means = TruncatedGaussian(t([0,0.5]),t(1,1),t(-1,2),t(1,5)).mean
# Sequential computation
# Note: the 'TruncatedGaussian' class can also be used for this sequential case
mean_0 = SeqTruncatedGaussian(t([0]),t(1),t(-1),t(1)).mean
mean_1 = SeqTruncatedGaussian(t([0.5]),t(1),t(2),t(5)).mean
# Acknowledgements
We want to thank users [KFrank](https://discuss.pytorch.org/u/KFrank) and [ptrblck](https://discuss.pytorch.org/u/ptrblck) for their help in solving the bug when computing the gradients for the parallel version (bug solved in version 1.1.1).
Raw data
{
"_id": null,
"home_page": "",
"name": "stable-trunc-gaussian",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "machine-learning,statistics,pytorch,stability",
"author": "",
"author_email": "Carlos N\u00fa\u00f1ez Molina <ccaarlos@ugr.es>",
"download_url": "https://files.pythonhosted.org/packages/f1/a6/b9a1a06e1079d8392c821e77052e9bd95e2df7cc0784aa6b32391fc78725/stable-trunc-gaussian-1.3.9.tar.gz",
"platform": null,
"description": "[![PyPI version](https://badge.fury.io/py/stable-trunc-gaussian.svg)](https://badge.fury.io/py/stable-trunc-gaussian)\n[![Downloads](https://static.pepy.tech/badge/stable-trunc-gaussian)](https://pepy.tech/project/stable-trunc-gaussian)\n\n# Stable Truncated Gaussian\nA **differentiable** implementation of the **Truncated Gaussian (Normal)** distribution using Python and Pytorch, which is **numerically stable** even when the *\u03bc* parameter lies outside the interval *\\[a,b\\]* given by the bounds of the distribution. In this situation, a naive evaluation of the mean, variance and log-probability of the distribution could otherwise result in [catastrophic cancellation](https://en.wikipedia.org/wiki/Catastrophic_cancellation). Our code is inspired by [TruncatedNormal.jl](https://github.com/cossio/TruncatedNormal.jl) and [torch_truncnorm](https://github.com/toshas/torch_truncnorm). Currently, we provide numerically-stable methods for calculating the mean, variance, log-probability, KL-divergence and sampling from the distribution. Our current implementation of `icdf` (which is used for sampling from the distribution) still needs some work for those situations where the *\\[a,b\\]* interval is small. For a comparison between our `icdf` implementation and the one provided by `scipy`, take a look at the *images_cdf_comparison* folder.\n\n## Installation\n\nSimply install with `pip`:\n\n pip install stable-trunc-gaussian\n\n## Example\n\nRun the following code in Python:\n\n from stable_trunc_gaussian import TruncatedGaussian as TG\n from torch import tensor as t\n \n # Create a Truncated Gaussian with mu=0, sigma=1, a=10, b=11\n # Notice how mu is outside the interval [a,b]\n dist = TG(t(0),t(1),t(10),t(11))\n \n print(\"Mean:\", dist.mean)\n print(\"Variance:\", dist.variance)\n print(\"Log-prob(10.5):\", dist.log_prob(t(10.5)))\n \nResult:\n\n Mean: tensor(10.0981)\n Variance: tensor(0.0094)\n Log-prob(10.5): tensor(-2.8126)\n\n## Parallel vs Sequential Implementation\nThe class obtained by doing `from stable_trunc_gaussian import TruncatedGaussian` corresponds to a **parallel** implementation of the truncated gaussian, which makes possible to obtain several values (mean, variance and log-probs) in parallel. In case you are only interested in computing values sequentially, i.e., one at a time, we also provide a **sequential** implementation which results more efficient *only* for this case. In order to use this sequential implementation, simply do `from stable_trunc_gaussian import SeqTruncatedGaussian`. Here is an example:\n\n from stable_trunc_gaussian import TruncatedGaussian, SeqTruncatedGaussian\n from torch import tensor as t\n\n # Parallel computation\n means = TruncatedGaussian(t([0,0.5]),t(1,1),t(-1,2),t(1,5)).mean\n\n # Sequential computation\n # Note: the 'TruncatedGaussian' class can also be used for this sequential case\n mean_0 = SeqTruncatedGaussian(t([0]),t(1),t(-1),t(1)).mean\n mean_1 = SeqTruncatedGaussian(t([0.5]),t(1),t(2),t(5)).mean\n\n# Acknowledgements\n\nWe want to thank users [KFrank](https://discuss.pytorch.org/u/KFrank) and [ptrblck](https://discuss.pytorch.org/u/ptrblck) for their help in solving the bug when computing the gradients for the parallel version (bug solved in version 1.1.1).\n",
"bugtrack_url": null,
"license": "MIT License Copyright (c) 2023 Carlos N\u00fa\u00f1ez Molina Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
"summary": "A numerically-stable and differentiable implementation of the Truncated Gaussian distribution in Pytorch.",
"version": "1.3.9",
"project_urls": {
"Homepage": "https://github.com/TheAeryan/stable-truncated-gaussian"
},
"split_keywords": [
"machine-learning",
"statistics",
"pytorch",
"stability"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "42d964f25f8ce75dbe49a8f2cea82ccbc2539e97d0223b14f01dc05155c90080",
"md5": "e91f275d2a7bd432b6d5161c2d1cb7a4",
"sha256": "a0392f32d83bdc869f98cca4373c47a154f8017ce4aa686184310b84f7269cd1"
},
"downloads": -1,
"filename": "stable_trunc_gaussian-1.3.9-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e91f275d2a7bd432b6d5161c2d1cb7a4",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 20695,
"upload_time": "2023-11-05T23:07:30",
"upload_time_iso_8601": "2023-11-05T23:07:30.980050Z",
"url": "https://files.pythonhosted.org/packages/42/d9/64f25f8ce75dbe49a8f2cea82ccbc2539e97d0223b14f01dc05155c90080/stable_trunc_gaussian-1.3.9-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "f1a6b9a1a06e1079d8392c821e77052e9bd95e2df7cc0784aa6b32391fc78725",
"md5": "95b13e6d87dead4f4365adc2632e3fc2",
"sha256": "ed0ce4f1ea0b8b63d773c376f721c5da7a8184695a0fb14c1d6cf408b65e9acc"
},
"downloads": -1,
"filename": "stable-trunc-gaussian-1.3.9.tar.gz",
"has_sig": false,
"md5_digest": "95b13e6d87dead4f4365adc2632e3fc2",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 18473,
"upload_time": "2023-11-05T23:07:32",
"upload_time_iso_8601": "2023-11-05T23:07:32.239742Z",
"url": "https://files.pythonhosted.org/packages/f1/a6/b9a1a06e1079d8392c821e77052e9bd95e2df7cc0784aa6b32391fc78725/stable-trunc-gaussian-1.3.9.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-11-05 23:07:32",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "TheAeryan",
"github_project": "stable-truncated-gaussian",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "stable-trunc-gaussian"
}