1. 程式人生 > >180412 tf.identity,tf.assign_add(),with tf.control_dependencies()與tf.negative()

180412 tf.identity,tf.assign_add(),with tf.control_dependencies()與tf.negative()

tf.identity是返回了一個一模一樣新的tensor,再control_dependencies的作用塊下,需要增加一個新節點到gragh中。

x = tf.Variable(0.0)
x_plus_1 = tf.assign_add(x, 1) # tf.assign_add(a,b) 加法操作

# with tf.control_dependencies(A):B
# 先執行A,再執行B
with tf.control_dependencies([x_plus_1]):
    y = tf.identity(x) #新增節點並返回一個和x一樣的tensor
init = tf.global_variables_initializer()

with
tf.Session() as session: init.run() for i in np.arange(5): print(y.eval())
1.0
2.0
3.0
4.0
5.0

tf.negtive()

a = tf.Variable(2.0)
b = tf.negative(a)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(a))
    print(sess.run(b))
2.0
-2.0