Skip to content

Use model context in model.logpt #2255

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

Merged
merged 4 commits into from
Jun 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
530 changes: 231 additions & 299 deletions docs/source/notebooks/getting_started.ipynb

Large diffs are not rendered by default.

209 changes: 69 additions & 140 deletions docs/source/notebooks/rugby_analytics.ipynb

Large diffs are not rendered by default.

47 changes: 27 additions & 20 deletions pymc3/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,15 +455,17 @@ def dlogp_array(self):
@memoize
def logpt(self):
"""Theano scalar of log-probability of the model"""
factors = [var.logpt for var in self.basic_RVs] + self.potentials
return tt.add(*map(tt.sum, factors))
with self:
factors = [var.logpt for var in self.basic_RVs] + self.potentials
return tt.add(*map(tt.sum, factors))

@property
def varlogpt(self):
"""Theano scalar of log-probability of the unobserved random variables
(excluding deterministic)."""
factors = [var.logpt for var in self.vars]
return tt.add(*map(tt.sum, factors))
with self:
factors = [var.logpt for var in self.vars]
return tt.add(*map(tt.sum, factors))

@property
def vars(self):
Expand Down Expand Up @@ -521,14 +523,16 @@ def Var(self, name, dist, data=None, total_size=None):
name = self.name_for(name)
if data is None:
if getattr(dist, "transform", None) is None:
var = FreeRV(name=name, distribution=dist,
total_size=total_size, model=self)
with self:
var = FreeRV(name=name, distribution=dist,
total_size=total_size, model=self)
self.free_RVs.append(var)
else:
var = TransformedRV(name=name, distribution=dist,
transform=dist.transform,
total_size=total_size,
model=self)
with self:
var = TransformedRV(name=name, distribution=dist,
transform=dist.transform,
total_size=total_size,
model=self)
pm._log.debug('Applied {transform}-transform to {name}'
' and added transformed {orig_name} to model.'.format(
transform=dist.transform.name,
Expand All @@ -537,18 +541,20 @@ def Var(self, name, dist, data=None, total_size=None):
self.deterministics.append(var)
return var
elif isinstance(data, dict):
var = MultiObservedRV(name=name, data=data, distribution=dist,
total_size=total_size, model=self)
with self:
var = MultiObservedRV(name=name, data=data, distribution=dist,
total_size=total_size, model=self)
self.observed_RVs.append(var)
if var.missing_values:
self.free_RVs += var.missing_values
self.missing_values += var.missing_values
for v in var.missing_values:
self.named_vars[v.name] = v
else:
var = ObservedRV(name=name, data=data,
distribution=dist,
total_size=total_size, model=self)
with self:
var = ObservedRV(name=name, data=data,
distribution=dist,
total_size=total_size, model=self)
self.observed_RVs.append(var)
if var.missing_values:
self.free_RVs.append(var.missing_values)
Expand Down Expand Up @@ -615,11 +621,12 @@ def makefn(self, outs, mode=None, *args, **kwargs):
-------
Compiled Theano function
"""
return theano.function(self.vars, outs,
allow_input_downcast=True,
on_unused_input='ignore',
accept_inplace=True,
mode=mode, *args, **kwargs)
with self:
return theano.function(self.vars, outs,
allow_input_downcast=True,
on_unused_input='ignore',
accept_inplace=True,
mode=mode, *args, **kwargs)

def fn(self, outs, mode=None, *args, **kwargs):
"""Compiles a Theano function which returns the values of `outs`
Expand Down
3 changes: 3 additions & 0 deletions pymc3/tests/test_dist_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def test_logp(self):
logp = logp_f(cov_val, delta_val)
npt.assert_allclose(logp, expect)

@theano.configparser.change_flags(compute_test_value="ignore")
def test_grad(self):
np.random.seed(42)

Expand Down Expand Up @@ -179,12 +180,14 @@ def test_hessian(self):


class TestSplineWrapper(object):
@theano.configparser.change_flags(compute_test_value="ignore")
def test_grad(self):
x = np.linspace(0, 1, 100)
y = x * x
spline = SplineWrapper(interpolate.InterpolatedUnivariateSpline(x, y, k=1))
utt.verify_grad(spline, [0.5])

@theano.configparser.change_flags(compute_test_value="ignore")
def test_hessian(self):
x = np.linspace(0, 1, 100)
y = x * x
Expand Down
1 change: 1 addition & 0 deletions pymc3/tests/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def setup_method(self):
self.op_class = LogDet
self.op = logdet

@theano.configparser.change_flags(compute_test_value="ignore")
def validate(self, input_mat):
x = theano.tensor.matrix()
f = theano.function([x], self.op(x))
Expand Down