1. 程式人生 > >【tensorflow】tf.identity()

【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())