0

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)
Milad
  • 4,901
  • 5
  • 32
  • 43

1 Answers1

3

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.

ash
  • 6,681
  • 3
  • 18
  • 30
  • That makes sense. I guess this also answers the other question I [posted](https://stackoverflow.com/questions/48013711/using-op-inputs-when-defining-custom-gradients-in-tensorflow) earlier about defining custom gradient for a _function_. It seems to be possible using py_func but they run on CPU. I was hoping to achieve it using tf.indentity() but looks like tfe is exactly what I want. – Milad Dec 28 '17 at 20:57