[tf]tf.identity和=赋值的区别

2018-12-13  本文已影响21人  VanJordan
x = tf.Variable(0.0)
#返回一个op,表示给变量x加1的操作
x_plus_1 = tf.assign_add(x, 1)
  
#control_dependencies的意义是,在执行with包含的内容(在这里就是 y = x)前
#先执行control_dependencies中的内容(在这里就是 x_plus_1)
with tf.control_dependencies([x_plus_1]):
    y = x
init = tf.initialize_all_variables()
  
with tf.Session() as session:
    init.run()
    for i in xrange(5):
        print(y.eval())
>>0
0
0
0
0
x = tf.Variable(1.0)
y = tf.Variable(0.0)
x_plus_1 = tf.assign_add(x, 1)

with tf.control_dependencies([x_plus_1]):
    y = tf.identity(x)#修改部分
init = tf.initialize_all_variables()

with tf.Session() as session:
    init.run()
    for i in xrange(5):
        print(y.eval())
>>2
3
4
5
6
上一篇 下一篇

猜你喜欢

热点阅读