【问题标题】:Using tf.train.exponential_decay with predefined estimator?将 tf.train.exponential_decay 与预定义的估计器一起使用?
【发布时间】:2018-03-11 19:27:46
【问题描述】:

我正在尝试将 tf.train.exponential_decay 与预定义的估算器一起使用,但由于某种原因,这被证明是非常困难的。我在这里遗漏了什么吗?

这是我的旧代码,学习率不变:

classifier = tf.estimator.DNNRegressor(
    feature_columns=f_columns,
    model_dir='./TF',
    hidden_units=[2, 2],
    optimizer=tf.train.ProximalAdagradOptimizer(
      learning_rate=0.50,
      l1_regularization_strength=0.001,
    ))

现在我尝试添加这个:

starter_learning_rate = 0.50
global_step = tf.Variable(0, trainable=False)
learning_rate = tf.train.exponential_decay(starter_learning_rate, global_step,
                                           10000, 0.96, staircase=True)

但现在呢?

  • estimator.predict() 不接受 global_step 所以会卡在 0?
  • 即使我将 learning_rate 传递给 tf.train.ProximalAdagradOptimizer() 我也会收到一条错误消息

"ValueError: Tensor("ExponentialDecay:0", shape=(), dtype=float32) 必须来自同一图表 张量("dnn/hiddenlayer_0/kernel/part_0:0", shape=(62, 2), dtype=float32_ref)。”

非常感谢您的帮助。我正在使用 TF1.6 顺便说一句。

【问题讨论】:

    标签: tensorflow tensorflow-estimator


    【解决方案1】:

    你应该让优化器在 mode == tf.estimator.ModeKeys.TRAIN 下

    这里是示例代码

    def _model_fn(features, labels, mode, config):
    
        # xxxxxxxxx
        # xxxxxxxxx
    
        assert mode == tf.estimator.ModeKeys.TRAIN
    
        global_step = tf.train.get_global_step()
        decay_learning_rate = tf.train.exponential_decay(learning_rate, global_step, 100, 0.98, staircase=True)
        optimizer = adagrad.AdagradOptimizer(decay_learning_rate)
    
        update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
        with tf.control_dependencies(update_ops):
             train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step())
        return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op, training_chief_hooks=chief_hooks, eval_metric_ops=metrics)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-11
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      相关资源
      最近更新 更多