Search code examples
pythontheanofactorial

How to calculate factorial in Theano


Here is a simple example.

import theano 
v = theano.tensor.scalar("variable") 
factorial = ? 
# Calculate factorial of v , like numpy.math.factorial(v), but it didn't work, 
# Actually it should be a integer as a parameter of numpy.math.factorial function 
result = theano.function([v], factorial)

There is few useful information on Internet. Something I found like http://matthewrocklin.com/blog//work/2013/08/14/SymPy-Theano-part-4 which told me it belonged to "the future work", which means there is no solution for this problem. How could that possible? I mean is there no body need calculate factorial in your theano project? I had this problem when I designed a RBM with Poisson conditional possibility which needs calculate factorial of visible units in the energy function.


Solution

  • I have found the answer that we use gamma function to solve the factorial problem. :( Sorry for bothering everybody! Here is the detail: https://groups.google.com/forum/#!msg/theano-users/ytqep8AickA/cE7QeJZxXzUJ

    So here should be:

    import theano 
    v = theano.tensor.scalar("variable") 
    factorial = theano.tensor.gamma(v)
    # Calculate factorial of v , like numpy.math.factorial(v), but it didn't work, 
    # Actually it should be a integer as a parameter of numpy.math.factorial function 
    result = theano.function([v], factorial)