【问题标题】:Keras and AutoGraphKeras 和 AutoGraph
【发布时间】:2019-11-02 06:06:28
【问题描述】:

阅读 this 和 this answer 我了解到,对于 TensorFlow-2.0 上的非动态模型,Keras 将使用 AutoGraph。但是现在写一个回调来获取训练过程中变量的历史,

class TrainHistory(tf.keras.callbacks.Callback):
    def on_train_begin(self, logs={}):
        self.vars = []

    def on_batch_end(self, batch, logs={}):
        self.vars.append([v.numpy() for v in self.model.variables])

我看到可以使用热切张量的.numpy()方法。我期待像numpy() is only available when eager execution is enabled 这样的错误。将 Keras 与 TensorFlow-2.0 结合使用时是否会执行急切代码?

Here 有人提到,对于像卷积这样的计算密集型函数,tf.function-decorated 函数与 Eager 代码相比并没有太大的加速,但从示例中显示的数字来看,我猜这种差异可能对长时间的训练。在使用 tf.function 装饰的自定义训练函数上使用 GradientTape 而不是 Keras 的 fit() 方法在性能方面会更好吗?

【问题讨论】:

    标签: python tensorflow keras tensorflow2.0


    【解决方案1】:

    如果您的意思是在外部循环(即“epoch”循环)上使用@tf.function,它可能不会对您的模型有太大好处。它只会使开发更加困难。代码越多越复杂。

    但是,您绝对必须在自定义损失函数和其他每批调用一次的函数上使用 tf.function。

    而且,不,使用 GradientTape-d 自定义 tf.function 可能不会胜过 keras 的 fit 方法。多年来,它经过了彻底的测试和改进。

    这能回答你的问题吗?

    【讨论】:

      猜你喜欢
      • 2019-10-11
      • 1970-01-01
      • 1970-01-01
      • 2020-08-16
      • 2019-06-21
      • 2021-06-14
      • 1970-01-01
      • 2017-10-08
      • 2023-03-10
      相关资源
      最近更新 更多