a06e
a06e

Reputation: 20774

Calling scipy special functions on Tensorflow tensors?

Is there any performance cost when calling scipy/numpy special functions with Tensorflow tensors as arguments? As opposed to using the functions provided by tensorflow.math?

I am asking because some special functions are available on scipy but not on Tensorflow (e.g. scipy.special.erfcx). I presume that Tensorflow provides the functions within tensorflow.math instead of recommending to use numpy or scipy directly because this provides some speedup?

Edit: Note that I tried to use @tf.function:

import scipy.special
import tensorflow as tf

@tf.function
def erfcx(x):
    return tf.convert_to_tensor(scipy.special.erfcx(x))

but I get an error when I call this on a tf.Tensor.

A = tf.random.uniform((5,6))
erfcx(A)
# NotImplementedError: Cannot convert a symbolic Tensor (x:0) to a numpy array.

Any suggestions?

Upvotes: 1

Views: 1527

Answers (1)

thushv89
thushv89

Reputation: 11333

So you can do the following. However I don't think you can use this function along with @tf.function which is probably too difficult for TF to build a graph with. This will be running in Eager execution mode.

import tensorflow as tf

x = tf.ones(shape=[10,2], dtype=np.float32)
erfcx = tf.numpy_function(scipy.special.erfcx,[x], tf.float32)

Upvotes: 1

Related Questions