【发布时间】:2022-01-23 04:18:18
【问题描述】:
我正在关注函数here 上的 Tensorflow 指南,根据我的理解,TF 将为每次调用具有不同输入签名(即数据类型和输入形状)的函数创建一个图表。但是,以下示例使我感到困惑。由于两个输入都是整数并且具有完全相同的形状,因此 TF 不应该只执行一次跟踪和构造图吗?为什么调用函数时会发生两次跟踪?
@tf.function
def a_function_with_python_side_effect(x):
print("Tracing!") # An eager-only side effect.
return x * x + tf.constant(2)
# This retraces each time the Python argument changes,
# as a Python argument could be an epoch count or other
# hyperparameter.
print(a_function_with_python_side_effect(2))
print(a_function_with_python_side_effect(3))
输出:
Tracing!
tf.Tensor(6, shape=(), dtype=int32)
Tracing!
tf.Tensor(11, shape=(), dtype=int32)
【问题讨论】:
标签: python tensorflow