日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

tensorflow中创建多个计算图(Graph)

發布時間:2024/7/23 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tensorflow中创建多个计算图(Graph) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉自https://blog.csdn.net/dcrmg/article/details/79028032

tf中可以定義多個計算圖,不同計算圖上的張量和運算是相互獨立的,不會共享。計算圖可以用來隔離張量和計算,同時提供了管理張量和計算的機制。計算圖可以通過Graph.device函數來指定運行計算的設備,為TensorFlow充分利用GPU/CPU提供了機制。
?

  • 使用 g = tf.Graph()函數創建新的計算圖;
  • 在with g.as_default():語句下定義屬于計算圖g的張量和操作
  • 在with tf.Session()中通過參數 graph = xxx指定當前會話所運行的計算圖;
  • 如果沒有顯式指定張量和操作所屬的計算圖,則這些張量和操作屬于默認計算圖;
  • 一個圖可以在多個sess中運行,一個sess也能運行多個圖
  • 創建多個計算圖:

    # -*- coding: utf-8 -*-) import tensorflow as tf# 在系統默認計算圖上創建張量和操作 a=tf.constant([1.0,2.0]) b=tf.constant([2.0,1.0]) result = a+b# 定義兩個計算圖 g1=tf.Graph() g2=tf.Graph()# 在計算圖g1中定義張量和操作 with g1.as_default():a = tf.constant([1.0, 1.0])b = tf.constant([1.0, 1.0])result1 = a + bwith g2.as_default():a = tf.constant([2.0, 2.0])b = tf.constant([2.0, 2.0])result2 = a + b# 在g1計算圖上創建會話 with tf.Session(graph=g1) as sess:out = sess.run(result1)print 'with graph g1, result: {0}'.format(out)with tf.Session(graph=g2) as sess:out = sess.run(result2)print 'with graph g2, result: {0}'.format(out)# 在默認計算圖上創建會話 with tf.Session(graph=tf.get_default_graph()) as sess:out = sess.run(result)print 'with graph default, result: {0}'.format(out)print g1.version # 返回計算圖中操作的個數

    輸出:

    with graph g1, result: [ 2. 2.] with graph g2, result: [ 4. 4.] with graph default, result: [ 3. 3.] 3

    ?

    總結

    以上是生活随笔為你收集整理的tensorflow中创建多个计算图(Graph)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。