{"action":"created","issue":{"url":"https://api.github.com/repos/pymc-devs/pymc3/issues/1492","repository_url":"https://api.github.com/repos/pymc-devs/pymc3","labels_url":"https://api.github.com/repos/pymc-devs/pymc3/issues/1492/labels{/name}","comments_url":"https://api.github.com/repos/pymc-devs/pymc3/issues/1492/comments","events_url":"https://api.github.com/repos/pymc-devs/pymc3/issues/1492/events","html_url":"https://github.com/pymc-devs/pymc3/issues/1492","id":186513584,"number":1492,"title":"Uniformative Theano error","user":{"login":"rogerthealien","id":2500192,"avatar_url":"https://avatars.githubusercontent.com/u/2500192?v=3","gravatar_id":"","url":"https://api.github.com/users/rogerthealien","html_url":"https://github.com/rogerthealien","followers_url":"https://api.github.com/users/rogerthealien/followers","following_url":"https://api.github.com/users/rogerthealien/following{/other_user}","gists_url":"https://api.github.com/users/rogerthealien/gists{/gist_id}","starred_url":"https://api.github.com/users/rogerthealien/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rogerthealien/subscriptions","organizations_url":"https://api.github.com/users/rogerthealien/orgs","repos_url":"https://api.github.com/users/rogerthealien/repos","events_url":"https://api.github.com/users/rogerthealien/events{/privacy}","received_events_url":"https://api.github.com/users/rogerthealien/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":6,"created_at":"2016-11-01T11:34:47Z","updated_at":"2016-11-01T12:46:36Z","closed_at":null,"body":"I'm trying to translate [Statistical Rethinking](http://xcelab.net/rm/statistical-rethinking/) from R and RStan to Python and PyMC3.\r\n\r\nOn page 304, there's a simple logistic regression example. Data being used (publicly available):\r\n\r\n```\r\n\tdept\tapplicant.gender\tadmit\treject\tapplications\tis_male\r\n1\tA\tmale\t512\t313\t825\t1\r\n2\tA\tfemale\t89\t19\t108\t0\r\n3\tB\tmale\t353\t207\t560\t1\r\n4\tB\tfemale\t17\t8\t25\t0\r\n5\tC\tmale\t120\t205\t325\t1\r\n6\tC\tfemale\t202\t391\t593\t0\r\n7\tD\tmale\t138\t279\t417\t1\r\n8\tD\tfemale\t131\t244\t375\t0\r\n9\tE\tmale\t53\t138\t191\t1\r\n10\tE\tfemale\t94\t299\t393\t0\r\n11\tF\tmale\t22\t351\t373\t1\r\n12\tF\tfemale\t24\t317\t341\t0\r\n```\r\n\r\nData is stored in a pandas DataFrame object. When I try and fit the model using:\r\n\r\n```\r\nwith pm.Model() as m106:\r\n    \r\n    alpha = pm.Normal('alpha', 0, 10)\r\n    beta_m = pm.Normal('beta_m', 0, 10)\r\n    \r\n    lin = alpha + beta_m * data['is_male']\r\n    p = np.exp(lin) / (1 + np.exp(lin))\r\n    \r\n    admit = pm.Binomial('admit', n=data['applications'], p=p, observed=data['admit'])\r\n    \r\n    m106_map = pm.find_MAP()\r\n    m106_traces = pm.sample(1000, start=m106_map)\r\n```\r\n\r\nI get the following error (which seems similar to #918): \r\n\r\n```\r\n---------------------------------------------------------------------------\r\nKeyError                                  Traceback (most recent call last)\r\n/Users/horatiu/anaconda/lib/python3.5/site-packages/theano/tensor/type.py in dtype_specs(self)\r\n    266                 'complex64': (complex, 'theano_complex64', 'NPY_COMPLEX64')\r\n--> 267             }[self.dtype]\r\n    268         except KeyError:\r\n\r\nKeyError: 'object'\r\n\r\nDuring handling of the above exception, another exception occurred:\r\n\r\nTypeError                                 Traceback (most recent call last)\r\n/Users/horatiu/anaconda/lib/python3.5/site-packages/theano/tensor/basic.py in constant_or_value(x, rtype, name, ndim, dtype)\r\n    407             rval = rtype(\r\n--> 408                 TensorType(dtype=x_.dtype, broadcastable=bcastable),\r\n    409                 x_.copy(),\r\n\r\n/Users/horatiu/anaconda/lib/python3.5/site-packages/theano/tensor/type.py in __init__(self, dtype, broadcastable, name, sparse_grad)\r\n     49         self.broadcastable = tuple(bool(b) for b in broadcastable)\r\n---> 50         self.dtype_specs()  # error checking is done there\r\n     51         self.name = name\r\n\r\n/Users/horatiu/anaconda/lib/python3.5/site-packages/theano/tensor/type.py in dtype_specs(self)\r\n    269             raise TypeError(\"Unsupported dtype for %s: %s\"\r\n--> 270                             % (self.__class__.__name__, self.dtype))\r\n    271 \r\n\r\nTypeError: Unsupported dtype for TensorType: object\r\n\r\nDuring handling of the above exception, another exception occurred:\r\n\r\nTypeError                                 Traceback (most recent call last)\r\n/Users/horatiu/anaconda/lib/python3.5/site-packages/theano/tensor/basic.py in as_tensor_variable(x, name, ndim)\r\n    201     try:\r\n--> 202         return constant(x, name=name, ndim=ndim)\r\n    203     except TypeError:\r\n\r\n/Users/horatiu/anaconda/lib/python3.5/site-packages/theano/tensor/basic.py in constant(x, name, ndim, dtype)\r\n    421     ret = constant_or_value(x, rtype=TensorConstant, name=name, ndim=ndim,\r\n--> 422                             dtype=dtype)\r\n    423 \r\n\r\n/Users/horatiu/anaconda/lib/python3.5/site-packages/theano/tensor/basic.py in constant_or_value(x, rtype, name, ndim, dtype)\r\n    416     except Exception:\r\n--> 417         raise TypeError(\"Could not convert %s to TensorType\" % x, type(x))\r\n    418 \r\n\r\nTypeError: ('Could not convert 1     Elemwise{mul,no_inplace}.0\\n2     Elemwise{mul,no_inplace}.0\\n3     Elemwise{mul,no_inplace}.0\\n4     Elemwise{mul,no_inplace}.0\\n5     Elemwise{mul,no_inplace}.0\\n6     Elemwise{mul,no_inplace}.0\\n7     Elemwise{mul,no_inplace}.0\\n8     Elemwise{mul,no_inplace}.0\\n9     Elemwise{mul,no_inplace}.0\\n10    Elemwise{mul,no_inplace}.0\\n11    Elemwise{mul,no_inplace}.0\\n12    Elemwise{mul,no_inplace}.0\\nName: applications, dtype: object to TensorType', <class 'pandas.core.series.Series'>)\r\n\r\nDuring handling of the above exception, another exception occurred:\r\n\r\nAsTensorError                             Traceback (most recent call last)\r\n<ipython-input-144-fb615dfa2e93> in <module>()\r\n      7     p = np.exp(lin) / (1 + np.exp(lin))\r\n      8 \r\n----> 9     admit = pm.Binomial('admit', n=data['applications'], p=p, observed=data['admit'])\r\n     10 \r\n     11     m106_map = pm.find_MAP()\r\n\r\n/Users/horatiu/anaconda/lib/python3.5/site-packages/pymc3/distributions/distribution.py in __new__(cls, name, *args, **kwargs)\r\n     24         if isinstance(name, string_types):\r\n     25             data = kwargs.pop('observed', None)\r\n---> 26             dist = cls.dist(*args, **kwargs)\r\n     27             return model.Var(name, dist, data)\r\n     28         elif name is None:\r\n\r\n/Users/horatiu/anaconda/lib/python3.5/site-packages/pymc3/distributions/distribution.py in dist(cls, *args, **kwargs)\r\n     37     def dist(cls, *args, **kwargs):\r\n     38         dist = object.__new__(cls)\r\n---> 39         dist.__init__(*args, **kwargs)\r\n     40         return dist\r\n     41 \r\n\r\n/Users/horatiu/anaconda/lib/python3.5/site-packages/pymc3/distributions/discrete.py in __init__(self, n, p, *args, **kwargs)\r\n     43         self.n = n\r\n     44         self.p = p\r\n---> 45         self.mode = tt.cast(tt.round(n * p), self.dtype)\r\n     46 \r\n     47     def random(self, point=None, size=None, repeat=None):\r\n\r\n/Users/horatiu/anaconda/lib/python3.5/site-packages/theano/tensor/basic.py in round(a, mode)\r\n   2052     \"\"\"round_mode(a) with mode in [half_away_from_zero, half_to_even]\"\"\"\r\n   2053     if mode == \"half_away_from_zero\":\r\n-> 2054         return round_half_away_from_zero(a)\r\n   2055     elif mode == \"half_to_even\":\r\n   2056         return round_half_to_even(a)\r\n\r\n/Users/horatiu/anaconda/lib/python3.5/site-packages/theano/gof/op.py in __call__(self, *inputs, **kwargs)\r\n    609         \"\"\"\r\n    610         return_list = kwargs.pop('return_list', False)\r\n--> 611         node = self.make_node(*inputs, **kwargs)\r\n    612 \r\n    613         if config.compute_test_value != 'off':\r\n\r\n/Users/horatiu/anaconda/lib/python3.5/site-packages/theano/tensor/elemwise.py in make_node(self, *inputs)\r\n    541         using DimShuffle.\r\n    542         \"\"\"\r\n--> 543         inputs = list(map(as_tensor_variable, inputs))\r\n    544         shadow = self.scalar_op.make_node(\r\n    545             *[get_scalar_type(dtype=i.type.dtype).make_variable()\r\n\r\n/Users/horatiu/anaconda/lib/python3.5/site-packages/theano/tensor/basic.py in as_tensor_variable(x, name, ndim)\r\n    206         except Exception:\r\n    207             str_x = repr(x)\r\n--> 208         raise AsTensorError(\"Cannot convert %s to TensorType\" % str_x, type(x))\r\n    209 \r\n    210 # this has a different name, because _as_tensor_variable is the\r\n\r\nAsTensorError: ('Cannot convert 1     Elemwise{mul,no_inplace}.0\\n2     Elemwise{mul,no_inplace}.0\\n3     Elemwise{mul,no_inplace}.0\\n4     Elemwise{mul,no_inplace}.0\\n5     Elemwise{mul,no_inplace}.0\\n6     Elemwise{mul,no_inplace}.0\\n7     Elemwise{mul,no_inplace}.0\\n8     Elemwise{mul,no_inplace}.0\\n9     Elemwise{mul,no_inplace}.0\\n10    Elemwise{mul,no_inplace}.0\\n11    Elemwise{mul,no_inplace}.0\\n12    Elemwise{mul,no_inplace}.0\\nName: applications, dtype: object to TensorType', <class 'pandas.core.series.Series'>)\r\n```\r\nThe error is not very informative, and doesn't seem to point to my code. \r\nBut when using:\r\n\r\n```\r\nwith pm.Model() as m106:\r\n    \r\n    alpha = pm.Normal('alpha', 0, 10)\r\n    beta_m = pm.Normal('beta_m', 0, 10)\r\n    \r\n    lin = alpha + beta_m * data['is_male']\r\n    p = np.exp(lin) / (1 + np.exp(lin))\r\n    \r\n    admit = pm.Binomial('admit', n=data['applications'].values, p=p, observed=data['admit'])\r\n    \r\n    m106_map = pm.find_MAP()\r\n    m106_traces = pm.sample(1000, start=m106_map)\r\n```\r\n\r\nSo explicitly passing in the numpy array rather than the pandas Series:\r\n\r\n```\r\nadmit = pm.Binomial('admit', n=data['applications'].values, p=p, observed=data['admit'])\r\n```\r\n\r\nEverything works as expected. I'm just trying to figure out why that is? Is there a reference in the documentation for this behavior? What is the lesson I should take away from this? :)\r\n\r\nUsing pandas 0.19, numpy 1.11, pymc3.0rc2\r\n"},"comment":{"url":"https://api.github.com/repos/pymc-devs/pymc3/issues/comments/257557902","html_url":"https://github.com/pymc-devs/pymc3/issues/1492#issuecomment-257557902","issue_url":"https://api.github.com/repos/pymc-devs/pymc3/issues/1492","id":257557902,"user":{"login":"twiecki","id":674200,"avatar_url":"https://avatars.githubusercontent.com/u/674200?v=3","gravatar_id":"","url":"https://api.github.com/users/twiecki","html_url":"https://github.com/twiecki","followers_url":"https://api.github.com/users/twiecki/followers","following_url":"https://api.github.com/users/twiecki/following{/other_user}","gists_url":"https://api.github.com/users/twiecki/gists{/gist_id}","starred_url":"https://api.github.com/users/twiecki/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/twiecki/subscriptions","organizations_url":"https://api.github.com/users/twiecki/orgs","repos_url":"https://api.github.com/users/twiecki/repos","events_url":"https://api.github.com/users/twiecki/events{/privacy}","received_events_url":"https://api.github.com/users/twiecki/received_events","type":"User","site_admin":false},"created_at":"2016-11-01T12:46:36Z","updated_at":"2016-11-01T12:46:36Z","body":"Interestingly, it works if you pass it to `observed`. I think we have some logic in place there as it's the more common use case. "}}
{"actor":{"display_login":"twiecki"}}