Skip to content

Commit a7fcd07

Browse files
committed
Add example on freeze_data_and_dims
1 parent fe0e0d7 commit a7fcd07

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

pymc/model/transform/optimization.py

+22
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,28 @@ def freeze_dims_and_data(
5757
-------
5858
Model
5959
A new model with the specified dimensions and data frozen.
60+
61+
62+
Examples
63+
--------
64+
.. code-block:: python
65+
66+
import pymc as pm
67+
import pytensor.tensor as pt
68+
69+
from pymc.model.transform.optimization import freeze_dims_and_data
70+
71+
with pm.Model() as m:
72+
x = pm.Data("x", [0, 1, 2] * 1000)
73+
y = pm.Normal("y", mu=pt.unique(x).mean())
74+
75+
# pt.unique(x).mean() has to be computed in every logp function evaluation
76+
print("Logp eval time (1000x): ", m.profile(m.logp()).fct_call_time)
77+
78+
# pt.uniqe(x).mean() is cached in the logp function
79+
frozen_m = freeze_dims_and_data(m)
80+
print("Logp eval time (1000x): ", frozen_m.profile(frozen_m.logp()).fct_call_time)
81+
6082
"""
6183
fg, memo = fgraph_from_model(model)
6284

0 commit comments

Comments
 (0)