且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在`predict_step`的Keras模型中禁用了急切执行

更新时间:2023-01-23 15:04:42

如果要在急切模式下运行 predict_step 函数,可以执行以下操作.请注意,它将以急切模式设置所有内容.

If you want to run the predict_step function in eager mode, you can do it as follows. Please note, it will set everything in eager mode.

import tensorflow as tf
tf.config.run_functions_eagerly(True)

通常 tf.function 处于 Graph 模式.使用上面的语句,它们也可以设置为 Eager 模式,

Typically tf.function are in Graph mode. Using the above statement, they can be set to Eager mode too, src.

根据您的评论AFAIK,如果在编译模型时设置 run_eagerly ,应该没有任何区别.这是来自官方声明 src-model.compile .

As per your comment, AFAIK, there should not be any difference if you set run_eagerly while compiling the model. Here is from the official statement, src - model.compile.

run_eagerly :布尔.默认为False.如果为True,则此模型的逻辑不会包装在 tf中.功能.建议将其保留为无",除非您的模型无法在 tf中运行.功能.

run_eagerly: Bool. Defaults to False. If True, this Model's logic will not be wrapped in a tf. function. Recommended to leave this as None unless your Model cannot be run inside a tf. function.


关于您的第一个查询,为什么 TensorFlow 禁用 tf.keras.Model predict_step 函数内部的急切执行?


About your first query, why does TensorFlow disable eager execution inside the predict_step function of a tf.keras.Model?

主要原因之一是要提供模型的***性能.它不仅与 predict_step 有关,还与 train_step test_step 有关.基本上是 tf.keras 模型被编译成静态图.为了使它们以急切的模式运行,需要完成上述方法.但请注意,在这种情况下使用急切模式可能会减慢您的训练速度.为了集体的好, tf.keras 模型是在图形模式下编译的.

One of the main reasons is to deliver the best performance of your model. And it's not only with predict_step but also train_step and test_step. Basically tf. keras models are compiled to a static graph. In order to make run them in eager mode, the above approaches need to be done. But note that, using eager mode in such cases may slow down your training. For the collective of good, tf. keras models are compiled in graph mode.