日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Tensorflow学习笔记2:About Session, Graph, Operation and Tensor

發(fā)布時(shí)間:2025/3/20 编程问答 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Tensorflow学习笔记2:About Session, Graph, Operation and Tensor 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

簡(jiǎn)介

上一篇筆記:Tensorflow學(xué)習(xí)筆記1:Get Started?我們談到Tensorflow是基于圖(Graph)的計(jì)算系統(tǒng)。而圖的節(jié)點(diǎn)則是由操作(Operation)來構(gòu)成的,而圖的各個(gè)節(jié)點(diǎn)之間則是由張量(Tensor)作為邊來連接在一起的。所以Tensorflow的計(jì)算過程就是一個(gè)Tensor流圖。Tensorflow的圖則是必須在一個(gè)Session中來計(jì)算。這篇筆記來大致介紹一下Session、Graph、Operation和Tensor。

Session

Session提供了Operation執(zhí)行和Tensor求值的環(huán)境。如下面所示,

import tensorflow as tf# Build a graph. a = tf.constant([1.0, 2.0]) b = tf.constant([3.0, 4.0]) c = a * b# Launch the graph in a session. sess = tf.Session()# Evaluate the tensor 'c'. print sess.run(c) sess.close()# result: [3., 8.]

一個(gè)Session可能會(huì)擁有一些資源,例如Variable或者Queue。當(dāng)我們不再需要該session的時(shí)候,需要將這些資源進(jìn)行釋放。有兩種方式,

  • 調(diào)用session.close()方法;
  • 使用with tf.Session()創(chuàng)建上下文(Context)來執(zhí)行,當(dāng)上下文退出時(shí)自動(dòng)釋放。
  • 上面的例子可以寫成,

    import tensorflow as tf# Build a graph. a = tf.constant([1.0, 2.0]) b = tf.constant([3.0, 4.0]) c = a * bwith tf.Session() as sess:print sess.run(c)

    Session類的構(gòu)造函數(shù)如下所示:

    tf.Session.__init__(target='', graph=None, config=None)

    如果在創(chuàng)建Session時(shí)沒有指定Graph,則該Session會(huì)加載默認(rèn)Graph。如果在一個(gè)進(jìn)程中創(chuàng)建了多個(gè)Graph,則需要?jiǎng)?chuàng)建不同的Session來加載每個(gè)Graph,而每個(gè)Graph則可以加載在多個(gè)Session中進(jìn)行計(jì)算。

    執(zhí)行Operation或者求值Tensor有兩種方式:

  • 調(diào)用Session.run()方法:?該方法的定義如下所示,參數(shù)fetches便是一個(gè)或者多個(gè)Operation或者Tensor。

    tf.Session.run(fetches, feed_dict=None)

  • 調(diào)用Operation.run()或則Tensor.eval()方法:?這兩個(gè)方法都接收參數(shù)session,用于指定在哪個(gè)session中計(jì)算。但該參數(shù)是可選的,默認(rèn)為None,此時(shí)表示在進(jìn)程默認(rèn)session中計(jì)算。

  • 那如何設(shè)置一個(gè)Session為默認(rèn)的Session呢?有兩種方式:

    1. 在with語(yǔ)句中定義的Session,在該上下文中便成為默認(rèn)session;上面的例子可以修改成:

    import tensorflow as tf# Build a graph. a = tf.constant([1.0, 2.0]) b = tf.constant([3.0, 4.0]) c = a * bwith tf.Session():print c.eval()

    2. 在with語(yǔ)句中調(diào)用Session.as_default()方法。 上面的例子可以修改成:

    import tensorflow as tf# Build a graph. a = tf.constant([1.0, 2.0]) b = tf.constant([3.0, 4.0]) c = a * b sess = tf.Session() with sess.as_default():print c.eval() sess.close()

    Graph

    Tensorflow中使用tf.Graph類表示可計(jì)算的圖。圖是由操作Operation和張量Tensor來構(gòu)成,其中Operation表示圖的節(jié)點(diǎn)(即計(jì)算單元),而Tensor則表示圖的邊(即Operation之間流動(dòng)的數(shù)據(jù)單元)。

    tf.Graph.__init__()

    創(chuàng)建一個(gè)新的空Graph

    在Tensorflow中,始終存在一個(gè)默認(rèn)的Graph。如果要將Operation添加到默認(rèn)Graph中,只需要調(diào)用定義Operation的函數(shù)(例如tf.add())。如果我們需要定義多個(gè)Graph,則需要在with語(yǔ)句中調(diào)用Graph.as_default()方法將某個(gè)graph設(shè)置成默認(rèn)Graph,于是with語(yǔ)句塊中調(diào)用的Operation或Tensor將會(huì)添加到該Graph中。

    例如,

    import tensorflow as tf g1 = tf.Graph() with g1.as_default():c1 = tf.constant([1.0]) with tf.Graph().as_default() as g2:c2 = tf.constant([2.0])with tf.Session(graph=g1) as sess1:print sess1.run(c1) with tf.Session(graph=g2) as sess2:print sess2.run(c2)# result: # [ 1.0 ] # [ 2.0 ]

    如果將上面例子的sess1.run(c1)和sess2.run(c2)中的c1和c2交換一下位置,運(yùn)行會(huì)報(bào)錯(cuò)。因?yàn)閟ess1加載的g1中沒有c2這個(gè)Tensor,同樣地,sess2加載的g2中也沒有c1這個(gè)Tensor。

    Operation

    一個(gè)Operation就是Tensorflow Graph中的一個(gè)計(jì)算節(jié)點(diǎn)。其接收零個(gè)或者多個(gè)Tensor對(duì)象作為輸入,然后產(chǎn)生零個(gè)或者多個(gè)Tensor對(duì)象作為輸出。Operation對(duì)象的創(chuàng)建是通過直接調(diào)用Python operation方法(例如tf.matmul())或者Graph.create_op()。

    例如c = tf.matmul(a, b)表示創(chuàng)建了一個(gè)類型為MatMul的Operation,該Operation接收Tensor a和Tensor b作為輸入,而產(chǎn)生Tensor c作為輸出。

    當(dāng)一個(gè)Graph加載到一個(gè)Session中,則可以調(diào)用Session.run(op)來執(zhí)行op,或者調(diào)用op.run()來執(zhí)行(op.run()是tf.get_default_session().run()的縮寫)。

    Tensor

    Tensor表示的是Operation的輸出結(jié)果。不過,Tensor只是一個(gè)符號(hào)句柄,其并沒有保存Operation輸出結(jié)果的值。通過調(diào)用Session.run(tensor)或者tensor.eval()方可獲取該Tensor的值。

    關(guān)于Tensorflow的圖計(jì)算過程

    我們通過下面的代碼來看一下Tensorflow的圖計(jì)算過程:

    import tensorflow as tf a = tf.constant(1) b = tf.constant(2) c = tf.constant(3) d = tf.constant(4) add1 = tf.add(a, b) mul1 = tf.mul(b, c) add2 = tf.add(c, d) output = tf.add(add1, mul1) with tf.Session() as sess:print sess.run(output)
    #
    result: 9

    上面的代碼構(gòu)成的Graph如下圖所示,

    ?

    當(dāng)Session加載Graph的時(shí)候,Graph里面的計(jì)算節(jié)點(diǎn)都不會(huì)被觸發(fā)執(zhí)行。當(dāng)運(yùn)行sess.run(output)的時(shí)候,會(huì)沿著指定的Tensor output來進(jìn)圖路徑往回觸發(fā)相對(duì)應(yīng)的節(jié)點(diǎn)進(jìn)行計(jì)算(圖中紅色線表示的那部分)。當(dāng)我們需要output的值時(shí),觸發(fā)Operation tf.add(add1, mul1)被執(zhí)行,而該節(jié)點(diǎn)則需要Tensor add1和Tensor mul1的值,則往回觸發(fā)Operation tf.add(a, b)和Operation tf.mul(b, c)。以此類推。

    所以在計(jì)算Graph時(shí),并不一定是Graph中的所有節(jié)點(diǎn)都被計(jì)算了,而是指定的計(jì)算節(jié)點(diǎn)或者該節(jié)點(diǎn)的輸出結(jié)果被需要時(shí)。

    (done)

    轉(zhuǎn)載于:https://www.cnblogs.com/lienhua34/p/5998853.html

    總結(jié)

    以上是生活随笔為你收集整理的Tensorflow学习笔记2:About Session, Graph, Operation and Tensor的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。