代码之家  ›  专栏  ›  技术社区  ›  Stefan Falk

热门获取数据管道中当前的global_step

  •  0
  • Stefan Falk  · 技术社区  · 5 年前

    我正在尝试创建一个取决于当前电流的过滤器 global_step 虽然我没有做好训练,但我做得不好。

    首先,我无法使用 tf.train.get_or_create_global_step() 在下面的代码中,因为它会抛出

    ValueError: Variable global_step already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:
    

    这就是为什么我试图用 tf.get_default_graph().get_name_scope() 在这种情况下,我能够“ 得到 “全球步骤:

    def filter_examples(example):
        scope = tf.get_default_graph().get_name_scope()
    
        with tf.variable_scope(scope, reuse=tf.AUTO_REUSE):
            current_step = tf.train.get_or_create_global_step()
    
        subtokens_by_step = tf.floor(current_step / curriculum_step_update)
        max_subtokens = min_subtokens + curriculum_step_size * tf.cast(subtokens_by_step, dtype=tf.int32)
    
        return tf.size(example['targets']) <= max_subtokens
    
    
    dataset = dataset.filter(filter_examples)
    

    问题在于,它似乎并不像我预期的那样奏效。据我观察 current_step 在上面的代码中,似乎一直都是0(我不知道,只是根据我的观察,我假设是这样)。

    唯一似乎有所不同的是,重新开始训练,这听起来很奇怪。我认为,根据观察,在这种情况下 当前步数 这将是目前训练的实际步骤。但随着训练的继续,该值本身不会更新。

    如果有办法得到 实际的 当前步长的值,并在我的过滤器中使用它,就像上面一样?


    环境

    张量流1.12.1

    0 回复  |  直到 5 年前
        1
  •  0
  •   rvinas    5 年前

    正如我们在评论中讨论的那样,拥有并更新自己的计数器可能是使用 global_step 变量。这个 counter 变量可以更新如下:

    op = tf.assign_add(counter, 1)
    with tf.control_dependencies(op): 
        # Some operation here before which the counter should be updated
    

    使用 tf.control_dependencies 允许“附加”更新 柜台 到计算图内的路径。然后,您可以使用 柜台 变量,无论你需要它。

        2
  •  -1
  •   DomJack    5 年前

    如果你在数据集中使用变量,你需要在 tf 1.x .

    iterator = tf.compat.v1.make_initializable_iterator(dataset)
    init = iterator.initializer
    tensors = iterator.get_next()
    
    with tf.compat.v1.Session() as sess:
        for epoch in range(num_epochs):
            sess.run(init)
            for example in range(num_examples):
                tensor_vals = sess.run(tensors)