1. 程式人生 > >tensorflow學習筆記:sess.run()

tensorflow學習筆記:sess.run()

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