session.run([fetch1, fetch2])
关于 session.run([fetch1, fetch2]),请看http://stackoverflow.com/questions/42407611/how-tensorflow-handle-the-computional-graph-when-executing-sess-run/42408368?noredirect=1#comment71994086_42408368
执行sess.run()时,tensorflow是否计算了整个图
我们在编写代码的时候,总是要先定义好整个图,然后才调用sess.run()。那么调用sess.run()的时候,程序是否执行了整个图
import tensorflow as tf
state = tf.Variable(0.0,dtype=tf.float32)
one = tf.constant(1.0,dtype=tf.float32)
new_val = tf.add(state, one)
update = tf.assign(state, new_val) #返回tensor, 值为new_val
update2 = tf.assign(state, 10000) #没有fetch,便没有执行
init = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
for _ in range(3):
print sess.run(update)1234567891011
和上个程序差不多,但我们这次仅仅是fetch “update”,输出是1.0 , 2.0, 3.0,可以看出,tensorflow并没有计算整个图,只是计算了与想要fetch 的值相关的部分
本文来自 ke1th 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/u012436149/article/details/52908692?utm_source=copy