gpt4 book ai didi

python - tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)) 在 tensorflow 中

转载 作者:太空狗 更新时间:2023-10-29 22:24:15 26 4
gpt4 key购买 nike

tensorflow 中 tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)) 的目的是什么?

更多上下文:

    optimizer = tf.train.AdamOptimizer(FLAGS.learning_rate)
with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)):
train_op = optimizer.minimize(loss_fn, var_list=tf.trainable_variables())

最佳答案

tf.control_dependencies 方法可以确保用作上下文管理器输入的操作在上下文管理器内部定义的操作之前运行。

例如:

count = tf.get_variable("count", shape=(), initializer=tf.constant_initializer(1), trainable=False)
count_increment = tf.assign_add(count, 1)
c = tf.constant(2.)
with tf.control_dependencies([count_increment]):
d = c + 3
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print("eval count", count.eval())
print("eval d", d.eval())
print("eval count", count.eval())

这打印:

eval count 1
eval d 5.0 # Running d make count_increment operation being run
eval count 2 # count_increment operation has be run and now count hold 2.

因此,在您的情况下,每次运行 train_op 操作时,它将首先运行 tf.GraphKeys.UPDATE_OPS 集合中定义的所有操作。

关于python - tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)) 在 tensorflow 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53772787/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com