EDIT: I solved the problem by now. At least I’m no longer getting an error at instantiation of the RV.
There were two problems in my code:
- I was using tt.log inside the custom op (which apparently leads to problems)
- The otype of course needs to be a float and not an int type.
Original message below for reference.
Thanks again for the help!
Jan
Dear @lucianopaz, dear all,
I think this helped a bit. I’m now running into the next problems. I have not worked tẃith theano much so far … So any help is really appreciated.
This is how the op looks now:
@as_op(itypes=[tt.lscalar, tt.iscalar, tt.iscalar, tt.iscalar, tt.dscalar],
otypes=[tt.lscalar])
def wallenius_logp(value, n, m ,N ,odds)
vwalprob = np.vectorize(wallenius.wallenius_prob)
p = vwalprob(value, n, m, N, odds)
return tt.log(p)
Where wallenius.wallenius_prob is the wrapped c++ funtion.
I’m unsure about the itypes and otypes of the op. For testing, I thought going with scalars (and extend to multidimensional types later). But maybe that’s already a problem?
This is the distribution’s logp function
def logp(self, value):
n = self.n
m = self.m
N = self.N
odds = self.odds
return bound(
wallenius_logp(value, n, m, N, odds),
0 <= value, value <= n,
0 <= m, m <= N)
And here is a trace of the error I get now:
with pymc3.Model():
w = pymc3.Wallenius(‘wal’, 20,20,40,0.5)
leads to the error message below. The last part is maybe the most informative …
Any further hints are highly welcome.
Many thanks
Jan
Error message:
`---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in
1 import pymc3; m = pymc3.Model();
2 with m:
----> 3 w = pymc3.Wallenius(‘wal’, 20,20,40,0.5)
4
~/D/test/pymc3/pymc3/distributions/distribution.py in new(cls, name, *args, **kwargs)
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:
48 raise TypeError(“Name needs to be a string but got: {}”.format(name))
~/D/test/pymc3/pymc3/model.py in Var(self, name, dist, data, total_size)
824 with self:
825 var = FreeRV(name=name, distribution=dist,
–> 826 total_size=total_size, model=self)
827 self.free_RVs.append(var)
828 else:
~/D/test/pymc3/pymc3/model.py in init(self, type, owner, index, name, distribution, total_size, model)
1272 self.tag.test_value = np.ones(
1273 distribution.shape, distribution.dtype) * distribution.default()
-> 1274 self.logp_elemwiset = distribution.logp(self)
1275 # The logp might need scaling in minibatches.
1276 # This is done in Factor
.
~/D/test/pymc3/pymc3/distributions/discrete.py in logp(self, value)
102 wallenius_logp(value, n, m, N, odds),
103 0 <= value, value <= n,
–> 104 0 <= m, m <= N)
105
106 def repr_latex(self, name=None, dist=None):
~/D/test/pymc3/pymc3/distributions/dist_math.py in bound(logp, *conditions, **kwargs)
48 alltrue = alltrue_scalar
49
—> 50 return tt.switch(alltrue(conditions), logp, -np.inf)
51
52
~/anaconda3/envs/dycose/lib/python3.7/site-packages/theano/gof/op.py in call(self, *inputs, **kwargs)
623 for i, ins in enumerate(node.inputs):
624 try:
–> 625 storage_map[ins] = [self._get_test_value(ins)]
626 compute_map[ins] = [True]
627 except AttributeError:
~/anaconda3/envs/dycose/lib/python3.7/site-packages/theano/gof/op.py in _get_test_value(cls, v)
560 # ensure that the test value is correct
561 try:
–> 562 ret = v.type.filter(v.tag.test_value)
563 except Exception as e:
564 # Better error message.
~/anaconda3/envs/dycose/lib/python3.7/site-packages/theano/tensor/type.py in filter(self, data, strict, allow_downcast)
85 if isinstance(data, Variable):
86 raise TypeError(
—> 87 'Expected an array-like object, but found a Variable: ’
88 'maybe you are trying to call a function on a (possibly ’
89 ‘shared) variable instead of a numeric array?’)
TypeError: For compute_test_value, one input test value does not have the requested type.
Backtrace when that variable is created:
File “/home/jeti/anaconda3/envs/dycose/lib/python3.7/site-packages/IPython/core/interactiveshell.py”, line 3020, in run_cell_async
interactivity=interactivity, compiler=compiler, result=result)
File “/home/jeti/anaconda3/envs/dycose/lib/python3.7/site-packages/IPython/core/interactiveshell.py”, line 3185, in run_ast_nodes
if (yield from self.run_code(code, result)):
File “/home/jeti/anaconda3/envs/dycose/lib/python3.7/site-packages/IPython/core/interactiveshell.py”, line 3267, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File “”, line 3, in
w = pymc3.Wallenius(‘wal’, 20,20,40,0.5)
File “/home/jeti/D/test/pymc3/pymc3/distributions/distribution.py”, line 46, in new
return model.Var(name, dist, data, total_size)
File “/home/jeti/D/test/pymc3/pymc3/model.py”, line 826, in Var
total_size=total_size, model=self)
File “/home/jeti/D/test/pymc3/pymc3/model.py”, line 1274, in init
self.logp_elemwiset = distribution.logp(self)
File “/home/jeti/D/test/pymc3/pymc3/distributions/discrete.py”, line 102, in logp
wallenius_logp(value, n, m, N, odds),
The error when converting the test value to that variable type:
Expected an array-like object, but found a Variable: maybe you are trying to call a function on a (possibly shared) variable instead of a numeric array?
`