【tensorflow】tf.identity()

常与tf.control_dependencies(self, control_inputs)配合使用,只有当这个上下文管理器中的是一个操作时,control_inputs才会执行。


x = tf.Variable(0.0)
x_plus_1 = tf.assign_add(x, 1)

with tf.control_dependencies([x_plus_1]):
    y = x
    ##此时程序输出会是1 1 1 1 1
    ##并不是一个op,需要改为y = tf.identity(x)
    ##程序输出为1 2 3 4 5
init = tf.global_variables_initializer()

with tf.Session() as session:
    init.run()
    for i in range(5):
        print(y.eval())

猜你喜欢

转载自blog.csdn.net/PartyPartyAnimal/article/details/81348145
今日推荐