Skip to content
Advertisement

How to calculate factorial in tensorflow?

I am new to tensorflow, I am trying to find a function that calculates the n!. I saw that one can use the gamma function, which was possible with theano, but did not work for tensorflow.

JavaScript

I am using a for loop to multiply number from n to 1, but I assume there is an easier and faster way. I saw functions related to gamma distribution, but couldn’t figure out how to calculate the factorial. Would appreciate if one can point me to some documentation.

Here is the way I do it now

JavaScript

Output is

JavaScript

Advertisement

Answer

Try this: tf.exp(tf.lgamma(x + 1)).

tf.lgamma computes the log of the absolute value of Gamma(x) element-wise, so the exponent will give you the raw Gamma(x) value:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement