- import tensorflow as tf
-
- #图的构建
- with tf.Graph().as_default():
- #定义一个变量
- x = tf.Variable(initial_value=1,validate_shape=False ,dtype=tf.float32,name='x')
- #定义一个占位符
- input_y = tf.placeholder(dtype=tf.float32,shape=None,name='input_y')
- tmp = tf.multiply(x,input_y)
-
- # y = x * 3
- #赋值操作
- assign_op = tf.assign(x,tmp)
- #控制依赖
- with tf.control_dependencies(control_inputs=[assign_op]):
- y = x * 3
- with tf.Session()as sess:
- sess.run(tf.global_variables_initializer())
- for data in range(1,5):
- tmp_,assign_op_,y_ = sess.run([tmp,assign_op,y],feed_dict={input_y:data})
- print(data,y_)
问题:
为什么x对assign_op的依赖必须得到控制,而tmp 对x的依赖就不需要控制?