Skip to content

Commit 98a1ad4

Browse files
committed
Use model.logp(sum=False) with dot-product for logp scalings
1 parent 7472c32 commit 98a1ad4

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

pymc_extras/inference/advi/objective.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
import pytensor.tensor as pt
4+
35
from pymc import Model
46
from pytensor.graph.replace import graph_replace
57
from pytensor.tensor import TensorVariable
@@ -53,11 +55,11 @@ def get_logp_logq(
5355
}
5456

5557
if logp_scalings:
56-
scaled = set(logp_scalings)
57-
rest = [var for var in (*model.basic_RVs, *model.potentials) if var not in scaled]
58-
model_logp = model.logp(vars=rest)
59-
for var, scale in logp_scalings.items():
60-
model_logp = model_logp + scale * model.logp(vars=[var])
58+
logps = model.logp(sum=False)
59+
all_vars = model.free_RVs + model.observed_RVs + model.potentials
60+
scales = pt.constant([logp_scalings.get(var, 1.0) for var in all_vars])
61+
summed_logps = pt.stack([pt.sum(logp) for logp in logps])
62+
model_logp = pt.dot(scales, summed_logps)
6163
else:
6264
model_logp = model.logp()
6365

0 commit comments

Comments
 (0)