-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This `Op` adds the value variable to the graph so that `PreserveRVMappings` is no longer needed. It also allows clarifies the definition and actions of rewrites that truly apply to a `MeasurableVariable` and its value variable simultaneously.
- Loading branch information
1 parent
580c887
commit a6a9cb4
Showing
5 changed files
with
171 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import aesara | ||
import aesara.tensor as at | ||
import numpy as np | ||
from aesara.gradient import NullTypeGradError, grad | ||
from pytest import raises | ||
|
||
from aeppl.abstract import valued_variable | ||
|
||
|
||
def test_observed(): | ||
rv_var = at.random.normal(0, 1, size=3) | ||
obs_var = valued_variable( | ||
rv_var, np.array([0.2, 0.1, -2.4], dtype=aesara.config.floatX) | ||
) | ||
|
||
assert obs_var.owner.inputs[0] is rv_var | ||
|
||
with raises(TypeError): | ||
valued_variable(rv_var, np.array([1, 2], dtype=int)) | ||
|
||
with raises(TypeError): | ||
valued_variable(rv_var, np.array([[1.0, 2.0]], dtype=rv_var.dtype)) | ||
|
||
# obs_rv = valued_variable(None, np.array([0.2, 0.1, -2.4], dtype=aesara.config.floatX)) | ||
# | ||
# assert isinstance(obs_rv.owner.inputs[0].type, NoneTypeT) | ||
|
||
rv_val = at.vector() | ||
rv_val.tag.test_value = np.array([0.2, 0.1, -2.4], dtype=aesara.config.floatX) | ||
|
||
obs_var = valued_variable(rv_var, rv_val) | ||
|
||
with raises(NullTypeGradError): | ||
grad(obs_var.sum(), [rv_val]) |
This file contains 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import aesara.tensor as at | ||
import pytest | ||
from aesara.graph.opt import EquilibriumOptimizer | ||
from aesara.graph.opt_utils import optimize_graph | ||
from aesara.tensor.extra_ops import BroadcastTo | ||
|
||
from aeppl.opt import valued_var_bcast_lift | ||
|
||
bcast_lift_opt = EquilibriumOptimizer( | ||
[valued_var_bcast_lift], ignore_newtrees=False, max_use_ratio=1000 | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"rv_params, rv_size, bcast_shape, should_rewrite", | ||
[ | ||
# The `BroadcastTo` shouldn't be lifted, because it would imply that there | ||
# are 10 independent samples, when there's really only one | ||
pytest.param( | ||
(0, 1), | ||
None, | ||
(10,), | ||
False, | ||
marks=pytest.mark.xfail(reason="Not implemented"), | ||
), | ||
# These should work, under the assumption that `size == 10`, of course. | ||
((0, 1), at.iscalar("size"), (10,), True), | ||
((0, 1), at.iscalar("size"), (1, 10, 1), True), | ||
((at.zeros((at.iscalar("size"),)), 1), None, (10,), True), | ||
], | ||
) | ||
def test_naive_bcast_rv_lift(rv_params, rv_size, bcast_shape, should_rewrite): | ||
graph = at.broadcast_to(at.random.normal(*rv_params, size=rv_size), bcast_shape) | ||
|
||
assert isinstance(graph.owner.op, BroadcastTo) | ||
|
||
new_graph = optimize_graph(graph, custom_opt=bcast_lift_opt) | ||
|
||
if should_rewrite: | ||
assert not isinstance(new_graph.owner.op, BroadcastTo) | ||
else: | ||
assert isinstance(new_graph.owner.op, BroadcastTo) |