Skip to content

Add wrapper for theano as_op #584

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

Closed
karlnapf opened this issue Aug 12, 2014 · 10 comments
Closed

Add wrapper for theano as_op #584

karlnapf opened this issue Aug 12, 2014 · 10 comments
Assignees

Comments

@karlnapf
Copy link

Hi there,

is there a way to make pymc3 use custom density functions that are not theano compatible?
Say I have some black box c-code to evaluate my log-pdf, and now I want to use this with one of pymc3's gradient free samplers such as metropolis.

Currently, trying this gives me things like object of type 'TensorVariable' has no len() when trying to use my own python code to evaluate a log-pdf (which assumes the input is a numpy array).

Thanks for your help and for the great package!

@twiecki
Copy link
Member

twiecki commented Aug 13, 2014

Hi Heiko,

Seems to be a dupe of #507. E.g.

@theano.compile.ops.as_op(itypes=[t.lscalar, t.dscalar, t.dscalar],otypes=[t.dvector])
def rate(switchpoint,early_mean, late_mean):
    ''' Concatenate Poisson means '''
    out = empty(years)
    out[:switchpoint] = early_mean
    out[switchpoint:] = late_mean
    return out

Let me know if that works or doesn't work for you.

@twiecki
Copy link
Member

twiecki commented Aug 17, 2014

Closing, feel free to reopen if this doesn't work for you.

@twiecki twiecki closed this as completed Aug 17, 2014
@karlnapf
Copy link
Author

Hi Thomas,

thanks for the help, that fixed the problem. Amazing! Now I can do pseudo-marginal MCMC models using PyMC, which is something I am working on and would like to use PyMC for.

For others, here is how I did it:
http://nbviewer.ipython.org/gist/karlnapf/8ed7de163eed39e733da

@twiecki
Copy link
Member

twiecki commented Aug 19, 2014

Cool, thanks for sharing!

@twiecki
Copy link
Member

twiecki commented Aug 19, 2014

I also wonder if we should create a wrapper around @theano.compile.ops.as_op(itypes=[T.dscalar],otypes=[T.dscalar]) because it requires another call to pm.DensityDist('y', logp). Something along the lines of:

@CustomDensity(itypes=[T.dscalar],otypes=[T.dscalar])
def MyCustomDist():...

with model:
    my_dist = MyCustomDist('y', ...)

CC @fonnesbeck @jsalvatier

@jsalvatier
Copy link
Member

Yes, that's true. I wonder if it should be T.dvector though, since that
seems like a more common case. I like this idea.

On Tue, Aug 19, 2014 at 4:55 AM, Thomas Wiecki [email protected]
wrote:

I also wonder if we should create a wrapper around
@theano.compile.ops.as_op(itypes=[T.dscalar],otypes=[T.dscalar]) because
it requires another call to pm.DensityDist('y', logp). Something along
the lines of:

@CustomDensity(itypes=[T.dscalar],otypes=[T.dscalar])def MyCustomDist():...
with model:
my_dist = MyCustomDist('y', ...)

CC @fonnesbeck https://github.com/fonnesbeck @jsalvatier
https://github.com/jsalvatier


Reply to this email directly or view it on GitHub
#584 (comment).

@twiecki twiecki changed the title Custom densities Add wrapper for theano as_op Aug 20, 2014
@twiecki twiecki reopened this Aug 20, 2014
@fonnesbeck
Copy link
Member

I like it too. In favor of anything that hides the Theano complexity from most users.

@jsalvatier
Copy link
Member

Theano/Theano#3094 issue should make it easier to make as_op make theano ops.

@fonnesbeck
Copy link
Member

Is this still something we worry about? Its pretty easy to create custom distributions now.

@twiecki
Copy link
Member

twiecki commented Sep 5, 2016

Yeah, I think it's fine, haven't heard many complaints.

@twiecki twiecki closed this as completed Sep 5, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants