What is the difference in purpose between tf.py_function and tf.function?

后端 未结 1 1611
灰色年华
灰色年华 2021-02-10 12:59

The difference between the two is muddled in my head, notwithstanding the nuances of what is eager and what isn\'t. From what I gather, the @tf.function decorator h

1条回答
  •  [愿得一人]
    2021-02-10 13:28

    They do indeed start to resemble each other as they are improved, so it is useful to see where they come from. Initially, the difference was that:

    • @tf.function turns python code into a series of TensorFlow graph nodes.
    • tf.py_function wraps an existing python function into a single graph node.

    This means that tf.function requires your code to be relatively simple while tf.py_function can handle any python code, no matter how complex.

    While this line is indeed blurring, with tf.py_function doing more interpretation and tf.function accepting lot's of complex python commands, the general rule stays the same:

    • If you have relatively simple logic in your python code, use tf.function.
    • When you use complex code, like large external libraries (e.g. connecting to a database, or loading a large external NLP package) use tf.py_function.

    0 讨论(0)
提交回复
热议问题