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

Quickstart regression example fails -- ValueError: setting an array element with a sequence. #3618

Closed
ahwillia opened this issue Sep 5, 2019 · 5 comments · Fixed by #3619

Comments

@ahwillia
Copy link

ahwillia commented Sep 5, 2019

Just installed pymc3 and am having some trouble. Some simple examples seem to be working, but the example on the front page fails:

import pymc3 as pm
import numpy.random as npr
X = npr.randn(100, 1)
w = npr.randn(1)
y = X @ w + (npr.randn(100) * .1)

with pm.Model() as linear_model:
    weights = pm.Normal('weights', mu=0, sigma=1)
    noise = pm.Gamma('noise', alpha=2, beta=1)
    y_observed = pm.Normal('y_observed',
                mu=X.dot(weights),
                sigma=noise,
                observed=y)

    posterior = pm.sample()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-f503c46c891e> in <module>
     11                 mu=X.dot(weights),
     12                 sigma=noise,
---> 13                 observed=y)
     14 
     15     posterior = pm.sample()

~/anaconda3/lib/python3.7/site-packages/pymc3/distributions/distribution.py in __new__(cls, name, *args, **kwargs)
     43                 raise TypeError("observed needs to be data but got: {}".format(type(data)))
     44             total_size = kwargs.pop('total_size', None)
---> 45             dist = cls.dist(*args, **kwargs)
     46             return model.Var(name, dist, data, total_size)
     47         else:

~/anaconda3/lib/python3.7/site-packages/pymc3/distributions/distribution.py in dist(cls, *args, **kwargs)
     54     def dist(cls, *args, **kwargs):
     55         dist = object.__new__(cls)
---> 56         dist.__init__(*args, **kwargs)
     57         return dist
     58 

~/anaconda3/lib/python3.7/site-packages/pymc3/distributions/continuous.py in __init__(self, mu, sigma, tau, sd, **kwargs)
    467         self.tau = tt.as_tensor_variable(tau)
    468 
--> 469         self.mean = self.median = self.mode = self.mu = mu = tt.as_tensor_variable(floatX(mu))
    470         self.variance = 1. / self.tau
    471 

~/anaconda3/lib/python3.7/site-packages/pymc3/theanof.py in floatX(X)
     63     """
     64     try:
---> 65         return X.astype(theano.config.floatX)
     66     except AttributeError:
     67         # Scalar passed

ValueError: setting an array element with a sequence.

Interestingly, the following simpler example does work:

with pm.Model() as linear_model:
    mu = pm.Normal('mu', mu=0, sigma=1)
    noise = pm.Gamma('noise', alpha=2, beta=1)
    y_observed = pm.Normal('y_observed',
                mu=mean,
                sigma=noise,
                observed=npr.randn(100))

    posterior = pm.sample()

pymc3 version: 3.7
theano version: 1.0.4
Python version: 3.7.1

Edit: I get the same error if I try different dimensions (e.g. w=npr.randn(100, 5))

@ColCarroll
Copy link
Member

Thank you for reporting - this is an important problem to fix!

I can confirm I reproduce it on my machine, and here is a simple fix (swapping X.dot(weights) for pm.math.dot(X, weights)) I would guess the problem was new numpy outrunning theano compatibility.

I can make a PR in a day or two if no one has a better fix (@fonnesbeck / @twiecki I think you two present the code the most):

import pymc3 as pm
import numpy.random as npr
X = npr.randn(100, 1)
w = npr.randn(1)
y = X @ w + (npr.randn(100) * .1)

with pm.Model() as linear_model:
    weights = pm.Normal('weights', mu=0, sigma=1)
    noise = pm.Gamma('noise', alpha=2, beta=1)
    y_observed = pm.Normal('y_observed',
                mu=pm.math.dot(X, weights),
                sigma=noise,
                observed=y)

    posterior = pm.sample()

@fonnesbeck
Copy link
Member

You can also just swap X and weights

weights.dot(X)

@ColCarroll
Copy link
Member

Didn't realize this was 1-d. What about X * weights (also works), so that it doesn't make us linear algebra fiends sad?

@ahwillia
Copy link
Author

ahwillia commented Sep 5, 2019

Sorry I didn't mean to show the 1-d example -- the same error also happens for N-d. I was just curious if the error persisted for 1-d.

Swapping X and weights does seem to work. Why this is the case isn't clear to me...

Thanks for the quick responses!

@ColCarroll
Copy link
Member

This was a good example! I suspect the problem is that something in theano breaks with new numpy arrays. weights.dot(X) returns a theano object, but X.dot(weights) returns a numpy object, I think.

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 a pull request may close this issue.

3 participants