forked from pymc-devs/pymc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_transform_value.py
616 lines (504 loc) · 19.4 KB
/
test_transform_value.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# Copyright 2024 The PyMC Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import gc
import operator
import numpy as np
import pytensor
import pytest
import scipy as sp
from numdifftools import Derivative, Jacobian
from pytensor import scan
from pytensor import tensor as pt
from pytensor.compile.builders import OpFromGraph
from pytensor.graph import FunctionGraph
from pytensor.graph.basic import equal_computations
import pymc as pm
from pymc.distributions.transforms import _default_transform, log, logodds
from pymc.logprob import conditional_logp
from pymc.logprob.abstract import MeasurableOp, _logprob
from pymc.logprob.transform_value import TransformValuesMapping, TransformValuesRewrite
from pymc.logprob.transforms import ExpTransform, LogOddsTransform, LogTransform
from pymc.testing import assert_no_rvs
from tests.logprob.test_transforms import DirichletScipyDist
@pytest.fixture(scope="module")
def multiout_measurable_op():
# Create a dummy Op that just returns the two inputs
mu1, mu2 = pt.scalars("mu1", "mu2")
class TestOpFromGraph(MeasurableOp, OpFromGraph):
def do_constant_folding(self, fgraph, node):
False
multiout_op = TestOpFromGraph([mu1, mu2], [mu1 + 0.0, mu2 + 0.0])
@_logprob.register(TestOpFromGraph)
def logp_multiout(op, values, mu1, mu2):
value1, value2 = values
return value1 + mu1, value2 + mu2
return multiout_op
def test_TransformValuesMapping():
x = pt.vector()
fg = FunctionGraph(outputs=[x])
tvm = TransformValuesMapping({})
fg.attach_feature(tvm)
tvm2 = TransformValuesMapping({})
fg.attach_feature(tvm2)
assert fg._features[-1] is tvm
def test_original_values_output_dict():
"""
Test that the original unconstrained value variable appears an the key of
the logprob factor
"""
p_rv = pt.random.beta(1, 1, name="p")
p_vv = p_rv.clone()
tr = TransformValuesRewrite({p_vv: logodds})
logp_dict = conditional_logp({p_rv: p_vv}, extra_rewrites=tr)
assert p_vv in logp_dict
@pytest.mark.parametrize(
"pt_dist, dist_params, sp_dist, size",
[
(pt.random.uniform, (0, 1), sp.stats.uniform, ()),
(
pt.random.pareto,
(1.5, 10.5),
lambda b, scale: sp.stats.pareto(b, scale=scale),
(),
),
(
pt.random.triangular,
(1.5, 3.0, 10.5),
lambda lower, mode, upper: sp.stats.triang(
(mode - lower) / (upper - lower), loc=lower, scale=upper - lower
),
(),
),
(
pt.random.halfnormal,
(0, 1),
sp.stats.halfnorm,
(),
),
pytest.param(
pt.random.wald,
(1.5, 10.5),
lambda mean, scale: sp.stats.invgauss(mean / scale, scale=scale),
(),
marks=pytest.mark.xfail(
reason="We don't use PyTensor's Wald operator",
raises=NotImplementedError,
),
),
(
pt.random.exponential,
(1.5,),
lambda mu: sp.stats.expon(scale=mu),
(),
),
pytest.param(
pt.random.lognormal,
(-1.5, 10.5),
lambda mu, sigma: sp.stats.lognorm(s=sigma, loc=0, scale=np.exp(mu)),
(),
),
(
pt.random.lognormal,
(-1.5, 1.5),
lambda mu, sigma: sp.stats.lognorm(s=sigma, scale=np.exp(mu)),
(),
),
(
pt.random.halfcauchy,
(1.5, 10.5),
lambda alpha, beta: sp.stats.halfcauchy(loc=alpha, scale=beta),
(),
),
(
pt.random.gamma,
(1.5, 10.5),
lambda alpha, inv_beta: sp.stats.gamma(alpha, scale=1.0 / inv_beta),
(),
),
(
pt.random.invgamma,
(1.5, 10.5),
lambda alpha, beta: sp.stats.invgamma(alpha, scale=beta),
(),
),
(
pm.ChiSquared.dist,
(1.5,),
lambda df: sp.stats.chi2(df),
(),
),
pytest.param(
pt.random.weibull,
(1.5,),
lambda c: sp.stats.weibull_min(c),
(),
marks=pytest.mark.xfail(
reason="We don't use PyTensor's Weibull operator",
raises=NotImplementedError,
),
),
(
pt.random.beta,
(1.5, 1.5),
lambda alpha, beta: sp.stats.beta(alpha, beta),
(),
),
(
pt.random.vonmises,
(1.5, 10.5),
lambda mu, kappa: sp.stats.vonmises(kappa, loc=mu),
(),
),
(
pt.random.dirichlet,
(np.array([0.7, 0.3]),),
lambda alpha: sp.stats.dirichlet(alpha),
(),
),
(
pt.random.dirichlet,
(np.array([[0.7, 0.3], [0.9, 0.1]]),),
lambda alpha: DirichletScipyDist(alpha),
None,
),
pytest.param(
pt.random.dirichlet,
(np.array([0.3, 0.7]),),
lambda alpha: DirichletScipyDist(alpha),
(3, 2),
),
],
)
def test_default_value_transform_logprob(pt_dist, dist_params, sp_dist, size):
"""
This test takes a `RandomVariable` type, plus parameters, and uses it to
construct a variable ``a`` that's used in the graph ``b =
pt.random.normal(a, 1.0)``. The transformed log-probability is then
computed for ``b``. We then test that the log-probability of ``a`` is
properly transformed, as well as any instances of ``a`` that are used
elsewhere in the graph (i.e. in ``b``), by comparing the graph for the
transformed log-probability with the SciPy-derived log-probability--using a
numeric approximation to the Jacobian term.
TODO: This test is rather redundant with those in tess/distributions/test_transform.py
"""
a = pt_dist(*dist_params, size=size)
a.name = "a"
a_value_var = pt.tensor(dtype=a.dtype, shape=(None,) * a.ndim)
a_value_var.name = "a_value"
b = pt.random.normal(a, 1.0)
b.name = "b"
b_value_var = b.clone()
b_value_var.name = "b_value"
transform = _default_transform(a.owner.op, a)
transform_rewrite = TransformValuesRewrite({a_value_var: transform})
res = conditional_logp({a: a_value_var, b: b_value_var}, extra_rewrites=transform_rewrite)
res_combined = pt.sum([pt.sum(factor) for factor in res.values()])
test_val_rng = np.random.RandomState(3238)
logp_vals_fn = pytensor.function([a_value_var, b_value_var], res_combined)
a_forward_fn = pytensor.function([a_value_var], transform.forward(a_value_var, *a.owner.inputs))
a_backward_fn = pytensor.function(
[a_value_var], transform.backward(a_value_var, *a.owner.inputs)
)
log_jac_fn = pytensor.function(
[a_value_var],
transform.log_jac_det(a_value_var, *a.owner.inputs),
on_unused_input="ignore",
)
for i in range(10):
a_dist = sp_dist(*dist_params)
a_val = a_dist.rvs(size=size, random_state=test_val_rng).astype(a_value_var.dtype)
b_dist = sp.stats.norm(a_val, 1.0)
b_val = b_dist.rvs(random_state=test_val_rng).astype(b_value_var.dtype)
a_trans_value = a_forward_fn(a_val)
if a_val.ndim > 0:
def jacobian_estimate_novec(value):
dim_diff = a_val.ndim - value.ndim
if dim_diff > 0:
# Make sure the dimensions match the expected input
# dimensions for the compiled backward transform function
def a_backward_fn_(x):
x_ = np.expand_dims(x, axis=list(range(dim_diff)))
return a_backward_fn(x_).squeeze()
else:
a_backward_fn_ = a_backward_fn
jacobian_val = Jacobian(a_backward_fn_)(value)
n_missing_dims = jacobian_val.shape[0] - jacobian_val.shape[1]
if n_missing_dims > 0:
missing_bases = np.eye(jacobian_val.shape[0])[..., -n_missing_dims:]
jacobian_val = np.concatenate([jacobian_val, missing_bases], axis=-1)
return np.linalg.slogdet(jacobian_val)[-1]
jacobian_estimate = np.vectorize(jacobian_estimate_novec, signature="(n)->()")
exp_log_jac_val = jacobian_estimate(a_trans_value)
else:
jacobian_val = np.atleast_2d(Derivative(a_backward_fn, step=1e-6)(a_trans_value))
exp_log_jac_val = np.linalg.slogdet(jacobian_val)[-1]
log_jac_val = log_jac_fn(a_trans_value)
np.testing.assert_allclose(exp_log_jac_val, log_jac_val, rtol=1e-4, atol=1e-10)
exp_logprob_val = a_dist.logpdf(a_val).sum()
exp_logprob_val += exp_log_jac_val.sum()
exp_logprob_val += b_dist.logpdf(b_val).sum()
logprob_val = logp_vals_fn(a_trans_value, b_val)
np.testing.assert_allclose(exp_logprob_val, logprob_val, rtol=1e-4, atol=1e-10)
@pytest.mark.parametrize("use_jacobian", [True, False])
def test_value_transform_logprob_nojac(use_jacobian):
X_rv = pt.random.halfnormal(0, 3, name="X")
x_vv = X_rv.clone()
x_vv.name = "x"
transform_rewrite = TransformValuesRewrite({x_vv: log})
tr_logp = conditional_logp(
{X_rv: x_vv}, extra_rewrites=transform_rewrite, use_jacobian=use_jacobian
)
tr_logp_combined = pt.sum([pt.sum(factor) for factor in tr_logp.values()])
np.testing.assert_allclose(
tr_logp_combined.eval({x_vv: np.log(2.5)}),
sp.stats.halfnorm(0, 3).logpdf(2.5) + (np.log(2.5) if use_jacobian else 0.0),
)
def test_hierarchical_value_transform():
"""
This model requires rv-value replacements in the backward transformation of
the value var `x`
"""
lower_rv = pt.random.uniform(0, 1, name="lower")
upper_rv = pt.random.uniform(9, 10, name="upper")
x_rv = pt.random.uniform(lower_rv, upper_rv, name="x")
lower = lower_rv.clone()
upper = upper_rv.clone()
x = x_rv.clone()
transform_rewrite = TransformValuesRewrite(
{
lower: _default_transform(lower_rv.owner.op, lower_rv),
upper: _default_transform(upper_rv.owner.op, upper_rv),
x: _default_transform(x_rv.owner.op, x_rv),
}
)
logp = conditional_logp(
{lower_rv: lower, upper_rv: upper, x_rv: x},
extra_rewrites=transform_rewrite,
)
logp_combined = pt.sum([pt.sum(factor) for factor in logp.values()])
assert_no_rvs(logp_combined)
assert not np.isinf(logp_combined.eval({lower: -10, upper: 20, x: -20}))
def test_nondefault_value_transform():
loc_rv = pt.random.uniform(-10, 10, name="loc")
scale_rv = pt.random.uniform(-1, 1, name="scale")
x_rv = pt.random.normal(loc_rv, scale_rv, name="x")
loc = loc_rv.clone()
scale = scale_rv.clone()
x = x_rv.clone()
transform_rewrite = TransformValuesRewrite(
{
loc: None,
scale: LogOddsTransform(),
x: LogTransform(),
}
)
logp = conditional_logp(
{loc_rv: loc, scale_rv: scale, x_rv: x},
extra_rewrites=transform_rewrite,
)
logp_combined = pt.sum([pt.sum(factor) for factor in logp.values()])
# Check numerical evaluation matches with expected transforms
loc_val = 0
scale_val_tr = -1
x_val_tr = -1
scale_val = sp.special.expit(scale_val_tr)
x_val = np.exp(x_val_tr)
exp_logp = 0
exp_logp += sp.stats.uniform(-10, 20).logpdf(loc_val)
exp_logp += sp.stats.uniform(-1, 2).logpdf(scale_val)
exp_logp += np.log(scale_val) + np.log1p(-scale_val) # logodds log_jac_det
exp_logp += sp.stats.norm(loc_val, scale_val).logpdf(x_val)
exp_logp += x_val_tr # log log_jac_det
np.testing.assert_allclose(
logp_combined.eval({loc: loc_val, scale: scale_val_tr, x: x_val_tr}),
exp_logp,
)
def test_no_value_transform_multiout_input():
r"""Make sure that `Op`\s with multiple outputs are handled correctly."""
# This SVD value is necessarily `1`, but it's generated by an `Op` with
# multiple outputs and no default output.
sd = pt.linalg.svd(pt.eye(1))[1][0]
x_rv = pt.random.normal(0, sd, name="x")
x = x_rv.clone()
transform_rewrite = TransformValuesRewrite({x: None})
logp = conditional_logp(
{x_rv: x},
extra_rewrites=transform_rewrite,
)
logp_combined = pt.sum([pt.sum(factor) for factor in logp.values()])
np.testing.assert_allclose(
logp_combined.eval({x: 1}),
sp.stats.norm(0, 1).logpdf(1),
)
@pytest.mark.parametrize("transform_x", (True, False))
@pytest.mark.parametrize("transform_y", (True, False))
def test_value_transform_multiout_op(transform_x, transform_y, multiout_measurable_op):
x, y = multiout_measurable_op(1, 2)
x.name = "x"
y.name = "y"
x_vv = x.clone()
y_vv = y.clone()
transform_rewrite = TransformValuesRewrite(
{
x_vv: LogTransform() if transform_x else None,
y_vv: ExpTransform() if transform_y else None,
}
)
logp = conditional_logp({x: x_vv, y: y_vv}, extra_rewrites=transform_rewrite)
logp_combined = pt.sum([pt.sum(factor) for factor in logp.values()])
x_vv_test = np.random.normal()
y_vv_test = np.abs(np.random.normal())
expected_logp = 0
if not transform_x:
expected_logp += x_vv_test + 1
else:
expected_logp += np.exp(x_vv_test) + 1 + x_vv_test
# y logp
if not transform_y:
expected_logp += y_vv_test + 2
else:
expected_logp += np.log(y_vv_test) + 2 - np.log(y_vv_test)
np.testing.assert_allclose(
logp_combined.eval({x_vv: x_vv_test, y_vv: y_vv_test}), expected_logp
)
def test_transformed_rv_and_value():
y_rv = pt.random.halfnormal(-1, 1, name="base_rv") + 1
y_rv.name = "y"
y_vv = y_rv.clone()
transform_rewrite = TransformValuesRewrite({y_vv: LogTransform()})
logp = conditional_logp({y_rv: y_vv}, extra_rewrites=transform_rewrite)
logp_combined = pt.sum([pt.sum(factor) for factor in logp.values()])
assert_no_rvs(logp_combined)
logp_fn = pytensor.function([y_vv], logp_combined)
y_test_val = -5
np.testing.assert_allclose(
logp_fn(y_test_val),
sp.stats.halfnorm(0, 1).logpdf(np.exp(y_test_val)) + y_test_val,
)
@pytest.mark.filterwarnings("error")
def test_mixture_transform():
"""Make sure that non-`RandomVariable` `MeasurableOp` variables can be transformed.
This test is specific to `MixtureRV`, which is derived from an `OpFromGraph`.
"""
I_rv = pt.random.bernoulli(0.5, name="I")
Y_1_rv = pt.random.beta(100, 1, name="Y_1")
Y_2_rv = pt.random.beta(1, 100, name="Y_2")
# A `MixtureRV`, which is an `OpFromGraph` subclass, will replace this
# `pt.stack` in the graph
Y_rv = pt.stack([Y_1_rv, Y_2_rv])[I_rv]
Y_rv.name = "Y"
i_vv = I_rv.clone()
i_vv.name = "i"
y_vv = Y_rv.clone()
y_vv.name = "y"
logp_no_trans = conditional_logp(
{Y_rv: y_vv, I_rv: i_vv},
)
logp_no_trans_comb = pt.sum([pt.sum(factor) for factor in logp_no_trans.values()])
transform_rewrite = TransformValuesRewrite({y_vv: LogTransform()})
logp_trans = conditional_logp(
{Y_rv: y_vv, I_rv: i_vv},
extra_rewrites=transform_rewrite,
use_jacobian=False,
)
logp_trans_combined = pt.sum([pt.sum(factor) for factor in logp_trans.values()])
# The untransformed graph should be the same as the transformed graph after
# replacing the `Y_rv` value variable with a transformed version of itself
logp_nt_fg = FunctionGraph(outputs=[logp_no_trans_comb], clone=False)
y_trans = pt.exp(y_vv)
y_trans.name = "y_log"
logp_nt_fg.replace(y_vv, y_trans)
logp_nt = logp_nt_fg.outputs[0]
assert equal_computations([logp_nt], [logp_trans_combined])
def test_scan_transform():
"""Test that Scan valued variables can be transformed"""
init = pt.random.beta(1, 1, name="init")
init_vv = init.clone()
def scan_step(prev_innov):
next_innov = pt.random.beta(prev_innov * 10, (1 - prev_innov) * 10)
update = {next_innov.owner.inputs[0]: next_innov.owner.outputs[0]}
return next_innov, update
innov, _ = scan(
fn=scan_step,
outputs_info=[init],
n_steps=4,
)
innov.name = "innov"
innov_vv = innov.clone()
tr = TransformValuesRewrite(
{
init_vv: LogOddsTransform(),
innov_vv: LogOddsTransform(),
}
)
logp = conditional_logp({init: init_vv, innov: innov_vv}, extra_rewrites=tr, use_jacobian=True)[
innov_vv
]
logp_fn = pytensor.function([init_vv, innov_vv], logp, on_unused_input="ignore")
# Create an unrolled scan graph as reference
innov = []
prev_innov = init
for i in range(4):
next_innov = pt.random.beta(prev_innov * 10, (1 - prev_innov) * 10, name="innov[i]")
innov.append(next_innov)
prev_innov = next_innov
innov = pt.stack(innov)
innov.name = "innov"
tr = TransformValuesRewrite(
{
init_vv: LogOddsTransform(),
innov_vv: LogOddsTransform(),
}
)
ref_logp = conditional_logp(
{init: init_vv, innov: innov_vv}, extra_rewrites=tr, use_jacobian=True
)[innov_vv]
ref_logp_fn = pytensor.function([init_vv, innov_vv], ref_logp, on_unused_input="ignore")
test_point = {
"init": np.array(-0.5),
"innov": np.full((4,), -0.5),
}
np.testing.assert_allclose(logp_fn(**test_point), ref_logp_fn(**test_point))
def test_weakref_leak():
"""Check that the rewrite does not have a growing memory footprint.
See #6990
"""
def _growth(limit=10, peak_stats={}):
"""Vendoring of objgraph.growth
Source: https://github.com/mgedmin/objgraph/blob/94b1ca61a11109547442701800292dcfc7f59fc8/objgraph.py#L253
"""
gc.collect()
objects = gc.get_objects()
stats = {}
for o in objects:
n = type(o).__name__
stats[n] = stats.get(n, 0) + 1
deltas = {}
for name, count in stats.items():
old_count = peak_stats.get(name, 0)
if count > old_count:
deltas[name] = count - old_count
peak_stats[name] = count
deltas = sorted(deltas.items(), key=operator.itemgetter(1), reverse=True)
if limit:
deltas = deltas[:limit]
return [(name, stats[name], delta) for name, delta in deltas]
rvs_to_values = {pt.random.beta(1, 1, name=f"p_{i}"): pt.scalar(f"p_{i}") for i in range(30)}
tr = TransformValuesRewrite({v: logodds for v in rvs_to_values.values()})
for i in range(20):
conditional_logp(rvs_to_values, extra_rewrites=tr)
res = _growth()
# Only start checking after warmup
if i > 15:
assert not res, "Object counts are still growing"