tf.identity()與tf.group()
阿新 • • 發佈:2019-02-07
1. tf.identity():
import tensorflow as tf x = tf.Variable(0.0) x_plus = tf.assign_add(x, 1) with tf.control_dependencies([x_plus]):#只有當內部為操作時以來才會生效 y = tf.identity(x)#將該語句變為操作 init = tf.global_variables_initializer() with tf.Session() as session: init.run() for i in range(5): print(y.eval()) print(x.eval())#5
2.tf.group():
總結:tf.identity()和tf.group()均可將語句變為操作import tensorflow as tf x = tf.Variable(0.0) x_plus = tf.assign_add(x, 1) with tf.control_dependencies([x_plus]):#只有當內部為操作時以來才會生效 #y = tf.identity(x)#將該語句變為操作 y = x update = tf.group(y)#將該語句變為操作 init = tf.global_variables_initializer() with tf.Session() as session: init.run() for i in range(5): session.run(update) print(y.eval()) print(x.eval())#5