@@ -455,15 +455,17 @@ def dlogp_array(self):
455
455
@memoize
456
456
def logpt (self ):
457
457
"""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 ))
460
461
461
462
@property
462
463
def varlogpt (self ):
463
464
"""Theano scalar of log-probability of the unobserved random variables
464
465
(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 ))
467
469
468
470
@property
469
471
def vars (self ):
@@ -521,14 +523,16 @@ def Var(self, name, dist, data=None, total_size=None):
521
523
name = self .name_for (name )
522
524
if data is None :
523
525
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 )
526
529
self .free_RVs .append (var )
527
530
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 )
532
536
pm ._log .debug ('Applied {transform}-transform to {name}'
533
537
' and added transformed {orig_name} to model.' .format (
534
538
transform = dist .transform .name ,
@@ -537,18 +541,20 @@ def Var(self, name, dist, data=None, total_size=None):
537
541
self .deterministics .append (var )
538
542
return var
539
543
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 )
542
547
self .observed_RVs .append (var )
543
548
if var .missing_values :
544
549
self .free_RVs += var .missing_values
545
550
self .missing_values += var .missing_values
546
551
for v in var .missing_values :
547
552
self .named_vars [v .name ] = v
548
553
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 )
552
558
self .observed_RVs .append (var )
553
559
if var .missing_values :
554
560
self .free_RVs .append (var .missing_values )
@@ -615,11 +621,12 @@ def makefn(self, outs, mode=None, *args, **kwargs):
615
621
-------
616
622
Compiled Theano function
617
623
"""
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 )
623
630
624
631
def fn (self , outs , mode = None , * args , ** kwargs ):
625
632
"""Compiles a Theano function which returns the values of `outs`
0 commit comments