Milad
Milad

Reputation: 5510

Defining tensors inside Tensorflow's py_func

Am I right in thinking that tf.py_func functions in TensorFlow cannot use any TF operations and should basically be pure python/numpy? For instance, I don't seem to be able to do something like:

def my_py_func(values):
    return tf.greater(values, 1)

Upvotes: 0

Views: 331

Answers (1)

ash
ash

Reputation: 6751

That is correct, tf.py_func is provided with numpy arrays and is expected to return a numpy array as well.

TensorFlow operations like tf.greater etc. normally do not execute immediately and instead return handles to symbolic tensors in the graph. Hence, using them in a py_func doesn't quite make sense, since they will merely add operations to some graph.

However, TensorFlow's eager execution feature (blog post) makes TensorFlow operations execute immediately.

In future releases of TensorFlow (version 1.5 onwards), you should be able to use tfe.py_func instead - which will allow you to use TensorFlow operations in the Python function (as eager execution is enabled in the context of that function). This feature is under active development, so if it is important to you I'd make sure to chime in on the Github issues list. In particular, it will be possible to have the Python function provide to tfe.py_func execute operations on the GPU as well and also be differentiable.

Hope that helps.

Upvotes: 3

Related Questions