Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example on freeze_data_and_dims #7594

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add example on freeze_data_and_dims
  • Loading branch information
ricardoV94 committed Nov 27, 2024
commit 9f50d37f2ffba5610079aaae79e26efea194784c
22 changes: 22 additions & 0 deletions pymc/model/transform/optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,28 @@ def freeze_dims_and_data(
-------
Model
A new model with the specified dimensions and data frozen.


Examples
--------
.. code-block:: python

import pymc as pm
import pytensor.tensor as pt

from pymc.model.transform.optimization import freeze_dims_and_data

with pm.Model() as m:
x = pm.Data("x", [0, 1, 2] * 1000)
y = pm.Normal("y", mu=pt.unique(x).mean())

# pt.unique(x).mean() has to be computed in every logp function evaluation
print("Logp eval time (1000x): ", m.profile(m.logp()).fct_call_time)

# pt.uniqe(x).mean() is cached in the logp function
frozen_m = freeze_dims_and_data(m)
print("Logp eval time (1000x): ", frozen_m.profile(frozen_m.logp()).fct_call_time)

"""
fg, memo = fgraph_from_model(model)

Expand Down