Phy*_*ade 2 python tensorflow tensorflow2.0
通过tf.function和可以获得类似的结果autograph.to_graph。
但是,这似乎取决于版本。
例如,函数(取自官方指南):
def square_if_positive(x):
if x > 0:
x = x * x
else:
x = 0.0
return x
Run Code Online (Sandbox Code Playgroud)
可以使用图形模式进行评估:
autograph.to_graph 在 TF 1.14tf_square_if_positive = autograph.to_graph(square_if_positive)
with tf.Graph().as_default():
g_out = tf_square_if_positive(tf.constant( 9.0))
with tf.Session() as sess:
print(sess.run(g_out))
Run Code Online (Sandbox Code Playgroud)
tf.function 在 TF2.0@tf.function
def square_if_positive(x):
if x > 0:
x = x * x
else:
x = 0.0
return x
square_if_positive(tf.constant( 9.0))
Run Code Online (Sandbox Code Playgroud)
所以:
tf.function和之间是什么关系autograph.to_graph?可以假设tf.function在内部使用autograph.to_graph(以及autograph.to_code),但这远非显而易见。autograph.to_graphTF2.0 中是否仍支持该代码段(因为它需要tf.Session)?它存在于TF 1.14的签名文档中,但不存在于 TF 2.0 的相应文档中我在一篇由三部分组成的文章中涵盖并回答了您的所有问题:“分析 tf.function 以发现 AutoGraph 的优势和微妙之处”:第 1部分、第 2部分、第 3 部分。
总结并回答您的 3 个问题:
tf.function和之间是什么关系autograph.to_graph?tf.function默认使用 AutoGraph。第一次调用tf.function 装饰的函数时会发生什么:
if要保留的哪个分支等等)tf.*调用,即它知道的 Python 语句 ( while-> tf.while, if-> tf.cond, ...)-。合并来自点 1 和 2 的信息,构建一个新图,并根据函数名称和参数类型将其缓存在地图中(请参阅文章以获得更好的理解)。
是的,tf.autograph.to_graph它仍然存在并且它会在内部为您创建一个会话(在 TF2 中您不必担心它们)。
无论如何,我建议您阅读链接的三篇文章,因为它们详细介绍了tf.function.
| 归档时间: |
|
| 查看次数: |
526 次 |
| 最近记录: |