1. 程式人生 > >tf.identity()與tf.group()

tf.identity()與tf.group()

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

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
總結:tf.identity()和tf.group()均可將語句變為操作