Skip to content

Commit 4df211e

Browse files
aseyboldttwiecki
authored andcommitted
Use model context in model.logpt
1 parent ff16a0e commit 4df211e

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

pymc3/model.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -455,15 +455,17 @@ def dlogp_array(self):
455455
@memoize
456456
def logpt(self):
457457
"""Theano scalar of log-probability of the model"""
458-
factors = [var.logpt for var in self.basic_RVs] + self.potentials
459-
return tt.add(*map(tt.sum, factors))
458+
with self:
459+
factors = [var.logpt for var in self.basic_RVs] + self.potentials
460+
return tt.add(*map(tt.sum, factors))
460461

461462
@property
462463
def varlogpt(self):
463464
"""Theano scalar of log-probability of the unobserved random variables
464465
(excluding deterministic)."""
465-
factors = [var.logpt for var in self.vars]
466-
return tt.add(*map(tt.sum, factors))
466+
with self:
467+
factors = [var.logpt for var in self.vars]
468+
return tt.add(*map(tt.sum, factors))
467469

468470
@property
469471
def vars(self):
@@ -521,14 +523,16 @@ def Var(self, name, dist, data=None, total_size=None):
521523
name = self.name_for(name)
522524
if data is None:
523525
if getattr(dist, "transform", None) is None:
524-
var = FreeRV(name=name, distribution=dist,
525-
total_size=total_size, model=self)
526+
with self:
527+
var = FreeRV(name=name, distribution=dist,
528+
total_size=total_size, model=self)
526529
self.free_RVs.append(var)
527530
else:
528-
var = TransformedRV(name=name, distribution=dist,
529-
transform=dist.transform,
530-
total_size=total_size,
531-
model=self)
531+
with self:
532+
var = TransformedRV(name=name, distribution=dist,
533+
transform=dist.transform,
534+
total_size=total_size,
535+
model=self)
532536
pm._log.debug('Applied {transform}-transform to {name}'
533537
' and added transformed {orig_name} to model.'.format(
534538
transform=dist.transform.name,
@@ -537,18 +541,20 @@ def Var(self, name, dist, data=None, total_size=None):
537541
self.deterministics.append(var)
538542
return var
539543
elif isinstance(data, dict):
540-
var = MultiObservedRV(name=name, data=data, distribution=dist,
541-
total_size=total_size, model=self)
544+
with self:
545+
var = MultiObservedRV(name=name, data=data, distribution=dist,
546+
total_size=total_size, model=self)
542547
self.observed_RVs.append(var)
543548
if var.missing_values:
544549
self.free_RVs += var.missing_values
545550
self.missing_values += var.missing_values
546551
for v in var.missing_values:
547552
self.named_vars[v.name] = v
548553
else:
549-
var = ObservedRV(name=name, data=data,
550-
distribution=dist,
551-
total_size=total_size, model=self)
554+
with self:
555+
var = ObservedRV(name=name, data=data,
556+
distribution=dist,
557+
total_size=total_size, model=self)
552558
self.observed_RVs.append(var)
553559
if var.missing_values:
554560
self.free_RVs.append(var.missing_values)
@@ -615,11 +621,12 @@ def makefn(self, outs, mode=None, *args, **kwargs):
615621
-------
616622
Compiled Theano function
617623
"""
618-
return theano.function(self.vars, outs,
619-
allow_input_downcast=True,
620-
on_unused_input='ignore',
621-
accept_inplace=True,
622-
mode=mode, *args, **kwargs)
624+
with self:
625+
return theano.function(self.vars, outs,
626+
allow_input_downcast=True,
627+
on_unused_input='ignore',
628+
accept_inplace=True,
629+
mode=mode, *args, **kwargs)
623630

624631
def fn(self, outs, mode=None, *args, **kwargs):
625632
"""Compiles a Theano function which returns the values of `outs`

0 commit comments

Comments
 (0)