-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Implement default_transform
and transform
argument for distributions
#7207
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
Merged
ricardoV94
merged 24 commits into
pymc-devs:main
from
aerubanov:5674-default-transform-arg
Apr 19, 2024
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
8a50f9f
add default transform argument
aerubanov 7434b9a
Apply suggestions from code review
aerubanov 4fde32f
fix arguments passing
aerubanov 5c7519f
add support for transform=None
aerubanov 1dc0fcb
fix failed tests
aerubanov 55b0edb
Update pymc/distributions/distribution.py
aerubanov d791ac7
fix failed tests
aerubanov def0f6e
fix test
aerubanov 7d6ecf9
add tests for transfor args
aerubanov 704aac6
fix formating
aerubanov 23bd69f
Update pymc/model/core.py
aerubanov 35b44fe
Update tests/distributions/test_transform.py
aerubanov 7f0d1d3
Update tests/distributions/test_transform.py
aerubanov 80641f8
pass transform to default transform
aerubanov 80fa510
remove duplicated test case
aerubanov 655a364
move transform=None warning to core module
aerubanov 995ec93
update type hints and docstrings
aerubanov 3eefa8e
change test for transforms order
aerubanov 8a005fe
add new test case
aerubanov 8a708f7
Update pymc/model/core.py
aerubanov 412ef7f
change test casses for transform args
aerubanov 48f5f85
fix docstring and change argument order
aerubanov bff4371
Apply suggestions from code review
aerubanov e0f26b0
refactor test case
aerubanov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,6 @@ | |
from sys import modules | ||
from typing import ( | ||
TYPE_CHECKING, | ||
Any, | ||
Literal, | ||
Optional, | ||
TypeVar, | ||
|
@@ -48,7 +47,7 @@ | |
|
||
from pymc.blocking import DictToArrayBijection, RaveledVars | ||
from pymc.data import GenTensorVariable, is_minibatch | ||
from pymc.distributions.transforms import _default_transform | ||
from pymc.distributions.transforms import ChainedTransform, _default_transform | ||
from pymc.exceptions import ( | ||
BlockModelAccessError, | ||
ImputationWarning, | ||
|
@@ -58,6 +57,7 @@ | |
) | ||
from pymc.initial_point import make_initial_point_fn | ||
from pymc.logprob.basic import transformed_conditional_logp | ||
from pymc.logprob.transforms import Transform | ||
from pymc.logprob.utils import ParameterValueError, replace_rvs_by_values | ||
from pymc.model_graph import model_to_graphviz | ||
from pymc.pytensorf import ( | ||
|
@@ -1214,7 +1214,16 @@ def set_data( | |
shared_object.set_value(values) | ||
|
||
def register_rv( | ||
self, rv_var, name, observed=None, total_size=None, dims=None, transform=UNSET, initval=None | ||
self, | ||
rv_var, | ||
name, | ||
*, | ||
observed=None, | ||
total_size=None, | ||
dims=None, | ||
default_transform=UNSET, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing description in the docstrings |
||
transform=UNSET, | ||
initval=None, | ||
): | ||
"""Register an (un)observed random variable with the model. | ||
|
||
|
@@ -1229,8 +1238,10 @@ def register_rv( | |
upscales logp of variable with ``coef = total_size/var.shape[0]`` | ||
dims : tuple | ||
Dimension names for the variable. | ||
default_transform | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: show default_transform before transform (also in the signature)? |
||
A default transform for the random variable in log-likelihood space. | ||
transform | ||
A transform for the random variable in log-likelihood space. | ||
Additional transform which may be applied after default transform. | ||
initval | ||
The initial value of the random variable. | ||
|
||
|
@@ -1255,7 +1266,7 @@ def register_rv( | |
if total_size is not None: | ||
raise ValueError("total_size can only be passed to observed RVs") | ||
self.free_RVs.append(rv_var) | ||
self.create_value_var(rv_var, transform) | ||
self.create_value_var(rv_var, transform=transform, default_transform=default_transform) | ||
self.add_named_variable(rv_var, dims) | ||
self.set_initval(rv_var, initval) | ||
else: | ||
|
@@ -1278,7 +1289,9 @@ def register_rv( | |
|
||
# `rv_var` is potentially changed by `make_obs_var`, | ||
# for example into a new graph for imputation of missing data. | ||
rv_var = self.make_obs_var(rv_var, observed, dims, transform, total_size) | ||
rv_var = self.make_obs_var( | ||
rv_var, observed, dims, default_transform, transform, total_size | ||
) | ||
|
||
return rv_var | ||
|
||
|
@@ -1287,7 +1300,8 @@ def make_obs_var( | |
rv_var: TensorVariable, | ||
data: np.ndarray, | ||
dims, | ||
transform: Any | None, | ||
default_transform: Transform | None, | ||
transform: Transform | None, | ||
total_size: int | None, | ||
) -> TensorVariable: | ||
"""Create a `TensorVariable` for an observed random variable. | ||
|
@@ -1301,8 +1315,10 @@ def make_obs_var( | |
The observed data. | ||
dims : tuple | ||
Dimension names for the variable. | ||
transform : int, optional | ||
default_transform | ||
A transform for the random variable in log-likelihood space. | ||
transform | ||
Additional transform which may be applied after default transform. | ||
|
||
Returns | ||
------- | ||
|
@@ -1339,12 +1355,19 @@ def make_obs_var( | |
|
||
# Register ObservedRV corresponding to observed component | ||
observed_rv.name = f"{name}_observed" | ||
self.create_value_var(observed_rv, transform=None, value_var=observed_data) | ||
self.create_value_var( | ||
observed_rv, transform=transform, default_transform=None, value_var=observed_data | ||
) | ||
self.add_named_variable(observed_rv) | ||
self.observed_RVs.append(observed_rv) | ||
|
||
# Register FreeRV corresponding to unobserved components | ||
self.register_rv(unobserved_rv, f"{name}_unobserved", transform=transform) | ||
self.register_rv( | ||
unobserved_rv, | ||
f"{name}_unobserved", | ||
transform=transform, | ||
default_transform=default_transform, | ||
) | ||
|
||
# Register Deterministic that combines observed and missing | ||
# Note: This can widely increase memory consumption during sampling for large datasets | ||
|
@@ -1363,14 +1386,21 @@ def make_obs_var( | |
rv_var.name = name | ||
|
||
rv_var.tag.observations = data | ||
self.create_value_var(rv_var, transform=None, value_var=data) | ||
self.create_value_var( | ||
rv_var, transform=transform, default_transform=None, value_var=data | ||
) | ||
self.add_named_variable(rv_var, dims) | ||
self.observed_RVs.append(rv_var) | ||
|
||
return rv_var | ||
|
||
def create_value_var( | ||
self, rv_var: TensorVariable, transform: Any, value_var: Variable | None = None | ||
self, | ||
rv_var: TensorVariable, | ||
*, | ||
default_transform: Transform, | ||
transform: Transform, | ||
value_var: Variable | None = None, | ||
) -> TensorVariable: | ||
"""Create a ``TensorVariable`` that will be used as the random | ||
variable's "value" in log-likelihood graphs. | ||
|
@@ -1385,7 +1415,11 @@ def create_value_var( | |
---------- | ||
rv_var : TensorVariable | ||
|
||
transform : Any | ||
default_transform: Transform | ||
A transform for the random variable in log-likelihood space. | ||
|
||
transform: Transform | ||
Additional transform which may be applied after default transform. | ||
|
||
value_var : Variable, optional | ||
|
||
|
@@ -1396,11 +1430,25 @@ def create_value_var( | |
|
||
# Make the value variable a transformed value variable, | ||
# if there's an applicable transform | ||
if transform is UNSET: | ||
if transform is None and default_transform is UNSET: | ||
default_transform = None | ||
warnings.warn( | ||
"To disable default transform, please use default_transform=None" | ||
" instead of transform=None. Setting transform to None will" | ||
" not have any effect in future.", | ||
UserWarning, | ||
) | ||
|
||
if default_transform is UNSET: | ||
if rv_var.owner is None: | ||
transform = None | ||
default_transform = None | ||
else: | ||
transform = _default_transform(rv_var.owner.op, rv_var) | ||
default_transform = _default_transform(rv_var.owner.op, rv_var) | ||
|
||
if transform is UNSET: | ||
ricardoV94 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
transform = default_transform | ||
elif transform is not None and default_transform is not None: | ||
transform = ChainedTransform([default_transform, transform]) | ||
ricardoV94 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if value_var is None: | ||
if transform is None: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.