我需要在图中循环不同的时间。我无法通过feed_dict传递整数变量。
rough1 = tf.Graph()
with rough1.as_default():
st = tf.placeholder(tf.float32,shape = ())
d = tf.Variable(0)
for i in range(st):
d = tf.add(d,1)
with tf.Session(graph = rough1) as sess:
sess.run(tf.initialize_all_variables())
s = sess.run([d], feed_dict={st:3})
print s
发布于 2017-02-02 16:28:10
当您构建Tensorflow图时,直到您调用tf.Session.run
才会实际计算该图。这样做的结果是,range
之类的Python构造不能检查Tensor
的值,除非调用run()
。在这里,range(st)
要求st
的值是Python已知的整数,但st
的值直到图形实际执行时才知道。
换句话说:构建图的代码不能依赖于对该图的评估。
但在这里,您可以只使用Python整数而不是张量,一切都很正常:
rough1 = tf.Graph()
with rough1.as_default():
d = tf.Variable(0)
for i in range(3):
d = tf.add(d,1)
with tf.Session(graph = rough1) as sess:
sess.run(tf.initialize_all_variables())
s = sess.run([d])
print s
希望这能有所帮助!
发布于 2017-06-28 08:11:50
试试这个,看看它是不是你要找的。
st=1
rough1 = tf.Graph()
with rough1.as_default():
d = tf.Variable(0)
for i in range(st):
d = tf.add(d,1)
with tf.Session(graph = rough1) as sess:
sess.run(tf.initialize_all_variables())
s = sess.run([d])
print s
https://stackoverflow.com/questions/41930725
复制