- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
通过 tf.function
和 autograph.to_graph
可以获得类似的结果。
然而,这似乎与版本有关。
例如函数(摘自官方指南):
def square_if_positive(x):
if x > 0:
x = x * x
else:
x = 0.0
return x
可以使用以下方法在图形模式下进行评估:
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))
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))
所以:
tf.function
和 autograph.to_graph
之间有什么关系?可以假设 tf.function
在内部使用 autograph.to_graph
(以及 autograph.to_code
),但这远非显而易见。autograph.to_graph
片段(因为它需要 tf.Session
)?它存在于 autograph doc 中在 TF 1.14 中,但在 TF 2.0 的相应文档中没有最佳答案
我在一篇分为三部分的文章中涵盖并回答了您的所有问题:“分析 tf.function 以发现 AutoGraph 的优势和微妙之处”:part 1 , part 2 , part 3 .
总结并回答您的 3 个问题:
tf.function
和 autograph.to_graph
有什么关系?tf.function
默认使用 AutoGraph。当您第一次调用 tf.function 修饰的函数时会发生什么:
if
的哪个分支到保持等等)tf.*
调用,它知道的 Python 语句 (while
-> tf.while
, if
-> tf.cond
, ...)-.合并第 1 点和第 2 点的信息,构建一个新图,并根据函数名称和参数类型将其缓存在 map 中(请参阅文章以获得更好的理解)。
是的,tf.autograph.to_graph
仍然存在,它会在内部为您创建一个 session (在 TF2 中您不必担心它们)。
无论如何,我建议您阅读链接的三篇文章,因为它们详细介绍了 tf.function
的这一点和其他特点。
关于python - Tensorflow中 `tf.function`和 `autograph.to_graph`是什么关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57526164/
阅读this和 this answer我知道对于 TensorFlow-2.0 上的非动态模型,Keras 会使用 AutoGraph。但是现在编写一个回调来获取训练期间变量的历史记录, class
我正在使用 tensorflow 2.1 和 python 3.7 以下代码片段用于构建 tensorflow 图。当作为独立的 python 脚本执行时,代码运行没有错误。 (可能 tensorfl
在当前笔记本教程 (gpflow 2.0) 中,所有 @tf.function 标签都包含选项 签名=假,例如( https://gpflow.readthedocs.io/en/2.0.0-rc1/
我的问题与this有关和 this一个在这里。我在 Windows 和 Python 3.7.8 和 Tensorflow 2.2.0 上使用 PyCharm: print (sys.version)
通过 tf.function 和 autograph.to_graph 可以获得类似的结果。 然而,这似乎与版本有关。 例如函数(摘自官方指南): def square_if_positive(x):
所以,here它说间接修改不应该起作用,这意味着更改将是不可见的(隐形更改是什么意思?) 但是这段代码正确地计算了梯度: import tensorflow as tf class C: de
def train_one_step(): with tf.GradientTape() as tape: a = tf.random.normal([1, 3, 1])
我正在尝试运行 https://www.tensorflow.org/probability/examples/Probabilistic_Layers_VAE 中找到的代码. 我使用的是 Pytho
在下面的代码中,我绝对必须在 GPU 中执行完整的函数,而不需要一次跳转回 CPU。这是因为我有 4 个 CPU 核心,但我有 1200 个 cuda 核心。理论上,这是可能的,因为 tensorfl
我是一名优秀的程序员,十分优秀!