Reputation: 43
I am new to tensorflow, i wish to apply a scipy gamma function to an existing tensor. When I try this
from scipy.special import gamma
gamma_t = K.map_fn(lambda x:gamma(1.0 + 1.0 / x) ,b)
Where b is an existing tensor I get
TypeError: ufunc 'gamma' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
How Can I solve This?
Upvotes: 1
Views: 467
Reputation: 59731
You cannot use SciPy functions (or NumPy based functions, in general) directly on a TensorFlow tensor. You could do it with tf.py_func
, but in general the best option is to use TensorFlow operations only. In this case, neither the Keras backend abstraction nor TensorFlow have the gamma function, but TensorFlow has tf.lgamma
, which is its logarithm (well, the logarithm of its absolute value, to be precise). You can then get what you want with:
gamma_t = K.map_fn(lambda x: K.exp(tf.lgamma(1.0 + 1.0 / x)), b)
PS: Note that generally it is recommended to use only backend functions when manipulating Keras tensors, but since this is a rather specific feature and it is not exposed there (also, although Theano does have a gamma function implementation, CNTK currently does not, so it would not be possible to implement it for all backends).
Upvotes: 2