Skip to content

Support for special functions #291

Closed

Description

@Bonnevie

If the intent is for TensorFlow to be a general purpose computational tool for machine learning there should be more support for special functions (i.e. the functions in scipy.special). In particular, functions occurring in probability distributions such as the gamma(ln) are quite central to probabilistic machine learning.

Activity

  1. girving commented on Nov 19, 2015

    @girving
    Contributor

    Thanks, special functions are well worth adding, and we'd welcome contributions in this direction.

  2. self-assigned this
    on Nov 27, 2015
  3. akuz commented on Dec 27, 2015

    @akuz

    I was just thinking of implementing some Bayesian learning on TensorFlow and realised that I would need the Gamma and also in particular LogGamma (this is just the log of the Gamma function computed directly). These are absolutely essential for many distributions (Beta, Dirichlet). I would love to help to add them, however could you please provide some small guidance on where to start? I see there are many levels of op implementation, description, Python wrapping etc. Thanks

  4. vrv commented on Dec 28, 2015

    @vrv

    @ebrevdo is already half-way to implementing this, I think (I think he added it to Eigen for both CPU and GPU? not sure if it's been upstreamed yet).

  5. ebrevdo commented on Dec 28, 2015

    @ebrevdo
    Contributor

    Yeah; this has been implemented and should make it into the next upstream release. I've added lgamma to Eigen (both ours & upstream) and added an lgamma unary op.

    Digamma should also make its way in, in the next 2 weeks.

  6. akuz commented on Dec 28, 2015

    @akuz

    @ebrevdo is it possible for me to pull the lgamma implementation from your repo? I can't see it in your profile, perhaps it's internal at your company, and I can't just pull changes from it? I really need lgamma soon, can't wait ;)

  7. akuz commented on Dec 28, 2015

    @akuz

    @ebrevdo hi, I saw you implemented erf, erfc, and lgamma in Eigen. Why not also implement tgamma from the same section of "cmath" header, just so that all four functions from "Error and gamma functions" section would be available? http://en.cppreference.com/w/cpp/header/cmath

    Looking forward for them to become available as TensorFlow operations! Thank you

  8. davmre commented on Jan 3, 2016

    @davmre

    @akuz I've been using a hack where you just define a series expansion within the TF graph. It's not pretty and I don't guarantee accuracy, but if you really can't wait for proper support it might be worth a try:

    def gammaln(x):
        # fast approximate gammaln from paul mineiro
        # http://www.machinedlearnings.com/2011/06/faster-lda.html
        logterm = tf.log (x * (1.0 + x) * (2.0 + x))
        xp3 = 3.0 + x
        return -2.081061466 - x + 0.0833333 / xp3 - logterm + (2.5 + x) * tf.log (xp3)
    
  9. ebrevdo commented on Jan 3, 2016

    @ebrevdo
    Contributor

    It'll get pushed to the public repo next week.
    On Jan 3, 2016 8:49 AM, "David Moore" [email protected] wrote:

    @akuz https://github.com/akuz I've been using a hack where you just
    define a series expansion within the TF graph. It's not pretty and I don't
    guarantee accuracy, but if you really can't wait for proper support it
    might be worth a try:

    def gammaln(x):
    # fast approximate gammaln from paul mineiro
    # http://www.machinedlearnings.com/2011/06/faster-lda.html
    logterm = tf.log (x * (1.0 + x) * (2.0 + x))
    xp3 = 3.0 + x
    return -2.081061466 - x + 0.0833333 / xp3 - logterm + (2.5 + x) * tf.log (xp3)

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

  10. ebrevdo commented on Mar 9, 2016

    @ebrevdo
    Contributor

    CPU and GPU versions of lgamma, digamma, erf, and erfc are in, igamma/igammac/betainc are coming soon. Marking as done.

  11. drao2 commented on Mar 15, 2016

    @drao2

    Sorry to dig this up again, but are there any plans to incorporate the polygamma function (1) in the near future? Anything using a Dirichlet / Beta prior distribution will utilise the digamma function, for which the gradient is the polygamma.

  12. ebrevdo commented on Mar 15, 2016

    @ebrevdo
    Contributor

    As far as I can tell, the digamma function is only used for entropy and
    other log-expectation calculations. The rest use gamma or log-gamma.

    On Tue, Mar 15, 2016 at 4:48 AM, dush64 [email protected] wrote:

    Sorry to dig this up again, but are there any plans to incorporate the
    polygamma function (1) in the near future? Anything using a Dirichlet /
    Beta prior distribution will utilise the digamma function, for which the
    gradient is the polygamma.

    —
    You are receiving this because you modified the open/close state.
    Reply to this email directly or view it on GitHub:
    #291 (comment)

  13. drao2 commented on Mar 15, 2016

    @drao2

    That's true - sorry, should have been more specific. The digamma/polygamma come up when computing expectations and KL divergences for dirichlet distributions. Is this general enough to warrant a place in Tensorflow, or should I be independently implementing it (or contributing it)?

  14. ebrevdo commented on Mar 15, 2016

    @ebrevdo
    Contributor

    it is worthwhile to add trigamma or polygamma from cephes. you will have to add it to Eigen first, and verify CUDA support works. see bitbucket.org/eigen/eigen/ and specifically my PRs adding lgamma/digamma/igamma/igammac/erf/errfc to that repository. happy to review your code there.

  15. girving commented on Mar 15, 2016

    @girving
    Contributor

    However, let's split any new special functions off into a new issue.

  16. ebrevdo commented on Mar 15, 2016

    @ebrevdo
    Contributor

    Agreed; this issue is closed. First push changes to Eigen and then open an issue to bring tri/polygamma into TensorFlow once that's done.

  17. added
    type:feature
    and removed on Feb 9, 2017
  18. added a commit that references this issue on Dec 4, 2019
    45dc6b7
  19. added a commit that references this issue on Jan 23, 2020
  20. added a commit that references this issue on May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type:feature

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions