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

Manually set theano TensorType for length 1 shared variables #3335

Merged
merged 7 commits into from
Jan 15, 2019

Conversation

mattpitkin
Copy link
Contributor

In #3122 it shows an error caused when trying to impute the values of a single missing variable. The error thrown by theano is, e.g.:

TypeError: Cannot convert Type TensorType(int64, vector) (of Variable obs_t_minus_1_missing_missing_shared__) into Type TensorType(int64, (True,)). You can try to manually convert obs_t_minus_1_missing_missing_shared__ into a TensorType(int64, (True,)).

This PR fixes the problem by doing as the theano error suggests. So when shared variables are being set they are checked to see if they are arrays with length 1, and if so the TensorType of the shared variable is changes.

This is a slight hack, but hopefully one that works. I have added a comment above the change in the code, but maybe this should be changed to a UserWarning message?

mattpitkin and others added 3 commits January 9, 2019 22:48
 - this patch fixes an issue highlighted in pymc-devs#3122
   where imputation of a single missing observation fails.
   It implements the suggestion from the theano error message
   to manual force a TensorType change in cases where the
   variable has a length of one.
@twiecki
Copy link
Member

twiecki commented Jan 14, 2019

Thanks @mattpitkin, can you add a test for the bug you ran into?

@junpenglao junpenglao requested a review from lucianopaz January 14, 2019 11:01
@mattpitkin
Copy link
Contributor Author

@twiecki Where would be best to add the test? test_model.py? Or, do you just mean an example test on this PR page?

For the latter, a test would be, which should fail if my patch has not been applied:

import pymc3 as pm
import numpy as np
from scipy.stats import bernoulli

true_p = 0.5
nobs = 10

# draw some observations
obs = bernoulli.rvs(true_p, size=nobs)

# mask one of the observations
obs[0] = -1  # mask first observation
obs = np.ma.masked_values(obs, value=-1)

with pm.Model() as model:
    prob = pm.Uniform('prob', 0., 1.0)
    pdraws = pm.Bernoulli('draws', p=prob, observed=obs)
    trace = pm.sample()

@twiecki
Copy link
Member

twiecki commented Jan 14, 2019

Yes, in test_model.py with a description and link to this issue.

@mattpitkin
Copy link
Contributor Author

I've added a test in e93efce. Let me know if that test looks ok.

@twiecki
Copy link
Member

twiecki commented Jan 15, 2019

Looks great, can you add a note to the release notes?

@mattpitkin
Copy link
Contributor Author

Sure, I've added a comment in 2ae431e

RELEASE-NOTES.md Outdated Show resolved Hide resolved
fix typo

Co-Authored-By: mattpitkin <[email protected]>
@twiecki twiecki merged commit f245f11 into pymc-devs:master Jan 15, 2019
@twiecki
Copy link
Member

twiecki commented Jan 15, 2019

Thanks!

@mattpitkin mattpitkin deleted the one_missing_data_pooint_fix branch January 15, 2019 22:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants