pymc-devs / pymc

Bayesian Modeling and Probabilistic Programming in Python
https://docs.pymc.io/
Other
8.72k stars 2.02k forks source link

Dirichlet distribution does not infer shape #824

Closed MCGallaspy closed 8 years ago

MCGallaspy commented 9 years ago

On the master branch, just downloaded today, the following script

import numpy as np
import pymc3

model = pymc3.Model()

with model:
    theta = pymc3.Dirichlet("theta", np.ones(3))

Gives me this error:

Traceback (most recent call last):
  File "error_test.py", line 7, in <module>
    theta = pymc3.Dirichlet("theta", np.ones(3))
  File "C:\Users\Mike\Anaconda3\envs\tictactoe\lib\site-packages\pymc3\distributions\distribution.py", line 22, in __new__
    return model.Var(name, dist, data)
  File "C:\Users\Mike\Anaconda3\envs\tictactoe\lib\site-packages\pymc3\model.py", line 229, in Var
    var = TransformedRV(name=name, distribution=dist, model=self, transform=dist.transform)
  File "C:\Users\Mike\Anaconda3\envs\tictactoe\lib\site-packages\pymc3\model.py", line 589, in __init__
    self.transformed = model.Var(name + "_" + transform.name, transform.apply(distribution))
  File "C:\Users\Mike\Anaconda3\envs\tictactoe\lib\site-packages\pymc3\distributions\transforms.py", line 25, in apply
    return TransformedDistribution.dist(dist, self)
  File "C:\Users\Mike\Anaconda3\envs\tictactoe\lib\site-packages\pymc3\distributions\distribution.py", line 34, in dist
    dist.__init__(*args, **kwargs)
  File "C:\Users\Mike\Anaconda3\envs\tictactoe\lib\site-packages\pymc3\distributions\transforms.py", line 52, in __init__
    v = forward(FreeRV(name='v', distribution=dist))
  File "C:\Users\Mike\Anaconda3\envs\tictactoe\lib\site-packages\pymc3\model.py", line 438, in __init__
    self.logp_elemwiset = distribution.logp(self)
  File "C:\Users\Mike\Anaconda3\envs\tictactoe\lib\site-packages\pymc3\distributions\multivariate.py", line 107, in logp
    sum(logpow(value, a - 1) - gammaln(a), axis=0) + gammaln(sum(a)),
  File "C:\Users\Mike\Anaconda3\envs\tictactoe\lib\site-packages\pymc3\distributions\dist_math.py", line 56, in logpow
    return switch(eq(x, 0) & eq(m, 0), 0, m * log(x))
  File "C:\Users\Mike\Anaconda3\envs\tictactoe\lib\site-packages\theano\gof\op.py", line 517, in __call__
    storage_map[ins] = [self._get_test_value(ins)]
  File "C:\Users\Mike\Anaconda3\envs\tictactoe\lib\site-packages\theano\gof\op.py", line 454, in _get_test_value
    ret = v.type.filter(v.tag.test_value)
  File "C:\Users\Mike\Anaconda3\envs\tictactoe\lib\site-packages\theano\tensor\type.py", line 169, in filter
    data.shape))
TypeError: For compute_test_value, one input test value does not have the requested type.

The error when converting the test value to that variable type:
Wrong number of dimensions: expected 0, got 1 with shape (3,).

By specifying the shape parameter (theta = pymc3.Dirichlet("theta", np.ones(3), shape=3)), it works fine. Any reason we can't infer the shape from the parameter a?

amcknight commented 8 years ago

The shape isn't inferred for an array of Dirichlets:

import numpy as np
import pymc3 as pm

num_alphas_per_dirichlet = 4

# One Dirichlet
with pm.Model():
    alphas = np.ones(num_alphas_per_dirichlet)
    p = pm.Dirichlet('p', alphas)

# Three Dirichlets
with pm.Model():
    num_dirichlets = 3
    alphas = np.ones((num_dirichlets, num_alphas_per_dirichlet))
    p = pm.Dirichlet('p', alphas, shape=num_dirichlets)

The second Model throws an error, not inferring the shape of the alpha parameters:

ValueError: operands could not be broadcast together with shapes (3,) (3,4)
fonnesbeck commented 8 years ago

Correct, nor does this occur for any multvariate distributions at present. It is something that we have discussed and need to tackle soon. (See #535) Your input is welcome.