|
| 1 | +import numpy as np |
| 2 | +import theano |
1 | 3 | import theano.tensor as tt
|
2 | 4 |
|
3 | 5 | from unification import var
|
|
9 | 11 |
|
10 | 12 | from theano.gof.opt import EquilibriumOptimizer
|
11 | 13 | from theano.gof.graph import inputs as tt_inputs
|
| 14 | +from theano.scan_module.scan_op import Scan |
12 | 15 |
|
13 | 16 | from symbolic_pymc.theano.meta import mt
|
14 |
| -from symbolic_pymc.theano.opt import KanrenRelationSub, FunctionGraph |
| 17 | +from symbolic_pymc.theano.opt import ( |
| 18 | + KanrenRelationSub, |
| 19 | + FunctionGraph, |
| 20 | + push_out_rvs_from_scan, |
| 21 | +) |
15 | 22 | from symbolic_pymc.theano.utils import optimize_graph
|
| 23 | +from symbolic_pymc.theano.random_variables import CategoricalRV, DirichletRV, NormalRV |
16 | 24 |
|
17 | 25 |
|
18 | 26 | def test_kanren_opt():
|
@@ -58,3 +66,74 @@ def distributes(in_lv, out_lv):
|
58 | 66 | assert fgraph_opt.owner.inputs[1].owner.op == tt.add
|
59 | 67 | assert isinstance(fgraph_opt.owner.inputs[1].owner.inputs[0].owner.op, tt.Dot)
|
60 | 68 | assert isinstance(fgraph_opt.owner.inputs[1].owner.inputs[1].owner.op, tt.Dot)
|
| 69 | + |
| 70 | + |
| 71 | +def test_push_out_rvs(): |
| 72 | + theano.config.cxx = "" |
| 73 | + theano.config.mode = "FAST_COMPILE" |
| 74 | + tt.config.compute_test_value = "warn" |
| 75 | + |
| 76 | + rng_state = np.random.RandomState(np.random.MT19937(np.random.SeedSequence(1234))) |
| 77 | + rng_tt = theano.shared(rng_state, name="rng", borrow=True) |
| 78 | + rng_tt.tag.is_rng = True |
| 79 | + rng_tt.default_update = rng_tt |
| 80 | + |
| 81 | + N_tt = tt.iscalar("N") |
| 82 | + N_tt.tag.test_value = 10 |
| 83 | + M_tt = tt.iscalar("M") |
| 84 | + M_tt.tag.test_value = 2 |
| 85 | + |
| 86 | + mus_tt = tt.matrix("mus_t") |
| 87 | + mus_tt.tag.test_value = np.stack([np.arange(0.0, 10), np.arange(0.0, -10, -1)], axis=-1).astype( |
| 88 | + theano.config.floatX |
| 89 | + ) |
| 90 | + |
| 91 | + sigmas_tt = tt.ones((N_tt,)) |
| 92 | + Gamma_rv = DirichletRV(tt.ones((M_tt, M_tt)), rng=rng_tt, name="Gamma") |
| 93 | + |
| 94 | + # The optimizer should do nothing to this term, because it's not a `Scan` |
| 95 | + fgraph = FunctionGraph(tt_inputs([Gamma_rv]), [Gamma_rv]) |
| 96 | + pushoutrvs_opt = EquilibriumOptimizer([push_out_rvs_from_scan], max_use_ratio=10) |
| 97 | + Gamma_opt_rv = optimize_graph(fgraph, pushoutrvs_opt, return_graph=False) |
| 98 | + # The `FunctionGraph` will, however, clone the graph objects, so we can't |
| 99 | + # simply check that `gamma_opt_rv == Gamma_rv` |
| 100 | + assert all(type(a) == type(b) for a, b in zip(tt_inputs([Gamma_rv]), tt_inputs([Gamma_opt_rv]))) |
| 101 | + assert theano.scan_module.scan_utils.equal_computations( |
| 102 | + [Gamma_opt_rv], [Gamma_rv], tt_inputs([Gamma_opt_rv]), tt_inputs([Gamma_rv]) |
| 103 | + ) |
| 104 | + |
| 105 | + # In this case, `Y_t` depends on `S_t` and `S_t` is not output. Our |
| 106 | + # push-out optimization should create a new `Scan` that also outputs each |
| 107 | + # `S_t`. |
| 108 | + def scan_fn(mus_t, sigma_t, Gamma_t, rng): |
| 109 | + S_t = CategoricalRV(Gamma_t[0], rng=rng, name="S_t") |
| 110 | + Y_t = NormalRV(mus_t[S_t], sigma_t, rng=rng, name="Y_t") |
| 111 | + return Y_t |
| 112 | + |
| 113 | + Y_rv, _ = theano.scan( |
| 114 | + fn=scan_fn, |
| 115 | + sequences=[mus_tt, sigmas_tt], |
| 116 | + non_sequences=[Gamma_rv, rng_tt], |
| 117 | + outputs_info=[{}], |
| 118 | + strict=True, |
| 119 | + name="scan_rv", |
| 120 | + ) |
| 121 | + Y_rv.name = "Y_rv" |
| 122 | + |
| 123 | + orig_scan_op = Y_rv.owner.op |
| 124 | + assert len(Y_rv.owner.outputs) == 2 |
| 125 | + assert isinstance(orig_scan_op, Scan) |
| 126 | + assert len(orig_scan_op.outputs) == 2 |
| 127 | + assert orig_scan_op.outputs[0].owner.op == NormalRV |
| 128 | + assert isinstance(orig_scan_op.outputs[1].type, tt.raw_random.RandomStateType) |
| 129 | + |
| 130 | + fgraph = FunctionGraph(tt_inputs([Y_rv]), [Y_rv], clone=True) |
| 131 | + fgraph_opt = optimize_graph(fgraph, pushoutrvs_opt, return_graph=True) |
| 132 | + |
| 133 | + # There should now be a new output for all the `S_t` |
| 134 | + new_scan = fgraph_opt.outputs[0].owner |
| 135 | + assert len(new_scan.outputs) == 3 |
| 136 | + assert isinstance(new_scan.op, Scan) |
| 137 | + assert new_scan.op.outputs[0].owner.op == NormalRV |
| 138 | + assert new_scan.op.outputs[1].owner.op == CategoricalRV |
| 139 | + assert isinstance(new_scan.op.outputs[2].type, tt.raw_random.RandomStateType) |
0 commit comments