【问题标题】:Is there a replacement for global_step in TensorFlow 2.0?TensorFlow 2.0 中是否有 global_step 的替代品?
【发布时间】:2019-03-26 10:48:48
【问题描述】:

TensorFlow 2.0 中似乎缺少 global_step。

我有几个对当前训练进度感兴趣的回调,我不确定是否需要实现自己的计步器或依赖 epochs 计数...

有什么更换建议吗?

【问题讨论】:

    标签: tensorflow2.0


    【解决方案1】:

    现在最好声明我们自己的global_step = tf.Variable(1, name="global_step") 并手动使用它。

    查看文档,没有 tf.train.get_or_create_global_step 的直接替代品,文档中关于 step 的唯一部分是 tf.summary 模块的实验部分:https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/summary/experimental

    【讨论】:

      【解决方案2】:

      使用 TensorFlow 2.3.1 及其 Keras API。

      tf.keras.optimizers.Optimizer 的实例继承 iterations 属性。 The implementation 表明这是一个在每个训练步骤后递增的计数器。在编译模型之前从优化器访问它。

      import tensorflow as tf
      
      tf.compat.v1.disable_eager_execution()  # see note
      
      optimizer = tf.keras.optimizers.Adam()
      training_step = optimizer.iterations
      
      model = Model(inputs,outputs)
      model.compile(
          loss=my_adaptive_loss_function(training_step),
          optimizer=optimizer)
      

      注意:在我的设置中,我必须禁用急切执行才能使用此变量,否则我会收到以下TypeError。如果您的实现不如我的那么笨拙,您也许可以避免这种情况。

      TypeError:正在传递函数构建代码之外的操作 一个“图形”张量。可以有图张量 通过包含 tf.init_scope 在您的函数构建代码中。 例如,以下函数将失败: @tf.function def has_init_scope(): my_constant = tf.constant(1.) 使用 tf.init_scope(): 添加 = my_constant * 2 图张量的名称为:pulse_features:0 在处理上述异常的过程中,又出现了一个异常:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-13
        • 2017-03-25
        • 2017-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-23
        相关资源
        最近更新 更多