TensorFlow打印輸出tensor的值
在學(xué)習(xí)TensorFlow的過(guò)程中,我們需要知道某個(gè)tensor的值是什么,這個(gè)很重要,尤其是在debug的時(shí)候。也許你會(huì)說(shuō),這個(gè)很容易啊,直接print就可以了。其實(shí)不然,print只能打印輸出shape的信息,而要打印輸出tensor的值,需要借助class tf.Session, class tf.InteractiveSession。因?yàn)槲覀冊(cè)诮raph的時(shí)候,只建立tensor的結(jié)構(gòu)形狀信息,并沒(méi)有執(zhí)行數(shù)據(jù)的操作。
一 class tf.Session
運(yùn)行tensorflow操作的類,其對(duì)象封裝了執(zhí)行操作對(duì)象和評(píng)估tensor數(shù)值的環(huán)境。這個(gè)我們之前介紹過(guò),在定義好所有的數(shù)據(jù)結(jié)構(gòu)和操作后,其最后運(yùn)行。
import tensorflow as tf # Build a graph. a = tf.constant(5.0) b = tf.constant(6.0) c = a * b # Launch the graph in a session. sess = tf.Session() # Evaluate the tensor `c`. print(sess.run(c))
二 class tf.InteractiveSession
顧名思義,用于交互上下文的session,便于輸出tensor的數(shù)值。與上一個(gè)Session相比,其有默認(rèn)的session執(zhí)行相關(guān)操作,比如:Tensor.eval(), Operation.run()。Tensor.eval()是執(zhí)行這個(gè)tensor之前的所有操作,Operation.run()也同理。
import tensorflow as tf a = tf.constant(5.0) b = tf.constant(6.0) c = a * b with tf.Session(): # We can also use 'c.eval()' here. print(c.eval())
打印輸出張量的值的方法
import tensorflow as tf zeros = tf.zeros([3,3]) # 方法1 with tf.Session(): print(zeros.eval()) # 方法2 sess = tf.Session() print(sess.run(zeros))
打印輸出tensor變量的值的方法
import tensorflow as tf ones=tf.Variable(tf.ones([3,3])) # 方法1 InteractiveSession + initializer inter_sess=tf.InteractiveSession() ones.initializer.run() print(inter_sess.run(ones)) # 方法2 inter_sess=tf.InteractiveSession() tf.global_variables_initializer().run() print(inter_sess.run(ones)) # 方法3 Session + global_variables_initializer sess=tf.Session() sess.run(tf.global_variables_initializer()) print(sess.run(ones)) # 方法4 with Session + global_variables_initializer with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print(sess.run(ones))
Reference:
[1] https://www.tensorflow.org/versions/r0.9/api_docs/python/client.html#InteractiveSession
[2] http://stackoverflow.com/questions/33633370/how-to-print-the-value-of-a-tensor-object-in-tensorflow
到此這篇關(guān)于TensorFlow打印輸出tensor的值的文章就介紹到這了,更多相關(guān)TensorFlow打印輸出tensor內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)HTML轉(zhuǎn)Word的示例代碼
這篇文章主要為大家詳細(xì)介紹了使用Python實(shí)現(xiàn)HTML轉(zhuǎn)Word的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
python實(shí)現(xiàn)圖片轉(zhuǎn)字符畫(huà)的完整代碼
這篇文章主要給大家介紹了關(guān)于python實(shí)現(xiàn)圖片轉(zhuǎn)字符畫(huà)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
Python實(shí)現(xiàn)定時(shí)備份mysql數(shù)據(jù)庫(kù)并把備份數(shù)據(jù)庫(kù)郵件發(fā)送
這篇文章主要介紹了Python實(shí)現(xiàn)定時(shí)備份mysql數(shù)據(jù)庫(kù)并把備份數(shù)據(jù)庫(kù)郵件發(fā)送的相關(guān)資料,需要的朋友可以參考下2018-03-03
解決Jupyter-notebook不彈出默認(rèn)瀏覽器的問(wèn)題
這篇文章主要介紹了解決Jupyter-notebook不彈出默認(rèn)瀏覽器的問(wèn)題,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Python批量查找包含多個(gè)關(guān)鍵詞的PDF文件
在信息爆炸的時(shí)代,數(shù)據(jù)管理變得愈發(fā)重要,本文主要為大家介紹了如何通過(guò)Python批量查找包含多個(gè)關(guān)鍵詞的PDF文件,希望對(duì)大家有所幫助2024-11-11
10個(gè)Python面試常問(wèn)的問(wèn)題(小結(jié))
這篇文章主要介紹了10個(gè)Python面試常問(wèn)的問(wèn)題(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Python統(tǒng)計(jì)列表元素出現(xiàn)次數(shù)的方法示例
這篇文章主要介紹了Python統(tǒng)計(jì)列表元素出現(xiàn)次數(shù)的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04

