當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python tf.config.run_functions_eagerly用法及代碼示例

啟用/禁用 tf.function 的即刻執行。

用法

tf.config.run_functions_eagerly(
    run_eagerly
)

參數

  • run_eagerly 布爾值。是否即刻地運行函數。

調用tf.config.run_functions_eagerly(True) 將使tf.function 的所有調用立即運行,而不是作為跟蹤圖函數運行。

這對於調試很有用。

def my_func(a):
 print("Python side effect")
 return a + a
a_fn = tf.function(my_func)
# A side effect the first time the function is traced
a_fn(tf.constant(1))
Python side effect
<tf.Tensor:shape=(), dtype=int32, numpy=2>
# No further side effect, as the traced function is called
a_fn(tf.constant(2))
<tf.Tensor:shape=(), dtype=int32, numpy=4>
# Now, switch to eager running
tf.config.run_functions_eagerly(True)
# Side effect, as the function is called directly
a_fn(tf.constant(2))
Python side effect
<tf.Tensor:shape=(), dtype=int32, numpy=4>
# Turn this back off
tf.config.run_functions_eagerly(False)

注意:此標誌對作為參數傳遞給 tf.data 轉換的函數沒有影響。 tf.data 函數永遠不會即刻地執行,並且始終作為已編譯的 Tensorflow Graph 執行。

相關用法


注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.config.run_functions_eagerly。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。