130、TensorFlow操作多個計算圖


# Programming with multiple graphs
# 當訓練一個模型的時候一個常用的方式就是使用一個圖來訓練你的模型
# 另一個圖來評價和計算訓練的效果
# 在許多情況下前向計算和訓練是不同的
# 例如像Dropout和batch正則化使用不同的操作在不同的Case條件下
# 更進一步地說 通過使用默認的工具類,如tf.train.Saver使用tf.Variable的命名空間
# 在保存檢查點的時候tf.Variable的名字是根據tf.Operation來定義
# 當你使用這種方法來編程的時候你或者使用獨立的Python進程來建立和執行這個計算圖
# 或者你可以使用多個計算圖在相同的進程中
# tf.Graph為tf.Operation定義了命名空間
# 每一個操作必須有唯一的名字
# TensorFlow會通過在操作名字后面appending上_1,_2
# 如果所起的名字已經存在了,使用多個計算圖能夠讓你更好地控制每個計算節點

# 默認的圖存儲信息關於每個tf.Operation和tf.Tensor
# 如果你對程序創建了更大數量的沒有被連接的子圖
# 使用多個計算圖或許是更有效果的。因此, 不相關的狀態可以被垃圾收集
import tensorflow as tf
g_1 = tf.Graph()
with g_1.as_default():
    # Operations created in this scope will be added to 'g_1'
    c = tf.constant("Node in g_1")
    
    # Sessions created in this scope will run operations from 'g_1'
    sess_1 = tf.Session()
g_2 = tf.Graph()
with g_2.as_default():
    # operations created in this scope will be added to 'g_2'
    d = tf.constant("Node in g_2")

# Alternatively , you can pass a graph when constructing a 'tf.Session'
# 'sess_2' will run operations from 'g_2'
sess_2 = tf.Session(graph=g_2)
assert c.graph is g_1
assert sess_1.graph is g_1

assert d.graph is g_2
assert sess_2.graph is g_2

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM