首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用tf.train.exponential_decay和预定义的估计量?

使用tf.train.exponential_decay和预定义的估计量?
EN

Stack Overflow用户
提问于 2018-03-11 19:27:46
回答 1查看 445关注 0票数 6

我试图在预定义的估计器中使用tf.train.exponential_decay,但由于某些原因,这是非常困难的。我是不是漏掉了什么?

下面是带有常量learning_rate的旧代码:

代码语言:javascript
复制
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,
    ))

现在我试着添加以下内容:

代码语言:javascript
复制
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/ same /part 0:0”,shape=(62,2),dtype=float32_ref)。

非常感谢你的帮助。我用的是TF1.6。

EN

回答 1

Stack Overflow用户

发布于 2020-03-06 03:06:28

您应该让优化器在模式== tf.estimator.ModeKeys.TRAIN下进行。

这是示例代码

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49224141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档