generated from aesara-devs/aesara-repo
-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add goal for normal loc-scale transformation
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import aesara.tensor as at | ||
from etuples import etuple, etuplize | ||
from kanren import eq, lall | ||
from kanren.facts import Relation, fact | ||
from unification import var | ||
|
||
loc_scale_family = Relation("loc-scale") | ||
fact(loc_scale_family, at.random.normal) | ||
|
||
def normal_scale_loc_goal(in_expr, out_expr): | ||
"""Create a relation to lift and sink scale and location parameters of distributions.""" | ||
|
||
# Centered representation | ||
rng_lv, size_lv, type_idx_lv = var(), var(), var() | ||
mu_lv, sd_lv = var(), var() | ||
normal_centered_et = etuple( | ||
etuplize(at.random.normal), rng_lv, size_lv, type_idx_lv, mu_lv, sd_lv | ||
) | ||
|
||
# Non-centered representation | ||
normal_nc_et = etuple( | ||
etuplize(at.add), | ||
mu_lv, | ||
etuple( | ||
etuplize(at.mul), | ||
sd_lv, | ||
etuple( | ||
etuplize(at.random.normal), | ||
0.0, | ||
1.0, | ||
rng=rng_lv, | ||
size=size_lv, | ||
dtype=type_idx_lv, | ||
), | ||
), | ||
) | ||
|
||
return lall( | ||
eq(in_expr, normal_centered_et), | ||
eq(out_expr, normal_nc_et), | ||
) |
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,37 @@ | ||
from functools import partial | ||
|
||
import aesara.tensor as at | ||
from aesara.graph.unify import eval_if_etuple | ||
from kanren import run | ||
from kanren.graph import reduceo, walko | ||
from unification import var | ||
|
||
from aemcmc.transforms import normal_scale_loc_goal | ||
|
||
|
||
def test_normal_scale_loc_transform(): | ||
"""""" | ||
|
||
srng = at.random.RandomStream(0) | ||
mu_a_rv = srng.normal(0, 1) | ||
sigma_a_rv = srng.halfcauchy(1) | ||
a_rv = srng.normal(mu_a_rv, sigma_a_rv, size=(10,)) | ||
|
||
mu_b_rv = srng.normal(0, 1) | ||
sigma_b_rv = srng.halfcauchy(1) | ||
b_rv = srng.normal(mu_b_rv, sigma_b_rv, size=(10)) | ||
|
||
mu = a_rv + b_rv | ||
sigma_rv = srng.halfcauchy(5.0) | ||
Y_rv = srng.normal(mu, sigma_rv) | ||
|
||
q_lv = var() | ||
(expr_graph,) = run( | ||
1, q_lv, walko(partial(reduceo, normal_scale_loc_goal), Y_rv, q_lv) | ||
) | ||
Y_nc_rv = eval_if_etuple(expr_graph) | ||
|
||
# Make sure that Y_rv gets replaced with an addition | ||
assert Y_nc_rv.owner.op == at.add | ||
rhs = Y_nc_rv.owner.inputs[1].owner.inputs[0] | ||
assert rhs.owner.op == at.mul |